diff --git a/GraphConfig.ps1 b/GraphConfig.ps1 new file mode 100644 index 0000000..0b70aa7 --- /dev/null +++ b/GraphConfig.ps1 @@ -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 + } +} diff --git a/MainForm.ps1 b/MainForm.ps1 index 7f11b6e..2bea905 100644 --- a/MainForm.ps1 +++ b/MainForm.ps1 @@ -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" diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 index 2fe4885..336a7de 100644 --- a/SharePointFunctions.ps1 +++ b/SharePointFunctions.ps1 @@ -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 {