geek sync | learn the powershell essentials on error trapping

14
The Essential PowerShell on Error Trapping August 10, 2016

Upload: idera-software

Post on 28-Jan-2018

236 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Geek Sync | Learn the PowerShell Essentials on Error Trapping

The Essential PowerShell on Error Trapping

August 10, 2016

Page 2: Geek Sync | Learn the PowerShell Essentials on Error Trapping

Using Try-Catch-Finally

What's already available?

Working with Errors

Questions

3

1

2

4

1

Page 3: Geek Sync | Learn the PowerShell Essentials on Error Trapping

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

Page 4: Geek Sync | Learn the PowerShell Essentials on Error Trapping

Visual Studio Community (free)

Integrating your tools

• Idera’s PowerShellPlus (and others)

• PowerShell Tools for VS 2013( or 2015)

4

Page 5: Geek Sync | Learn the PowerShell Essentials on Error Trapping

What's already available in PowerShell?

• PowerShell session variables

• PowerShell Environment variables

• PowerShell Documentation “Get-Help”

6

Page 6: Geek Sync | Learn the PowerShell Essentials on Error Trapping

What's already available in PowerShell?

• Demo 1 – Variables and Help

6

Page 7: Geek Sync | Learn the PowerShell Essentials on Error Trapping

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

Page 8: Geek Sync | Learn the PowerShell Essentials on Error Trapping

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

Page 9: Geek Sync | Learn the PowerShell Essentials on Error Trapping

Working with Errors 7

• Demo 2 – CmdletBinding and Errors Trapping

Page 10: Geek Sync | Learn the PowerShell Essentials on Error 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

Page 11: Geek Sync | Learn the PowerShell Essentials on Error Trapping

Using Try-Catch-Finally

9

• Demo 3 – Try | Catch | Finally sample

Page 12: Geek Sync | Learn the PowerShell Essentials on Error Trapping

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

Page 13: Geek Sync | Learn the PowerShell Essentials on Error Trapping

Maximo Trinidad

Email: [email protected]

Tweeter: @maxtrinidad

Contact information:

Page 14: Geek Sync | Learn the PowerShell Essentials on Error Trapping

Head over to IDERA.com to

download any of our product trials

free for 14-days.

Free Trials