diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 index 27be4eb..93d1408 100644 --- a/SharePointFunctions.ps1 +++ b/SharePointFunctions.ps1 @@ -112,12 +112,24 @@ function Get-FolderBrowser { try { # Get site ID first $siteUrl = $script:txtSiteUrl.Text.TrimEnd('/') - $site = Get-MgSite -Search $siteUrl | Where-Object { $_.WebUrl -eq $siteUrl } + # Extract site path from URL + $sitePathMatch = $siteUrl -match "sharepoint\.com(.*)" + if (-not $sitePathMatch) { + throw "Invalid SharePoint URL format" + } + $sitePath = $matches[1] + + # Get site directly using path + $site = Get-MgSite -SiteId "root" -Property "siteCollection,webUrl,id" -ErrorAction Stop + $site = Get-MgSite -SiteId $site.Id -Property "siteCollection,webUrl,id" -ErrorAction Stop ` + -Filter "startswith(webUrl, '$siteUrl')" if (-not $site) { - throw "Site not found" + throw "Site not found at $siteUrl" } + $script:txtStatus.Text += "Found site: $($site.WebUrl)`n" + # Get drive (document library) $drives = Get-MgSiteDrive -SiteId $site.Id # Handle "Shared Documents" special case