diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 index e9d258c..7d66ff9 100644 --- a/SharePointFunctions.ps1 +++ b/SharePointFunctions.ps1 @@ -47,18 +47,34 @@ function Connect-SharePoint { # Ignore disconnection errors } - # Use Azure Management authentication - Connect-PnPOnline -Url $siteUrl -UseWebLogin ` + # Use web login with additional parameters for stability + Connect-PnPOnline -Url $siteUrl ` + -UseWebLogin ` -ErrorAction Stop ` - -WarningAction SilentlyContinue + -WarningAction SilentlyContinue ` + -RetryCount 3 ` + -RetryWait 3 ` + -Timeout 300 + + Start-Sleep -Seconds 2 # Give the web login time to complete - # Verify connection - $web = Get-PnPWeb -ErrorAction Stop - if (-not $web) { - throw "Failed to connect to SharePoint site" + # Verify connection with timeout + $timeoutSeconds = 30 + $startTime = Get-Date + while ((Get-Date) -lt $startTime.AddSeconds($timeoutSeconds)) { + try { + $web = Get-PnPWeb -ErrorAction Stop + if ($web) { + return $true + } + } + catch { + Start-Sleep -Seconds 2 + continue + } } - return $true + throw "Timeout waiting for SharePoint connection" } catch { $script:txtStatus.Text += "Connection Error: $($_.Exception.Message)`n"