Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 of 6 tasks
Studermarc opened this issue Sep 14, 2022 · 6 comments · Fixed by #2510
Closed
1 of 6 tasks
Labels
bug Something isn't working

Comments

@Studermarc
Copy link
Contributor

Studermarc commented Sep 14, 2022

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?

  • Windows
  • Linux
  • MacOS
  • Azure Cloud Shell
  • Azure Functions
  • Other : please specify
@Studermarc Studermarc added the bug Something isn't working label Sep 14, 2022
@denicomp
Copy link

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

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

@jeremyhagan
Copy link

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"

@denicomp
Copy link

denicomp commented Oct 2, 2022

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.

@Studermarc
Copy link
Contributor Author

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.

@mczechowski-sfdc
Copy link

mczechowski-sfdc commented Oct 11, 2022

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 !!

@jdenicola-pa
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants