cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
684
Views
0
Helpful
4
Replies

Powershell catching Exceptions

rvadlapa
Level 1
Level 1

This is a sample code:

try {

$x = 0

    $i = 1/$x

    Write-Host $i

} catch [Exception] {

Write-Output "I am in Exception"

}

Write-Output "Completed"

In Powershell, the output I get is as expected.

In CPO, the customer is expecting the same, i.e for the code to go into exception handler. Currently, it is not and the actual exception is thrown.

CPO Version: 2.3.2

Thanks

4 Replies 4

Shaun Roberts
Cisco Employee
Cisco Employee

Do you have a better example than a divide by zero? It does fail the script and returns a -1 code. If all you care about is failing or not failing then you can look for that output code.

If you have a more detailed use case and script I would suggest opening a TAC case and getting their support.

-shaun

--Shaun Roberts
Principal Engineer, CX
shaurobe@cisco.com

Thanks Shaun.

It's not really about the script but just the behaviour of the powershell acitivity. The code doesn't go into a catch during an exception.

If this was the intended behaviour, then I assume it's to check the output code's and branch out accordingly and not having a huge powershell script in the activity.

The customer was expecting otherwise.

PO doesn't run the PowerShell script.  We hand the script off to PowerShell, possibly on a remote environment.  Just like when you run PowerShell from the command line or as a process, a failed script just returns an exit code, not the actual exception. This is what the activity sees, same as if you ran the script you paste into the activity as a file via the command line on the (possibly remote) Windows machine.  The script itself needs to catch the script and do anything with it you want.  In the PO process, your error handler can react to the error code returned by the Powershell script.

Justin Yancey came up with the following code snip to catch and output additional error information.  Paste it in just below your Param section.

I haven't tried this personally, but it looked useful so I kept it around.  Let us know if it helps.

trap {

  "Error Category {0}`nError Type {1}`nID: {2}`nMessage: {3}" -f

  $_.CategoryInfo.Category, $_.Exception.GetType().FullName,

  $_.FullyQualifiedErrorID, $_.Exception.Message;

  continue

}

$ErrorActionPreference = "Stop"

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: