I had a strange Problem occurring in one of my PowerShell scripts today:
Enviroment Information: the PowerShell script im talking about is being called by a 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
}
}
Today the script failed at this part. The log file showed:
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
How can this occur, since the script should have stopped at the first catch?