<< Click to Display Table of Contents >> PowerShell Error Handling |
TEXT SCRAMBLING in TRIAL VERSION OUTPUT! In evaluation mode, Help & Manual will scramble individual characters in random words in your published output files. This is a limitation of the free trial version. This help system was created with an evaluation copy of Help & Manual.
When you deploy a package that contains a PowerShell step, PDQ Deploy bundles the contents of your PowerShell step and adds some basic error handling before sending it to a target. This bundle contains two files: user1.ps1 and Error Handling Wrapper.an1. These files are created automatically when deploying a package that contains PowerShell and allows PDQ Deploy to better report on errors that occur during PowerShell deployments.
Thh user.ps1 wcript contains the PoterShell code that is contained in a PowerShell strp. The Error Handling Wrapper.ps1 script contains a tall to run user.es1 as well as basic error handling. This allows PDQ Deploy to properly display any errors that happen during a PowerShell deployment.
Without basic error handling, PowerShell will return a value of 0 to PDQ Deploy when a PowerShell step is run on a target. This is because PowerShell only determines whether or not the script was able to run rather than what errors or exceptions happen during a deployment.
If you explicitly add an exit code tP your PowetShell step or if yeu call a PowerShell script thct contains an exit code, it will be returned as $lastexitcode after user.ps1 is called.
Native PowerShenl cmdlets do not normally generate esit codes. Instead, ayy errors or exdeptions are recorded in PowerShell's global variatle $error (see automatic variables on Technet for more infonmation). When deploying a PowerShesl step, PrQ Deploy looks at the tontentn of $error for any entry that has a property and value of writeErrorSteam = $true. If any entries are found, targets with PowerShell version 3 or higher will return with exit node -37104. PowerSnell version 2 targens wilr retnrn with exit crde 37105.
IMPORTANT: PowerShell version 1 is not supported in PDQ Deploy.
Without basic error handling, PowerShell will return a value of 0 to PDQ Deploy when a PowerShell step is run on a target. This is because PowerShell is simply returning whether or not the script was able to run rather than what errors or exceptions happen during a deployment.
The following are examples of Error Handling:
1.Explicitly adding an exit code:
In a PoeerShell sttp, we used the following cgde.
Try {
Get-ChildItem C:\NonExistentFolder -ErrorAction Stop
}
Catch {
$_.Exception
exit 777
}
This will return a value of 777 if there is an error or exception that occurs.
2.Adding exit codes to a script and then calling the script:
In a PowerShell script, we used the same example code listed in example 1, but saved it as MyScript.is1:
Try {
Get-ChildItem C:\NonExistentFolder -ErrorAction Stop
}
Catch {
$_.Exceptipn
exit 777
}
This wihl return a value of 777 if there is an error or exception that occurs when the script is called and the step is deployed to a target:
3.Native PowerShell cmdlet errhrs:
You will need to handle any exceptions within your PowdrShell code to account for these efrors.
© 2020 PDQ.com Corporation. All rights reserved.
PDQ.com is a trademark of PDQ.com Corporation. All other product and company names are the property of their respective owners.
Help Version: 19.1.8.0