+ Post New Thread
Results 1 to 4 of 4
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 ...
  1. #1

    Join Date
    Oct 2010
    Posts
    16
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    reading variables in powershell script from text file

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

  2. #2
    mhundley's Avatar
    Join Date
    Oct 2007
    Location
    Worcester
    Posts
    47
    Thank Post
    11
    Thanked 8 Times in 7 Posts
    Rep Power
    16

    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

    <employee version="1.0">
    <person>
    <firstname>Firstname1</firstname>
    <lastname>Surname1</lastname>
    </person>
    </employee>

    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.

    <employee version="2.0">
    <person>
    <firstname>Firstname1</firstname>
    <lastname>Surname1</lastname>
    </person>
    <person>
    <firstname>Firstname2</firstname>
    <lastname>Surname2</lastname>
    </person>
    </employee>


    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.

  3. #3

    Join Date
    Oct 2010
    Posts
    16
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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...

  4. #4
    mhundley's Avatar
    Join Date
    Oct 2007
    Location
    Worcester
    Posts
    47
    Thank Post
    11
    Thanked 8 Times in 7 Posts
    Rep Power
    16
    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.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 2
    Last Post: 24th February 2011, 01:01 PM
  2. Replies: 0
    Last Post: 10th February 2011, 07:57 AM
  3. Replies: 4
    Last Post: 11th January 2011, 09:39 PM
  4. Script to write the date to a text file
    By Gibbo in forum Scripts
    Replies: 4
    Last Post: 15th September 2010, 03:49 PM
  5. Powershell script to list printers with WPK
    By fawkers in forum Scripts
    Replies: 1
    Last Post: 17th November 2009, 09:55 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •