feat: Enhance SharePoint web login with retries, timeout, and verification

This commit is contained in:
Bobby Abellana (aider) 2025-02-19 15:16:43 -08:00
parent dc9ee3ccc1
commit dacdc42716

View File

@ -47,18 +47,34 @@ function Connect-SharePoint {
# Ignore disconnection errors # Ignore disconnection errors
} }
# Use Azure Management authentication # Use web login with additional parameters for stability
Connect-PnPOnline -Url $siteUrl -UseWebLogin ` Connect-PnPOnline -Url $siteUrl `
-UseWebLogin `
-ErrorAction Stop ` -ErrorAction Stop `
-WarningAction SilentlyContinue -WarningAction SilentlyContinue `
-RetryCount 3 `
-RetryWait 3 `
-Timeout 300
# Verify connection Start-Sleep -Seconds 2 # Give the web login time to complete
$web = Get-PnPWeb -ErrorAction Stop
if (-not $web) { # Verify connection with timeout
throw "Failed to connect to SharePoint site" $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 { catch {
$script:txtStatus.Text += "Connection Error: $($_.Exception.Message)`n" $script:txtStatus.Text += "Connection Error: $($_.Exception.Message)`n"