Intermittent "403 - Forbidden" while using Connect-PNPOnline on automated solution

Copper Contributor

Hello,

 

I am running a script at a company using PNP Powershell.

What the code does through a Powershell GUI, is cycle through a previously exported CSV file which contains: SiteTitle, PercentUsed, Used MB, QuotaMB, URL - around 70-80-100 entries for a run .

 

Now what the script actually does, it cycles through the sites with a Foreach, and connects to each site URL and get's the Owners from the Owners group so I can send them an email template.

 

Each and every site has Site admins: Sharepoint Service administrator and Company administrator.

I can access each site via URL from browser, as I am Sharepoint Admin .

 

Intermittently, and random, upon running the scripts I receive the following error for multiple sites:

Get-PnpWeb : The remote server returned an error: (403) Forbidden.
At C:\guips\gui2.ps1:3204 char:23
+ $web=Get-PnpWeb
+ ~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Get-PnPWeb], WebException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.GetWeb

 

I say intermittently, because on my latest run, it dropped the error for 40/80 sites. Checked the sites, I can access them by browser, all have required admins defined. Additionally, if I run Connect-PnpOnline to these sites, and then Get-PNPContext, these two commands run. Get-PNPweb returns the same error. ( this way I ruled somewhat out that it is not a GUI issue ) .

 

Now the funny thing is, this scripts runs weekly, and sometimes the same sites appear, and the script ran perfect let's say, last week, and two weeks before, but throws this error now ( nothing changed in the mean time ) .

 

The code is use is below:

if($ifok1 -eq 1)
       {
         $sposites = Import-Csv -Path $Global:FilePath
         ForEach($sposite in $sposites)
         {
            	 $totalprocessed+=1
                 Start-Sleep -Second 1;
                 $spositeURL=$sposite.URL
                 #$spositeURL=$spositeURL -replace " ",""
	   	 $spositetitle=$sposite.SiteTitle
                 
                 if($spositeURL.Contains('msteams_')) 
                 {
                 $teamscount+=1 
                 continue }
                 

                 $result.text += "`r`nSite: " + $spositeURL 
                 Connect-PnPonline -Url $spositeURL -UseWebLogin
                 $web=Get-PnpWeb
                 if($?)
                 { 
                 $OwnersGroup = Get-PnPGroup  -AssociatedOwnerGroup
                 $owners =  get-pnpgroupmembers -identity $ownersgroup.title
                 $OwnersTO=@()
                 ForEach ($owner in $owners)
                        {
                         $owneremail=$owner.Email
                         $OwnersTO+=$owner.Email +";"
                        }
           	 $result.text += "`r`nOwners are: " + $OwnersTO
################DISCONNECT PNP ONLINE ###########################
                 Disconnect-PnpOnline
                 Start-Sleep -Second 3;
                 $testdisconnect=Get-PNPConnection
                 if($?)
                 { Disconnect-PnpOnline }

                 
                 

                 Start-Sleep -Second 1;
#################START OF SCRIPT THAT SENDS EMAIL TO OWNERS OF SITES#######################################################

 

Any help solving this issue would be much appreciated.

Thank you !

 

0 Replies