input from file - quick question

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 11 months ago.

  • Author
    Posts
  • #5502

    by chz013 at 2013-02-03 21:40:30

    Hi All

    Could someone share a few examples on how to pass file parameter to the powershell script and perform its function reading inputs from a file as its argument ?
    eg

    powershell.exe myscript filename.txt

    where filename.txt contains a list of hostnames:
    abc.ub.com
    papa.utm.com
    :
    :

    Thanks

    by DonJ at 2013-02-04 10:33:57

    You put a parameter block at the beginning of the script:


    param([string]$filename)

    Within the script, $filename will contain whatever file name was passed. You would run the script like this:


    myscript filename.txt

    Or more properly:


    myscript -filename filename.txt

    And in either case, $filename would contain "filename.txt." You could then read that file by using Get-Content.

    If you want to provide the CONTENTS of the file as a parameter value:


    param([string[]]$computername)
    foreach ($computer in $computername)
    # $computer contains one computer name at a time
    }

    And run as:


    Myscript -computername (Get-Content names.txt)

    Where names.txt contains one name per line.

You must be logged in to reply to this topic.