ISE与CLI

时间:2018-09-27 16:56:05

标签: powershell powercli

在Powershell ISE中,满足第一个条件。在CLI中运行相同的脚本时,即使条件相同,它也会跳过if语句。 CLI仅输出结尾的“ else”语句。它似乎甚至没有评估前2条语句。有任何想法吗?

foreach ($vm in (Get-VM -Name $vm)) {
    if($vm.ExtensionData.Runtime.PowerState -eq "poweredOn") {
        Shutdown-VMGuest -VM $vm -Confirm:$false
        while ($vm.ExtensionData.Runtime.PowerState -eq "poweredOn" -and ($x++ -lt 60))
        {
            Start-Sleep -Seconds 1
            $vm.ExtensionData.UpdateViewData("Runtime.PowerState")
            Write-Output "Waiting for $vm to shutdown gracefully. Took $x`s."
            if ($x -gt 5) {
                Write-Output "Forcefully powering off VM $vm"
                Stop-VM $vm -Confirm:$false
            }
        }
    }
    elseif($vm.ExtensionData.Runtime.PowerState -eq "poweredOff") {
        $DateTime = "$date"+"_autosnap"
        New-Snapshot -VM $vm -Name ("$date"+"_autosnap")
        Start-VM -VM $vm
    }
    else {
        Write-Host "Snapshot failed. Machine is not shutdown."
    }
    Write-Host "Cleaning up previous snapshots for $vm"
    Get-Snapshot -VM $vm | Where-Object {$_.Name -match "$retention"+"_autosnap"} | Remove-Snapshot
} 

1 个答案:

答案 0 :(得分:0)

我将看看如何更改您的$vm变量用法,这可能会使终端和ISE感到困惑。

也许将Name参数中使用的变量改为$vmname。看起来像以下内容:

foreach ($vm in (Get-VM -Name $vmname)) {