Scripts Thread, reading variables in powershell script from text file in Coding and Web Development; Dear All,
I want to write a Windows powershell script which will read the variable from a text file, sothat ...
28th June 2011, 12:02 PM #1
- Rep Power
reading variables in powershell script from text file
I want to write a Windows powershell script which will read the variable from a text file, sothat I can use it further process. for example, I have text file demo.txt and the contents of it is--
Now, I have demo.ps1 script and I want to read server and user names separately to perform the specific task for each server-
connect server Server -username user
but when I am trying it with Get-Content, it is reading the whole line,not a single variable.
foreach( $Server in Get-Content C:\Scripts\demo.txt)
connect server $Server -username $user
Please provide me your valuable suggestions.
Thanks in Advance...
28th June 2011, 09:15 PM #2
Text file needed?
Looking at your post, you are not seperating the server details from the user info, you would probably need to use a csv format and read the file into an array to process after.
Are you set on using text files? Powershell has pretty easy xml handling, plus you have the advantage that you will be using a standard file type which you could access in other scripts with minimal recoding.
For a file called template.xml with the following layout
You could use some code like the following
-+-+-+-+-+-+-+-+ Code Starts -+-+-+-+-+-+-+-+-+-+-+-+
#Hard coded file path (resolves to c:\users\Loggedonuser........)
#Load xml File
$xml = New-Object XML #Create holder called $xml as an xml object
$xml.Load($filepath) #Tell PS to load the contents of $filepath into $xml
#Create new entry
$oldperson=@($xml.employee.person) #Load contents of person array position 0 into variable $oldperson
$newperson=$oldperson.Clone() #This creates an empty version of the person description stored at $oldperson
$newperson.firstname="Firstname2" #Set your new info
$newperson.lastname="Surname2" #Set your new info
#Append values to file
$xml.employee.AppendChild($newperson) #adds values of $Newperson to end of array
#Save back to file
$xml.employee.version="2.0" #Change value in xml header to new number (Could be automated)
$xml.Save("$home\Powershell Testing\updated_template.xml") #Save values stored in $xml to the file called updated_template.xml
Get-Content "$home\Powershell Testing\updated_template.xml" #Read the contents of the file updates_template.xml
$xml.employee.person #Echo the values of $xml.employee.person to screen
-+-+-+-+-+-+-+-+ Code Ends -+-+-+-+-+-+-+-+-+-+-+-+
Running the above code will leave you with two files, the original template.xml and the created updated_template.xml file.
If you wanted to read the contents then you could use something similar to $personfirstname=$xml.employee.person.firstname
Apologies if I am a little wrong but I am sat on a mac with no windows box nearby, however this should put you on roughly the right lines. If you want when I get to work tomorrow I will look for the reference I used when I was trying to get this working.
Last edited by mhundley; 28th June 2011 at 09:17 PM.
29th June 2011, 11:40 AM #3
- Rep Power
mhundley,thanks for reply.
Yes, I am collecting & storing these information in txt file and from there, I am trying to read all information from each server from that txt file to perform specific task.
Please suggest me how can I read it from txt file?
thanks in advance...
29th June 2011, 12:05 PM #4
I had a quick look on google and it returned this post from the scripting guys at MS.
The PowerShell Guy : Hey PowerShell Guy !,How Can I Parse a Tab-Delimited File and Then Save That as a Comma-Separated Values File?
I had a look but the command it leaves you with is not a pretty one, without knowing the extent of your text file I would still look to move either to an xml or csv type of file, MS have written both xml and csv import functions which should make it easier to access your data, using csv is documented here
I can understand that a simple text file is easier to read when you are using notepad or other text editor but if you are looking to do more complex things with the data then using an accepted file format may make things easier in the long run. Although as with all of these things learning a new way of doing things will present more challenges so your mileage may vary. :-)
If you can give us a little more info on exactly what you are looking to do the wider community on here may be able to offer more specific advice, they are generally really good at approaching things in a logical way and someone may already have the bare bones of what you need to give you a head start.
By deano in forum Windows
Last Post: 24th February 2011, 01:01 PM
Last Post: 10th February 2011, 07:57 AM
By Stuart_C in forum Coding
Last Post: 11th January 2011, 09:39 PM
By Gibbo in forum Scripts
Last Post: 15th September 2010, 03:49 PM
By fawkers in forum Scripts
Last Post: 17th November 2009, 09:55 AM
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)