refactor: Migrate from PnP to Microsoft Graph authentication

This commit is contained in:
Bobby Abellana (aider) 2025-02-19 15:34:44 -08:00
parent 87da4e1185
commit df82840f99
3 changed files with 33 additions and 46 deletions

29
GraphConfig.ps1 Normal file
View File

@ -0,0 +1,29 @@
# App registration details
$script:graphConfig = @{
ClientId = "YOUR_CLIENT_ID" # Replace with your app registration client ID
TenantId = "YOUR_TENANT_ID" # Replace with your tenant ID
Scopes = @(
"Files.ReadWrite.All"
"Sites.ReadWrite.All"
)
}
function Connect-Graph {
try {
Connect-MgGraph -ClientId $graphConfig.ClientId `
-TenantId $graphConfig.TenantId `
-Scopes $graphConfig.Scopes `
-UseDeviceCode
# Verify connection
$profile = Get-MgProfile
if ($profile) {
return $true
}
return $false
}
catch {
$script:txtStatus.Text += "Graph Connection Error: $($_.Exception.Message)`n"
return $false
}
}

View File

@ -1,7 +1,9 @@
Add-Type -AssemblyName System.Windows.Forms
Import-Module PnP.PowerShell
Import-Module Microsoft.Graph.Sites
Import-Module Microsoft.Graph.Files
# Import our components
. "$PSScriptRoot\GraphConfig.ps1"
. "$PSScriptRoot\SharePointFunctions.ps1"
. "$PSScriptRoot\EventHandlers.ps1"

View File

@ -32,51 +32,7 @@ function Save-Config {
}
function Connect-SharePoint {
try {
$siteUrl = $script:txtSiteUrl.Text
if (-not $siteUrl) {
throw "Please enter a Site URL"
}
# Try to disconnect, but ignore any errors
try {
Disconnect-PnPOnline
}
catch {
# Ignore disconnection errors
}
# Use web login with simplified parameters
Connect-PnPOnline -Url $siteUrl `
-UseWebLogin `
-ErrorAction Stop `
-WarningAction SilentlyContinue
Start-Sleep -Seconds 2 # Give the web login time to complete
# 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
}
}
throw "Timeout waiting for SharePoint connection"
}
catch {
$script:txtStatus.Text += "Connection Error: $($_.Exception.Message)`n"
return $false
}
return Connect-Graph
}
function List-XlsFiles {