Jump to content

reading variables in powershell script from text file


Recommended Posts

Posted

Dear All,

 

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--

Server=xx.xx.xx.xx user=root

Server=xx.xx.xx.xx user=root1

 

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...

Posted (edited)

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

 

Firstname1

Surname1

 

You could use some code like the following

 

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

-+-+-+-+-+-+-+-+ Code Starts -+-+-+-+-+-+-+-+-+-+-+-+

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

 

#Hard coded file path (resolves to c:\users\Loggedonuser........)

$filepath="$home\Powershell Testing\template.xml"

 

#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)[0] #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

 

#List Data

$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.

 

Firstname1

Surname1

Firstname2

Surname2

 

 

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.

Edited by mhundley
Posted

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...

Posted

Vanit,

 

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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...