Passing multiple values to a single PowerShell script parameter

后端 未结 4 1793
长发绾君心
长发绾君心 2020-12-04 14:52

I have a script to which I pass server name(s) in $args.

This way I can do stuff to this (these) server(s) using foreach:

.\\script.ps1          


        
4条回答
  •  感动是毒
    2020-12-04 15:45

    Parameters take input before arguments. What you should do instead is add a parameter that accepts an array, and make it the first position parameter. ex:

    param(
        [Parameter(Position = 0)]
        [string[]]$Hosts,
        [string]$VLAN
        )
    
    foreach ($i in $Hosts)  
    { 
        Do-Stuff $i
    }
    

    Then call it like:

    .\script.ps1 host1, host2, host3 -VLAN 2
    

    Notice the comma between the values. This collects them in an array

提交回复
热议问题