在PowerCLI中关闭3位客人

时间:2018-02-01 21:59:00

标签: powershell vmware powercli

我正在运行一个脚本来关闭我的所有VM,然后再拍摄它们的快照。目前,该脚本一次只能执行1次...是否可以将数字增加到3?

$sleepTime = 5   
$maxWaitTime = 60

$vmNames1 = Import-Csv "C:\vmnames1.csv" -UseCulture | %{$_.vmName}

Get-VM -Name $vmNames1 | where {$_.PowerState -eq "PoweredOn"} | %{

# Guest shutdown
 if($_.Extensiondata.Guest.ToolsRunningStatus -ne "guestToolsNotRunning"){
    Shutdown-VMGuest -VM $_ -Confirm:$false
    $waitTime = 0
    while((Get-VM $_.Name).PowerState -ne "PoweredOff" -and $waitTime -lt $maxWaitTime){
        sleep $sleepTime
        $waitTime += $sleepTime
    }
    $line = $_.Name + " guest shutdown"
}

}

1 个答案:

答案 0 :(得分:0)

$sleepTime = 5   
$maxWaitTime = 60

$vmnames = "vm1","vm2","vm3","vm4","vm5","vm6","vm7","vm8","vm9"
for ($i = 0; $i -lt $vmnames.count; $i += 3) {
   $threevms += ,@($vmnames[$i..($i+2)]);
}
foreach($v in $threevms){
    Get-VM -Name $v | where {$_.PowerState -eq "PoweredOn"} | %{
    # Guest shutdown
    if($_.Extensiondata.Guest.ToolsRunningStatus -ne "guestToolsNotRunning"){
            Shutdown-VMGuest -VM $_ -Confirm:$false
            $waitTime = 0
            while((Get-VM $_.Name).PowerState -ne "PoweredOff" -and $waitTime -lt $maxWaitTime){
                sleep $sleepTime
                $waitTime += $sleepTime
            }
            $line = $_.Name + " guest shutdown"
        }
    }
}

如上所述,Get-VM将列出一个列表。