refactor: Migrate from PnP to Microsoft Graph authentication
This commit is contained in:
parent
87da4e1185
commit
df82840f99
29
GraphConfig.ps1
Normal file
29
GraphConfig.ps1
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,9 @@
|
|||||||
Add-Type -AssemblyName System.Windows.Forms
|
Add-Type -AssemblyName System.Windows.Forms
|
||||||
Import-Module PnP.PowerShell
|
Import-Module Microsoft.Graph.Sites
|
||||||
|
Import-Module Microsoft.Graph.Files
|
||||||
|
|
||||||
# Import our components
|
# Import our components
|
||||||
|
. "$PSScriptRoot\GraphConfig.ps1"
|
||||||
. "$PSScriptRoot\SharePointFunctions.ps1"
|
. "$PSScriptRoot\SharePointFunctions.ps1"
|
||||||
. "$PSScriptRoot\EventHandlers.ps1"
|
. "$PSScriptRoot\EventHandlers.ps1"
|
||||||
|
|
||||||
|
|||||||
@ -32,51 +32,7 @@ function Save-Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Connect-SharePoint {
|
function Connect-SharePoint {
|
||||||
try {
|
return Connect-Graph
|
||||||
$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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function List-XlsFiles {
|
function List-XlsFiles {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user