geek sync | learn the powershell essentials on error trapping
TRANSCRIPT
The Essential PowerShell on Error Trapping
August 10, 2016
Using Try-Catch-Finally
What's already available?
Working with Errors
Questions
3
1
2
4
1
Some Tools for the DBA’s
IDERA PowerShellPlus Tool
https://www.idera.com/productssolutions/freetools/powershellplus
Download SQL Server Data Tools Preview in Visual Studio 2015
https://msdn.microsoft.com/en-us/mt429383
Download SQL Server Data Tools Preview in Visual Studio 2013
https://www.microsoft.com/en-us/download/details.aspx?id=42313
2
Visual Studio Community (free)
Integrating your tools
• Idera’s PowerShellPlus (and others)
• PowerShell Tools for VS 2013( or 2015)
4
What's already available in PowerShell?
• PowerShell session variables
• PowerShell Environment variables
• PowerShell Documentation “Get-Help”
6
What's already available in PowerShell?
• Demo 1 – Variables and Help
6
Working with Errors
Error components during execution
a. $error - Variable use to store the error information
b. $ErrorActionPreference – default value “Continue“
c. *Common parameter:
-ErrorVariable
-ErrorAction
*note: On custom functions, only available when using
"[CmdletBinding()]".
7
Working with Errors
Using Commom Parameters on Errors
a. Included in all PowerShell cmdlets:
-ErrorVariable - Your assign variable name
without the "$".
-ErrorAction - Cmdlet modifiable response
b. *ErrorAction available response:
- Continue
- SilentlyContinue (covering)
- Others: “Ignore | Inquire | Stop | Suspend”
7
Working with Errors 7
• Demo 2 – CmdletBinding and Errors Trapping
Using Try-Catch-Finally
Use in the script file (or functions) to help identily and trap
errors efficiently in the body of the code:
try { .. code .. }
catch {.. What to do with Error ..}
finally { .. Code always run ..}
For more information, check the help documentation:
PS> Get-Help About_Try_Catch_Finally –ShowWindow;
7
Using Try-Catch-Finally
9
• Demo 3 – Try | Catch | Finally sample
Don’t forget to check out the
previously recorded Geek Sync
webcasts to learn more about
PowerShell and SQL Server.
Thank You!
https://www.idera.com/events/geeksync
Head over to IDERA.com to
download any of our product trials
free for 14-days.
Free Trials