Powershell "Exit" 表现不正常
Powershell "Exit" not behaving like it should
今天我的一个 PowerShell 脚本中出现了一个奇怪的问题:
环境信息:我所说的 PowerShell 脚本正在被 VBScript 调用。
if($VM -eq "Yes") {
添加 PSSnapin VMware.VimAutomation.Core
连接-VIServer -服务器 $VMHost -用户 $VMUser -密码 $VMPassword
$快照 = $null
尝试 {
$Snapshot = New-Snapshot -Name $NameofSnapshot -VM $ServerName -Memory
$CurrentPatchingState = "1;$Servername;Status=1;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') 创建快照" | Out-File -Filepath "C:$Servername.txt" -Append -encoding ASCII
} 抓住 [system.exception] {
$CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') 无法拍摄快照 - 中止" | Out-File -Filepath "C:$Servername.txt" -Append -encoding ASCII
断开连接-VIServer -Server $VMHost -Confirm:$false
退出
}
如果($快照-eq $null){
$CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') 无法获得干净的快照 - 中止" | Out-File -Filepath "C:$Servername.txt" -Append -encoding ASCII
断开连接-VIServer -Server $VMHost -Confirm:$false
退出
}
}
今天脚本在这部分失败了。日志文件显示:
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
这怎么会发生,因为脚本应该在第一次捕获时停止?
AFAICS 代码应该符合您的预期。要缓解此问题,您可以将内部 if
语句移动到 try
块中。我还将断开连接语句移动到 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
<b>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
}</b>
} 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
} <b>finally {
Disconnect-VIServer -Server $VMHost -Confirm:$false
}</b>
}
试试这个:
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”可确保快照创建期间的任何错误都将被视为终止错误。
其次,删除 Catch 块开头的“[system.exception]”。这确保了 Catch 块将适用于任何类型的异常。
今天我的一个 PowerShell 脚本中出现了一个奇怪的问题:
环境信息:我所说的 PowerShell 脚本正在被 VBScript 调用。
if($VM -eq "Yes") {
添加 PSSnapin VMware.VimAutomation.Core
连接-VIServer -服务器 $VMHost -用户 $VMUser -密码 $VMPassword
$快照 = $null
尝试 {
$Snapshot = New-Snapshot -Name $NameofSnapshot -VM $ServerName -Memory
$CurrentPatchingState = "1;$Servername;Status=1;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') 创建快照" | Out-File -Filepath "C:$Servername.txt" -Append -encoding ASCII
} 抓住 [system.exception] {
$CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') 无法拍摄快照 - 中止" | Out-File -Filepath "C:$Servername.txt" -Append -encoding ASCII
断开连接-VIServer -Server $VMHost -Confirm:$false
退出
}
如果($快照-eq $null){
$CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') 无法获得干净的快照 - 中止" | Out-File -Filepath "C:$Servername.txt" -Append -encoding ASCII
断开连接-VIServer -Server $VMHost -Confirm:$false
退出
}
}
今天脚本在这部分失败了。日志文件显示:
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
这怎么会发生,因为脚本应该在第一次捕获时停止?
AFAICS 代码应该符合您的预期。要缓解此问题,您可以将内部 if
语句移动到 try
块中。我还将断开连接语句移动到 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
<b>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
}</b>
} 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
} <b>finally {
Disconnect-VIServer -Server $VMHost -Confirm:$false
}</b>
}
试试这个:
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”可确保快照创建期间的任何错误都将被视为终止错误。
其次,删除 Catch 块开头的“[system.exception]”。这确保了 Catch 块将适用于任何类型的异常。