powershell icon indicating copy to clipboard operation
powershell copied to clipboard

[BUG] -ErrorAction "SilentlyContinue" is ignored when the cmdlet throws a Terminating Error

Open Studermarc opened this issue 3 years ago • 1 comments

Reporting an Issue or Missing Feature

When executing a Cmdlet with the ' -ErrorAction "SilentlyContinue" ' Parameter specified. The Cmdlet still throws a Terminating Error when a Terminatin Error occurs.

Expected behavior

It is expected that when the ' -ErrorAction "SilentlyContinue" ' Parameter is specified, that Terminating Errors are suppressed.

Actual behavior

Currently the Terminating Errors still come through, even if the ' -ErrorAction "SilentlyContinue" ' Parameter is specified.

Steps to reproduce behavior

  1. Execute a Cmdlet that will throw en Exception on Failure. E.g. "Get-PnPGroup" with the ' -ErrorAction "SilentlyContinue" ' Parameter
  2. Force an Exception by Providing it Input that will fail. In this case, I am providing it the Identity of a SP Group that doesn't exist
$ConnectionParameters = @{
    "ClientId"   = $ClientId
    "Thumbprint" = $Thumbrint
    "Tenant"     = $Tenant
    "Url"        = $SiteUrl
}
$SiteConnection = Connect-PnPOnline @ConnectionParameters -ReturnConnection

$ErrorActionPreference = 'Continue'

try {
    $SPGroup = Get-PnPGroup -Identity "GroupThatDoesntExist" -Connection $SiteConnection -ErrorAction "SilentlyContinue"
} catch {
    Write-Output "A Terminating Error was thrown!"
    $_
}

The Script above will yield to the following Output:

A Terminating Error was thrown!

Get-PnPGroup: 
Line |
  12 |  …  $SPGroup = Get-PnPGroup -Identity "GroupThatDoesntExist" -Connection …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Site group not found

Error Stacktrace:

Message          : Site group not found
Stacktrace       :    at PnP.PowerShell.Commands.Base.PnPConnectedCmdlet.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\PnPConnectedCmdlet.cs:line 79
                      at PnP.PowerShell.Commands.PnPSharePointCmdlet.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\PnPSharePointCmdlet.cs:line 112  
                      at System.Management.Automation.CommandProcessor.ProcessRecord()
ScriptLineNumber : 12

What is the version of the Cmdlet module you are running?

1.11.74

Which operating system/environment are you running PnP PowerShell on?

  • [x] Windows
  • [ ] Linux
  • [ ] MacOS
  • [ ] Azure Cloud Shell
  • [ ] Azure Functions
  • [ ] Other : please specify

Studermarc avatar Sep 14 '22 13:09 Studermarc

I'm having this same issue with Get-PnpFolder -Url $folderName -ErrorAction SilentlyContinue

It throws a "File Not Found" error instead of continuing.

denicomp avatar Sep 19 '22 02:09 denicomp

I am having the same issue with Set-PnPListItemPermission cmdlet when the user in AAD is disabled (or recently enabled). It throws "The specified user cannot be found"

jeremyhagan avatar Sep 26 '22 23:09 jeremyhagan

Any ETA on a fix for this, or at least revert the change that broke it? I'm still chasing down this in my scripts, having to replace them with try/catch.

denicomp avatar Oct 02 '22 21:10 denicomp

wrapping the cmdltes in a try/catch block is the only workaround for now. Reverting the changes doesn't look like a good option, as the errorhandling is in general a lot better/consistent now with cmdltes throwing an exception when an error occurs (instead of only an error message without an exception) I guess we have to be patient until someone can take a look at this issue and fix it.

Studermarc avatar Oct 10 '22 10:10 Studermarc

watch out for the try/catch workaround as you need to wrap every single cmdlet in the script (that you want to continue execution on error) with try/catch (or even nest try/catch in other try/catch, blaahhh). Otherwise if any cmlet in a try/catch block produces error, it will result in remaining cmdlets in that block to be ignored.

#Example:

try{ get-goodcmd get-badcmd get-anothergoodcmd } catch {echo $error}

try{ get-yetanothergoodcmd } catch {echo $error}

#In above example only get-goodcmd and then get-yetanothergoodcmd cmdlets will get executed.

I've started using github actions with powershell just recently, but in my opinion github actions error handling for powershell should definitely respect overriding erroractions like "silentlycontinue" for cmdlets !!

mczechowski-sfdc avatar Oct 11 '22 14:10 mczechowski-sfdc

Yes, I wrap each one individually.

I had to replace this, which used to work:

       $myDocsFolder = Get-PnpFolder -Url $myDocsFolderName -ErrorAction SilentlyContinue
	if ($null -eq $myDocsFolder) {

with:

	try {
		$myDocsFolder = Get-PnpFolder -Url $myDocsFolderName -ErrorAction SilentlyContinue
	}
	catch {
		$myDocsFolder = $null
	}
	if ($null -eq $myDocsFolder) {

I left the -ErrorAction there so when it is fixed at some point, I would know where to go back and remove the try/catch if I wanted to. Not a lot to be gained other then more succinct.

jdenicola-pa avatar Oct 11 '22 16:10 jdenicola-pa