Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple values to a single PowerShell script parameter

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 host1 host2 host3  foreach ($i in $args) {     Do-Stuff $i } 

I'd like to add a named optional parameter called vlan. I've tried:

Param(     [string]$vlan )  foreach ($i in $args) {     Write-Host $i } Write-Host $vlan 

It works if you pass a -vlan parameter but if you don't then the script auto assigns the last server name to $vlan.

So, how can you pass single or multiple parameters plus an optional named parameter to a PowerShell script?

Ideally, here are valid examples:

.\script.ps1 host1 .\script.ps1 host1 host2 host3 .\script.ps1 host1 host2 -vlan office 
like image 546
jcarpio Avatar asked Feb 27 '13 19:02

jcarpio


People also ask

How do I pass multiple values in a PowerShell script?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

How do I add multiple values to a variable in PowerShell?

In PowerShell, you can assign values to multiple variables using a single command. The first element of the assignment value is assigned to the first variable, the second element is assigned to the second variable, the third element to the third variable. This is known as multiple assignment.

How do you pass parameters in PowerShell script?

Passing arguments in PowerShell is the same as in any other shell: you just type the command name, and then each argument, separated by spaces. If you need to specify the parameter name, you prefix it with a dash like -Name and then after a space (or a colon), the value.

What is param () in PowerShell?

Parameters can be created for scripts and functions and are always enclosed in a param block defined with the param keyword, followed by opening and closing parentheses. param() Inside of that param block contains one or more parameters defined by -- at their most basic -- a single variable as shown below.


2 Answers

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.

like image 91
Bill_Stewart Avatar answered Sep 28 '22 17:09

Bill_Stewart


One way to do it would be like this:

 param(        [Parameter(Position=0)][String]$Vlan,        [Parameter(ValueFromRemainingArguments=$true)][String[]]$Hosts     ) ... 

This would allow multiple hosts to be entered with spaces.

like image 30
Nitz Avatar answered Sep 28 '22 17:09

Nitz