Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issues #137 and #138 #139

Merged
merged 3 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Modules/MSCloudLoginAssistant/MSCloudLoginAssistant.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,49 @@ function Connect-M365Tenant
}
}

<#
Ref https://learn.microsoft.com/en-us/sharepoint/change-your-sharepoint-domain-name
Customers that have changed their SharePoint domain name will have a redirector in place.
If so, the AdminURL calculated by MsCloudLoginAssistant will not be the AdminUrl, but a redirector.
Get-PnpTenant, used by all SharePoint Online and OneDrive resources, will fail if connected to the
redirector. The following code tests the connected URL, and if the target is a redirect-site, it
resolves the Url it redirects to. To not affect anything else, the Invoke-WebRequest is run with
-ErrorAction Ignore, so it will have effect only if the StatusCode is 308.
#>
$contextUrl = (Get-PnPContext).Url
try
{
$redirectSite = $null
$redirectSite = Invoke-WebRequest -Uri $contextUrl -UseBasicParsing -MaximumRedirection 0 -ErrorAction Ignore
#The $redirectSite.StatusCode is 308 if the url we are connected to is a redirector. If it is not, continue as normal.
if ($redirectSite.StatusCode -eq 308)
{
Write-Verbose -Message "The url '$contextUrl' is redirected - the target seems to be '$($redirectSite.Headers.Location)'"
$Global:MSCloudLoginConnectionProfile.PnP.ConnectionUrl = $redirectSite.Headers.Location
if (-not $Url) {
Write-Verbose -Message "The param 'Url' is unspecified - assuming the redirected site we're connecting to is the AdminUrl"
$Global:MSCloudLoginConnectionProfile.PnP.AdminUrl = $Global:MSCloudLoginConnectionProfile.PnP.ConnectionUrl
}
else {
Write-Verbose -Message "The param 'Url' was specified, assuming the redirected site we're connecting to is not the AdminUrl"
}
#Force the reconnection
$ForceRefresh = $true
$Global:MSCloudLoginConnectionProfile.PnP.Connected = $false
Write-Verbose -Message "Reconnecting to the Url '$($Global:MSCloudLoginConnectionProfile.PnP.ConnectionUrl)'"
$Global:MSCloudLoginConnectionProfile.PnP.Connect($ForceRefresh)
}
}
catch
{
Write-Verbose $_
}
finally
{
$redirectSite = $null
$contextUrl = $null
}

# If the AdminUrl is empty and a URL was provided, assume that the url
# provided is the admin center;
if (-not $Global:MSCloudLoginConnectionProfile.PnP.AdminUrl -and $Url)
Expand Down
2 changes: 1 addition & 1 deletion Modules/MSCloudLoginAssistant/Workloads/PnP.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function Connect-MSCloudLoginPnP
{
if ($Global:MSCloudLoginConnectionProfile.PnP.AuthenticationType -eq 'ServicePrincipalWithThumbprint')
{
if (-not $Url)
if ($Url)
{
Write-Information -Message 'Connecting with Service Principal - Thumbprint'
Write-Information -Message "URL: $Url"
Expand Down