diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 index 01930cf..2092225 100644 --- a/SharePointFunctions.ps1 +++ b/SharePointFunctions.ps1 @@ -38,11 +38,21 @@ function Connect-SharePoint { if (-not $siteUrl) { throw "Please enter a Site URL" } + + # Disconnect any existing connection first + Disconnect-PnPOnline -ErrorAction SilentlyContinue - # Simplified interactive auth - Connect-PnPOnline -Url $siteUrl -Interactive ` + # Use modern authentication with device code flow + Connect-PnPOnline -Url $siteUrl -Interactive -ForceAuthentication ` + -ErrorAction Stop ` -WarningAction SilentlyContinue + # Verify connection + $web = Get-PnPWeb -ErrorAction Stop + if (-not $web) { + throw "Failed to connect to SharePoint site" + } + return $true } catch {