add: new method of verifying spo tenant for Connect-SPOService branch

This commit is contained in:
DrIOS
2025-01-13 15:35:07 -06:00
parent c341279531
commit f409e8a5f1
2 changed files with 61 additions and 39 deletions

View File

@@ -7,6 +7,7 @@ The format is based on and uses the types of changes according to [Keep a Change
### Added ### Added
- Added additional error handling to connect function to identify problematic steps when they occur. - Added additional error handling to connect function to identify problematic steps when they occur.
- Added new method of verifying spo tenant for Connect-SPOService branch of connect function.
## [0.1.26] - 2024-08-04 ## [0.1.26] - 2024-08-04

View File

@@ -15,109 +15,130 @@ function Connect-M365Suite {
[switch]$SkipConfirmation [switch]$SkipConfirmation
) )
$VerbosePreference = if ($SkipConfirmation) { "SilentlyContinue" } else { "Continue" } $VerbosePreference = if ($SkipConfirmation) { 'SilentlyContinue' } else { 'Continue' }
$tenantInfo = @() $tenantInfo = @()
$connectedServices = @() $connectedServices = @()
try { try {
if ($RequiredConnections -contains "Microsoft Graph" -or $RequiredConnections -contains "EXO | Microsoft Graph") { if ($RequiredConnections -contains 'Microsoft Graph' -or $RequiredConnections -contains 'EXO | Microsoft Graph') {
try { try {
Write-Verbose "Connecting to Microsoft Graph..." Write-Verbose 'Connecting to Microsoft Graph...'
if ($AuthParams) { if ($AuthParams) {
Connect-MgGraph -CertificateThumbprint $AuthParams.ClientCertThumbPrint -AppId $AuthParams.ClientId -TenantId $AuthParams.TenantId -NoWelcome | Out-Null Connect-MgGraph -CertificateThumbprint $AuthParams.ClientCertThumbPrint -AppId $AuthParams.ClientId -TenantId $AuthParams.TenantId -NoWelcome | Out-Null
} else { }
Connect-MgGraph -Scopes "Directory.Read.All", "Domain.Read.All", "Policy.Read.All", "Organization.Read.All" -NoWelcome | Out-Null else {
Connect-MgGraph -Scopes 'Directory.Read.All', 'Domain.Read.All', 'Policy.Read.All', 'Organization.Read.All' -NoWelcome | Out-Null
} }
$graphOrgDetails = Get-MgOrganization $graphOrgDetails = Get-MgOrganization
$tenantInfo += [PSCustomObject]@{ $tenantInfo += [PSCustomObject]@{
Service = "Microsoft Graph" Service = 'Microsoft Graph'
TenantName = $graphOrgDetails.DisplayName TenantName = $graphOrgDetails.DisplayName
TenantID = $graphOrgDetails.Id TenantID = $graphOrgDetails.Id
} }
$connectedServices += "Microsoft Graph" $connectedServices += 'Microsoft Graph'
Write-Verbose "Successfully connected to Microsoft Graph." Write-Verbose 'Successfully connected to Microsoft Graph.'
} catch { }
catch {
throw "Failed to connect to Microsoft Graph: $($_.Exception.Message)" throw "Failed to connect to Microsoft Graph: $($_.Exception.Message)"
} }
} }
if ($RequiredConnections -contains "EXO" -or $RequiredConnections -contains "AzureAD | EXO" -or $RequiredConnections -contains "Microsoft Teams | EXO" -or $RequiredConnections -contains "EXO | Microsoft Graph") { if ($RequiredConnections -contains 'EXO' -or $RequiredConnections -contains 'AzureAD | EXO' -or $RequiredConnections -contains 'Microsoft Teams | EXO' -or $RequiredConnections -contains 'EXO | Microsoft Graph') {
try { try {
Write-Verbose "Connecting to Exchange Online..." Write-Verbose 'Connecting to Exchange Online...'
if ($AuthParams) { if ($AuthParams) {
Connect-ExchangeOnline -AppId $AuthParams.ClientId -CertificateThumbprint $AuthParams.ClientCertThumbPrint -Organization $AuthParams.OnMicrosoftUrl -ShowBanner:$false | Out-Null Connect-ExchangeOnline -AppId $AuthParams.ClientId -CertificateThumbprint $AuthParams.ClientCertThumbPrint -Organization $AuthParams.OnMicrosoftUrl -ShowBanner:$false | Out-Null
} else { }
else {
Connect-ExchangeOnline -ShowBanner:$false | Out-Null Connect-ExchangeOnline -ShowBanner:$false | Out-Null
} }
$exoTenant = (Get-OrganizationConfig).Identity $exoTenant = (Get-OrganizationConfig).Identity
$tenantInfo += [PSCustomObject]@{ $tenantInfo += [PSCustomObject]@{
Service = "Exchange Online" Service = 'Exchange Online'
TenantName = $exoTenant TenantName = $exoTenant
TenantID = "N/A" TenantID = 'N/A'
} }
$connectedServices += "EXO" $connectedServices += 'EXO'
Write-Verbose "Successfully connected to Exchange Online." Write-Verbose 'Successfully connected to Exchange Online.'
} catch { }
catch {
throw "Failed to connect to Exchange Online: $($_.Exception.Message)" throw "Failed to connect to Exchange Online: $($_.Exception.Message)"
} }
} }
if ($RequiredConnections -contains "SPO") { if ($RequiredConnections -contains 'SPO') {
try { try {
Write-Verbose "Connecting to SharePoint Online..." Write-Verbose 'Connecting to SharePoint Online...'
if ($AuthParams) { if ($AuthParams) {
Connect-PnPOnline -Url $AuthParams.SpAdminUrl -ClientId $AuthParams.ClientId -Tenant $AuthParams.OnMicrosoftUrl -Thumbprint $AuthParams.ClientCertThumbPrint | Out-Null Connect-PnPOnline -Url $AuthParams.SpAdminUrl -ClientId $AuthParams.ClientId -Tenant $AuthParams.OnMicrosoftUrl -Thumbprint $AuthParams.ClientCertThumbPrint | Out-Null
} else { }
else {
Connect-SPOService -Url $TenantAdminUrl | Out-Null Connect-SPOService -Url $TenantAdminUrl | Out-Null
} }
$tenantName = if ($AuthParams) { (Get-PnPSite).Url } else { (Get-SPOCrossTenantHostUrl).Host } $tenantName = if ($AuthParams) {
(Get-PnPSite).Url
}
else {
$sites = Get-SPOSite
# Get the URL from the first site collection
$url = $sites[0].Url
# Use regex to extract the base URL up to the .com portion
$baseUrl = [regex]::Match($url, 'https://[^/]+.com').Value
# Output the base URL
$baseUrl
}
$tenantInfo += [PSCustomObject]@{ $tenantInfo += [PSCustomObject]@{
Service = "SharePoint Online" Service = 'SharePoint Online'
TenantName = $tenantName TenantName = $tenantName
} }
$connectedServices += "SPO" $connectedServices += 'SPO'
Write-Verbose "Successfully connected to SharePoint Online." Write-Verbose 'Successfully connected to SharePoint Online.'
} catch { }
catch {
throw "Failed to connect to SharePoint Online: $($_.Exception.Message)" throw "Failed to connect to SharePoint Online: $($_.Exception.Message)"
} }
} }
if ($RequiredConnections -contains "Microsoft Teams" -or $RequiredConnections -contains "Microsoft Teams | EXO") { if ($RequiredConnections -contains 'Microsoft Teams' -or $RequiredConnections -contains 'Microsoft Teams | EXO') {
try { try {
Write-Verbose "Connecting to Microsoft Teams..." Write-Verbose 'Connecting to Microsoft Teams...'
if ($AuthParams) { if ($AuthParams) {
Connect-MicrosoftTeams -TenantId $AuthParams.TenantId -CertificateThumbprint $AuthParams.ClientCertThumbPrint -ApplicationId $AuthParams.ClientId | Out-Null Connect-MicrosoftTeams -TenantId $AuthParams.TenantId -CertificateThumbprint $AuthParams.ClientCertThumbPrint -ApplicationId $AuthParams.ClientId | Out-Null
} else { }
else {
Connect-MicrosoftTeams | Out-Null Connect-MicrosoftTeams | Out-Null
} }
$teamsTenantDetails = Get-CsTenant $teamsTenantDetails = Get-CsTenant
$tenantInfo += [PSCustomObject]@{ $tenantInfo += [PSCustomObject]@{
Service = "Microsoft Teams" Service = 'Microsoft Teams'
TenantName = $teamsTenantDetails.DisplayName TenantName = $teamsTenantDetails.DisplayName
TenantID = $teamsTenantDetails.TenantId TenantID = $teamsTenantDetails.TenantId
} }
$connectedServices += "Microsoft Teams" $connectedServices += 'Microsoft Teams'
Write-Verbose "Successfully connected to Microsoft Teams." Write-Verbose 'Successfully connected to Microsoft Teams.'
} catch { }
catch {
throw "Failed to connect to Microsoft Teams: $($_.Exception.Message)" throw "Failed to connect to Microsoft Teams: $($_.Exception.Message)"
} }
} }
if (-not $SkipConfirmation) { if (-not $SkipConfirmation) {
Write-Verbose "Connected to the following tenants:" Write-Verbose 'Connected to the following tenants:'
foreach ($tenant in $tenantInfo) { foreach ($tenant in $tenantInfo) {
Write-Verbose "Service: $($tenant.Service) | Tenant: $($tenant.TenantName)" Write-Verbose "Service: $($tenant.Service) | Tenant: $($tenant.TenantName)"
} }
$confirmation = Read-Host "Do you want to proceed with these connections? (Y/N)" $confirmation = Read-Host 'Do you want to proceed with these connections? (Y/N)'
if ($confirmation -notlike 'Y') { if ($confirmation -notlike 'Y') {
Disconnect-M365Suite -RequiredConnections $connectedServices Disconnect-M365Suite -RequiredConnections $connectedServices
throw "User aborted connection setup." throw 'User aborted connection setup.'
} }
} }
} catch { }
$VerbosePreference = "Continue" catch {
$VerbosePreference = 'Continue'
throw "Connection failed: $($_.Exception.Message)" throw "Connection failed: $($_.Exception.Message)"
} finally { }
$VerbosePreference = "Continue" finally {
$VerbosePreference = 'Continue'
} }
} }