To cover that we can indeed adapt the background job script.
$script={
param(
[string]$VMName,
[string]$vcServer,
[string]$vcId
)
Import-Module VMware.VimAutomation.Core
Connect-VIServer-Server $vcServer-Session $vcId
$vm=Get-VM-Name $VMName
if($vm.Guest.State-eq"Running"){
Shutdown-VMGuest -VM $vm-Confirm:$false-RunAsync
}
else{
Stop-VM-VM $vm-Confirm:$false-RunAsync
}
}
$fileName='.\input.csv'
$MaxJobs=10
$jobs=@()
Import-Csv-Path $fileName|ForEach-Object-Process {
if($jobs.Count-le$MaxJobs){
$job=(Start-Job-Verbose -ScriptBlock $script-ArgumentList $_.VMName,$global:DefaultVIServer.Name,$global:DefaultVIServer.SessionId)
$jobs+=$job
}
else{
Wait-Job-Job $jobs
$jobs=@()
}
}