microsoft / MSCloudLoginAssistant

PowerShell Module Handling Authentication to Cloud Services
https://www.powershellgallery.com/packages/MSCloudLoginAssistant/
MIT License
76 stars 50 forks source link

The Catch block in Connect-MSCloudLoginPnP isn't handling all exceptions #103

Closed ykuijs closed 3 years ago

ykuijs commented 3 years ago

I was testing an issue with Microsoft365Dsc and noticed that Test-MSCloudLogin wasn't connecting to the correct SharePoint site. After some troubleshooting, I noticed that the Connect-MSCloudLoginPnP function isn't handling all exceptions. The login has a few if/elseif statements, but if the thrown exception isn't any of the specified exceptions, the code just ignores it.

The issue I had was that my account didn't have the proper permissions on the site collection, which caused the function not to connect to it but didn't throw an error or display a message. Debugging showed that the following exception was thrown, but the catch block didn't do anything with it:

Connect-PnPOnline : The remote server returned an error: (403) Forbidden.
At C:\Program Files\WindowsPowerShell\Modules\MSCloudLoginAssistant\1.0.48\Workloads\PnP.psm1:120 char:17
+                 Connect-PnPOnline -Url $Global:SPOConnectionUrl `
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Connect-PnPOnline], WebException
    + FullyQualifiedErrorId : System.Net.WebException,PnP.PowerShell.Commands.Base.ConnectOnline

Would it be an idea to add an Else statement that handles all exceptions that aren't handled earlier in the code?

Involved code: https://github.com/microsoft/MSCloudLoginAssistant/blob/c0cd2c4cac10e1081193946b28973e2c09237691/Modules/MSCloudLoginAssistant/Workloads/PnP.psm1#L127-L198