Powershell“退出”表现得不像它应该的那样

时间:2015-01-19 08:09:52

标签: powershell exception-handling exit

我今天在我的一个PowerShell脚本中出现了一个奇怪的问题:

环境信息:我正在讨论的PowerShell脚本正在被VBScript调用。

if($VM -eq "Yes") {
    Add-PSSnapin VMware.VimAutomation.Core
    Connect-VIServer -Server $VMHost -User $VMUser -Password $VMPassword
    $Snapshot = $null
    Try {
        $Snapshot = New-Snapshot -Name $NameofSnapshot -VM $ServerName -Memory
        $CurrentPatchingState = "1;$Servername;Status=1;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Created Snapshot" | Out-File -Filepath  "C:\$Servername.txt" -Append -encoding ASCII 
    } Catch [system.exception] {
        $CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Wasnt able to take a Snapshot - Aborting" | Out-File -Filepath  "C:\$Servername.txt" -Append -encoding ASCII 
        Disconnect-VIServer -Server $VMHost -Confirm:$false
        exit
    }

    if ($Snapshot -eq $null) {
        $CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Wasnt able to get a Clean Snapshot - Aborting" | Out-File -Filepath  "C:\$Servername.txt" -Append -encoding ASCII 
        Disconnect-VIServer -Server $VMHost -Confirm:$false
        exit
    }
}

今天剧本在这部分失败了。日志文件显示:

2;xxxxxxxxxxxxxxx;Status=2;18.01.2015 11:01:51 Wasnt able to take a Snapshot - Aborting
2;xxxxxxxxxxxxxxx;Status=2;18.01.2015 11:01:51 Wasnt able to get a Clean Snapshot - Aborting

如何发生这种情况,因为脚本应该在第一次捕获时停止?

2 个答案:

答案 0 :(得分:1)

AFAICS代码应该按照您的期望进行。要缓解此问题,您可以在if块内移动内部try语句。我还将disconnect语句移到finally块。

if($VM -eq "Yes") {
    Add-PSSnapin VMware.VimAutomation.Core
    Connect-VIServer -Server $VMHost -User $VMUser -Password $VMPassword
    $Snapshot = $null
    Try {
        $Snapshot = New-Snapshot -Name $NameofSnapshot -VM $ServerName -Memory
        $CurrentPatchingState = "1;$Servername;Status=1;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Created Snapshot" | Out-File -Filepath  "C:\$Servername.txt" -Append -encoding ASCII 
        if ($Snapshot -eq $null) {
            $CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Wasnt able to get a Clean Snapshot - Aborting" | Out-File -Filepath  "C:\$Servername.txt" -Append -encoding ASCII 
            exit
        }
    } Catch [system.exception] {
        $CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Wasnt able to take a Snapshot - Aborting" | Out-File -Filepath  "C:\$Servername.txt" -Append -encoding ASCII 
        exit
    } finally {
        Disconnect-VIServer -Server $VMHost -Confirm:$false
    }
}

答案 1 :(得分:0)

试试这个:

    Try {
            $Snapshot = New-Snapshot -Name $NameofSnapshot -VM $ServerName -Memory -ErrorAction Stop
            $CurrentPatchingState = "1;$Servername;Status=1;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Created Snapshot" | Out-File -Filepath  "C:\$Servername.txt" -Append -encoding ASCII 
        }
Catch {
            $CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Wasnt able to take a Snapshot - Aborting" | Out-File -Filepath  "C:\$Servername.txt" -Append -encoding ASCII 
            Disconnect-VIServer -Server $VMHost -Confirm:$false
            exit
        }

仅当Try块中发生终止错误时才执行Catch块。 添加" -ErrorAction Stop"确保快照创建过程中的任何错误都将被视为终止错误。

其次,删除" [system.exception]"在Catch块的开头。这可以确保Catch块适用于任何类型的异常。