- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Create SnapShot with error reporting (Try/Catch)
Ok so my goal is to create a script that will automate creating a snapshot of a server but will catch errors and send an email based off the error.
From what I can tell Try/Catch blocks will be the best way to accomplish this. This is what I have so far but I cant get it to catch the error.
Here is the snippet of the code
try
{
Get-VM $serverVMName | new-snapshot -Name "dppfr-$serverVMName-$TimeStr" -Description "Automated backup"
}
catch [system.exception]
{
sendError
}
Any thoughts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You should add the parameter '-ErrorAction stop' on the New-Snapshot cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If i put the ErrorAction stop it catches the error but then also executes the catch even on successes. I have noticed that it executes the catch before the snapshot can finish, it has barely started by the time it executes the catch action.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ok a little more info, it works if i close and reopen the powershell window. Is it possible its storing the System.Exception ?? Is there a way to clear it after each run?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Also when you leave out the specific exception type on the Catch block ?
Try{
Get-VM $serverVMName |
New-Snapshot -Name "dppfr-$serverVMName-$TimeStr" -Description "Automated backup" -ErrorAction stop
}
Catch{
sendError
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ok a little more info, it works if i close and reopen the powershell window. Is it possible its storing the System.Exception ?? Is there a way to clear it after each run?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There should be no need to do that.
From where do you run the script ? From the PowerCLI prompt ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference