Multiple inputs to a variable (I know this has been covered)


Simple issue but I am having a hard time wrapping my head around parameters. I am going to get a listing of all the VM’s on a host and want to be able to pass multiple vm’s to the variable so I can vMotion them. So I want this:

VM’s on Host One:





Which VM’s do you want to move ? (enter names): VM2,VM3 (this would be a read-host)

{Foreach here with vmotion code}

I just want to understand parameters, as my guess is I need to use them to do multiple values input to a variable or array.

What am I missing? Please explain in detail, I’ve seen stuff online but having trouble understanding. Just want to understand this.


I’m not completely sure if I got it right. But I think a Read-Host is a bad idea for this kind of choice. You may use an Out-GridView instead. The user could select all desired VMs and submit it and you don’t have to parse an error prone plain text input. :wink:

Please read the complete help including the examples for Out-GridView to learn how to use it.

It sounds like you are not really using parameters. Parameters are used in functions, so you can type the function name in the CLI followed by the parameter and its value, you are using Read-Host for input instead.

Read-Host does not accept comma separated values. It will treat it as a single string. I guess that is where you might be having a problem. You can use the split method but you need to make sure the output is a comma separated list, I would also recommend not using any kind of quotes in the input string.

You would probably want something like this.

$VMHost = Get-VMHost MyHost1
$VMs = $VMHost | Get-VM | Sort-Object Name
Write-Host "VMs on Host $($VMHost):"
Write-Output $VMs | Select-Object Name | Out-Default <#Out-Default required to list the VMs before Read-Host#>
$VMsToMove = Read-Host 'Enter VMs to move separated by commma and without quotes'
$VMsToMove = $VMsToMove.Split(',')

#vMotion VMs
Move-VM -VM $VMsToMove -Destination MyHost2 -VMotionPriority High -RunAsync