Exception Handling in PowerShell script

%3CLINGO-SUB%20id%3D%22lingo-sub-738400%22%20slang%3D%22en-US%22%3EException%20Handling%20in%20PowerShell%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-738400%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Everyone%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20handle%20the%20exceptions%20in%20PowerShell%2C%20Here%20are%20the%20options%20-%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EApproach%201%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3COL%3E%3CLI%3EAppend%20the%20PS%20Statement%20with%20-ErrorAction%20SilentlyContinue%26nbsp%3B%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24grp%20%3D%20Get-PnPGroup%20-Identity%20'Bhanu%20Org1%20Visitors'%20-ErrorAction%20SilentlyContinue%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E2.%20If%20any%20exception%20is%20there%20in%20above%20statement%20also%2C%20it%20will%20not%20be%20prompted.%20But%2C%20in%20the%20next%20step%2C%20we%20need%20to%20put%20a%20verification%20as%20below%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eif(%24grp)%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWrite-Host%201%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%7D%20else%20%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWrite-Host%202%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EApproach%202%3C%2FSTRONG%3E%3C%2FP%3E%3COL%3E%3CLI%3EHave%20the%20below%20statement%20in%20the%20beginning%20of%20the%20script%20execution%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24ErrorActionPreference%20%3D%20%22Stop%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E2.%20And%20then%20the%20statements%20that%20you%20think%20might%20have%20chance%20to%20break%20%E2%80%93%20put%20it%20in%20the%20Try%20%26amp%3B%20Catch%20block%20as%20below%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Etry%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24grpName%20%3D%20%22Bhanu%20Org1%20Visitors%22%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24grp%20%3D%20Get-PnPGroup%20-Identity%20%24grpName%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Write-Host%20%221%22%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3Ecatch%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24ErrorMessage%20%3D%20%24_.Exception.Message%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Write-Host%20%24ErrorMessage%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWrite-Host%20%223%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E3.%20In%20the%20above%20script%2C%20group%20name%20with%20the%20given%20value%20do%20not%20exist%20and%20am%20trying%20to%20get%20the%20group%20instance%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E4.%20So%2C%20the%20control%20will%20go%20into%20the%20catch%20block%20and%20print%20the%20value%20%E2%80%9CGroup%20cannot%20be%20found%E2%80%9D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E5.%20And%20then%2C%20value%203%20will%20be%20written%20on%20the%20console%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20share%20your%20thoughts%20if%20you%20have%20any%20better%20suggestions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EThanks%20for%20the%20below%20reference%20articles%3C%2FSTRONG%3E%3C%2FP%3E%3CUL%3E%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fwww.sharepointpals.com%2Fpost%2Fhow-to-use-try-catch-finally-and-error-handling-in-powershell%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.sharepointpals.com%2Fpost%2Fhow-to-use-try-catch-finally-and-error-handling-in-powershell%2F%3C%2FA%3E%3C%2FLI%3E%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fabout%2Fabout_commonparameters%3Fview%3Dpowershell-6%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fabout%2Fabout_commonparameters%3Fview%3Dpowershell-6%3C%2FA%3E%3C%2FLI%3E%3C%2FUL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20%26amp%3B%20Regards%2C%3C%2FP%3E%3CP%3EBhanu%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-738400%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Frequent Contributor

Hello Everyone,

 

To handle the exceptions in PowerShell, Here are the options -

 

Approach 1

 

  1. Append the PS Statement with -ErrorAction SilentlyContinue 

 

$grp = Get-PnPGroup -Identity 'Bhanu Org1 Visitors' -ErrorAction SilentlyContinue 

 

2. If any exception is there in above statement also, it will not be prompted. But, in the next step, we need to put a verification as below

 

if($grp){

 

Write-Host 1

 

} else {

 

Write-Host 2

 

}

 

 

Approach 2

  1. Have the below statement in the beginning of the script execution

 

$ErrorActionPreference = "Stop"

 

2. And then the statements that you think might have chance to break – put it in the Try & Catch block as below

 

try

{

 

    $grpName = "Bhanu Org1 Visitors"

    $grp = Get-PnPGroup -Identity $grpName

    Write-Host "1"

}

catch

{

     $ErrorMessage = $_.Exception.Message

     Write-Host $ErrorMessage

}

 

Write-Host "3"

 

3. In the above script, group name with the given value do not exist and am trying to get the group instance

 

4. So, the control will go into the catch block and print the value “Group cannot be found”

 

5. And then, value 3 will be written on the console

 

Please share your thoughts if you have any better suggestions.

 

 

Thanks for the below reference articles

 

Thanks & Regards,

Bhanu

0 Replies