diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 index 93d1408..faec17c 100644 --- a/SharePointFunctions.ps1 +++ b/SharePointFunctions.ps1 @@ -112,17 +112,17 @@ function Get-FolderBrowser { try { # Get site ID first $siteUrl = $script:txtSiteUrl.Text.TrimEnd('/') - # Extract site path from URL - $sitePathMatch = $siteUrl -match "sharepoint\.com(.*)" - if (-not $sitePathMatch) { + + # Extract hostname and site path from URL + if ($siteUrl -match "https://([^/]+)(/.*)") { + $hostname = $matches[1] + $sitePath = $matches[2] + } else { 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')" + + # Get the site using hostname and path + $site = Get-MgSite -Filter "siteCollection/hostname eq '$hostname' and startsWith(webUrl, '$siteUrl')" -ErrorAction Stop if (-not $site) { throw "Site not found at $siteUrl"