Passing multiple values to a single PowerShell script parameter

后端 未结 4 1792
长发绾君心
长发绾君心 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:23

    The easiest way is probably to use two parameters: One for hosts (can be an array), and one for vlan.

    param([String[]] $Hosts, [String] $VLAN)
    

    Instead of

    foreach ($i in $args)
    

    you can use

    foreach ($hostName in $Hosts)
    

    If there is only one host, the foreach loop will iterate only once. To pass multiple hosts to the script, pass it as an array:

    myScript.ps1 -Hosts host1,host2,host3 -VLAN 2
    

    ...or something similar.

提交回复
热议问题