pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
990 stars 663 forks source link

Connect-PnPOnline gives same exception if site does not exist or if user does not have access #2518

Open spoman007 opened 4 years ago

spoman007 commented 4 years ago

When I am using below line Connect-PnPOnline -Url https://contoso.sharepoint.com -Credentials (Get-Credential)

I am getting exception as Connect-PnPOnline : The remote server returned an error: (401) Unauthorized.

I get this exception in following cases

If the user does not have access to site If the site does not exists I need to check if site exists, I was expecting a 404 if it does not exists. Is there a way in PNP to check if site does exists

Which version of the PnP-PowerShell Cmdlets are you using?

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

3.17.2001.2

How did you install the PnP-PowerShell Cmdlets?

https://github.com/MicrosoftDocs/office-docs-powershell/issues/4889

ghost commented 4 years ago

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

spoman007 commented 4 years ago

I am using something like this in the mean time. But need to know if there is a standard way.

carbon

jackpoz commented 4 years ago

You could check the way PnP-Sites-Core implemented CheckIfSiteExists() in C# at https://github.com/SharePoint/PnP-Sites-Core/blob/4519e6d7f5ac1dd5131fca840e70ffa10351f749/Core/OfficeDevPnP.Core/Extensions/TenantExtensions.cs#L224-L285

If you run your code with Application ID permissions with access to the whole SharePoint tenant then you could avoid any access denied permission.

spoman007 commented 4 years ago

@jackpoz We want users without tenant level permissions to check if the site exists.

abhitalks commented 4 years ago

I am using something like this in the mean time. But need to know if there is a standard way.

Thanks @spoman007, using your snippet to check if site exists works. Can I use it in the interim?

Somehow, I can't even catch the exception thrown by "Connect-PnPOnline", it doesn't go into the catch construct and simply exits to the console. Sometimes, it throws 401 "unauthorized", sometimes "forbidden", and sometimes even silently continues only to fail at some later point at another command. Very unpredictable.