Piping several parameters to a Script

This topic contains 3 replies, has 2 voices, and was last updated by  Ivan 1 year, 1 month ago.

  • Author
  • #72830


    Let's say I want to change the host description on several PCs at once, and I have a script like this:

    Set-HostNameDescription -ComputerName myHost -Description "This is my PC"

    Now, I have a list of hosts and their descriptions in a text file, my question is, how should I format that text file to properly pipe the values? like this:

    Get-Content C:\Stash\HostsList.txt | Set-HostNameDescription

    I have defined both parameters as mandatory, ValueFromPipeline and all that good stuff.

  • #72833

    Don Jones

    Assuming your ComputerName parameter is of type [string[]], and accepts ValueFromPipeline, and you've properly implemented a PROCESS{} block, then your text file would be one name per line.

    See "Learn PowerShell Toolmaking in a Month of Lunches" for a walkthrough on pretty much exactly this, if you need.

  • #72836

    Don Jones

    Ah – I should point out that you couldn't pipe the Description AND ComputerName from a text file like that.

    What you could do is rig both parameters to accept ValueFromPipelineByPropertyName (Mandatory has no bearing here), and instead of a text file, use a CSV file having a ComputerName column and a Description column. Import that using Import-CSV, and pipe it to your command. Again, assuming you've properly implemented a PROCESS{} block in the command.

  • #72842


    Ah, I see, yes, the script has PROCESS{} and all that, it works fine but since there are several parameters to be inputted I wasn't sure about the type of file, I knew a comma delimited wouldn't work.

    Regarding "Learn PowerShell Toolmaking in a Month of Lunches", dude, I've been devouring all those videos and others, I'm hooked.


You must be logged in to reply to this topic.