I created this script to create multiple VM's remotely on the Hyper-V server, however, it doesn't seem to create the VM's. I can create one directly on the Hyper-V server using the given parameters while using Enter-PSSession, but not via invoke-command? (there are no error messages, it just prints a blank line and goes back to the prompt)
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)][int]$newvmcount #require number of temp vm's to create
)
Invoke-Command -ScriptBlock {foreach ($vmnumber in $newvmcount){New-VM -Name "Windows10TMP$vmnumber" -BootDevice NetworkAdapter -Generation 2 -SwitchName LAN -MemoryStartupBytes 1gb -NewVHDPath "F:\hypervvirtualmachines\Windows10TMP$vmnumber.vhdx" -NewVHDSizeBytes 127gb -verbose}} -ComputerName hypervserver -ArgumentList $newvmcount
You can apply the $using scope modifier. It would look like this
$using:newvmcount
which let's you use the variable defined outside of the invoke-commandApply How to pass arguments for remote commands:
Either declare the parameter(s) at the beginning of your scriptblock:
or access argument(s) using the automatic variable
$args
.BTW, I can't understand the
foreach
loop as[int]$newvmcount
does not seem to be a collection…