Powershell脚本增强

我们有4个Hyper-V群集,并且为这些Hyper-V群集中的VM对象启动了一个脚本。

该脚本工作正常,但脚本只会启动第一个群集中的所有虚拟机,而不是下一个。

有没有什么方法可以让我们在多个集群上同时启动虚拟机。

$clusters = Get-Content "c:\temp\Clusters.txt" foreach ($clu in $clusters){ while($true) { write-host "Cluster VM resources bringing online for cluster $clu" -ForegroundColor Green $c = Get-Cluster -name $clu | Get-ClusterResource | where { $_.Name -and $_.state -eq "offline"} $count = $c.Length write-host "Current Count: $count" -ForegroundColor Green if ($count -eq 0){ break }else{ echo $c[0..5] |Start-ClusterResource -ErrorAction SilentlyContinue -Verbose Start-Sleep 20 } } } 

您可以使用Start-Job在后台运行cmdlet或脚本块:

 $clusters = Get-Content "c:\temp\Clusters.txt" foreach ($clu in $clusters) { Start-Job -ScriptBlock { Get-Cluster -name $clu | Get-ClusterResource | where { $_.Name -and $_.state -eq "offline"} |ForEach-Object { $_ |Start-ClusterResource -ErrorAction SilentlyContinue -Verbose } } } 

这没有经过testing(我没有可用于testing的hyper-v集群)。