URL: https://download.microsoft.com/download/7/3/0/730915ab-f8e5-4cdc-9600-2b007731cb9a/mst-ca-provisioning.ps1
# -----------------------------------------------------------------------
# Copyright © Microsoft Corporation. All rights reserved.
#
# mst-CA-readiness - Provisions a service principal for Microsoft Tunnel
# -----------------------------------------------------------------------
param (
[parameter(Mandatory=$false)]
[String]$MgGEnvironment
)
# Check if Microsoft Graph module is already installed
if (-not (Get-Module -ListAvailable -Name Microsoft.Graph)) {
Write-Host "Microsoft.Graph module not found. Installing..." -ForegroundColor Yellow
try {
Install-Module Microsoft.Graph -Scope CurrentUser -Repository PSGallery -Force
Write-Host "Microsoft.Graph module installed successfully" -ForegroundColor Green
}
catch {
Write-Error "Failed to install Microsoft.Graph module"
echo $_.Exception.GetType().FullName, $_.Exception.Message
exit 1
}
}
else {
Write-Host "Microsoft.Graph module already installed" -ForegroundColor Green
}
#Disconnect from any previous sessions and force login
Disconnect-MgGraph -ErrorAction SilentlyContinue
if ($MgGEnvironment -ieq "onedf" -or $MgGEnvironment -ieq "df" -or $MgGEnvironment -ieq "internal") {
try {
Connect-MgGraph -Environment Global -ContextScope Process
} catch [Exception] {
Write-Error "Error occured connecting to Mg-Graph"
echo $_.Exception.GetType().FullName, $_.Exception.Message
Write-Host "Failed to provision the Service Principal" -ForegroundColor Red
exit 1
}
} elseif ($MgGEnvironment -ieq "germany" -or $MgGEnvironment -ieq "blackforest" ) {
try {
Connect-MgGraph -Environment Germany -ContextScope Process
} catch [Exception] {
Write-Error "Error occured connecting to Mg-Graph"
echo $_.Exception.GetType().FullName, $_.Exception.Message
Write-Host "Failed to provision the Service Principal" -ForegroundColor Red
exit 1
}
} elseif ($MgGEnvironment -ieq "government" -or $MgGEnvironment -ieq "USGov" -or $MgGEnvironment -ieq "fairfax") {
try {
Connect-MgGraph -Environment USGov -ContextScope Process
} catch [Exception] {
Write-Error "Error occured connecting to Mg-Graph"
echo $_.Exception.GetType().FullName, $_.Exception.Message
Write-Host "Failed to provision the Service Principal" -ForegroundColor Red
exit 1
}
} elseif ($MgGEnvironment -ieq "USGovDoD") {
try {
Connect-MgGraph -Environment USGovDoD -ContextScope Process
} catch [Exception] {
Write-Error "Error occured connecting to Mg-Graph"
echo $_.Exception.GetType().FullName, $_.Exception.Message
Write-Host "Failed to provision the Service Principal" -ForegroundColor Red
exit 1
}
} elseif ($MgGEnvironment -ieq "China" -or $MgGEnvironment -ieq "mooncake" ) {
try {
Connect-MgGraph -Environment China -ContextScope Process
} catch [Exception] {
Write-Error "Error occured connecting to Mg-Graph"
echo $_.Exception.GetType().FullName, $_.Exception.Message
Write-Host "Failed to provision the Service Principal" -ForegroundColor Red
exit 1
}
} else {
try {
Connect-MgGraph -Environment Global -ContextScope Process
} catch [Exception] {
Write-Error "Error occured connecting to Mg Graph"
echo $_.Exception.GetType().FullName, $_.Exception.Message
Write-Host "Failed to provision the Service Principal" -ForegroundColor Red
exit 1
}
}
try {
$appId = '3678c9e9-9681-447a-974d-d19f668fcd88'
New-MgServicePrincipal -AppId $appId | Out-Null
$result = Get-MgServicePrincipal -Filter "AppId eq '$appId'"
$result | Format-Table DisplayName, Id, AppId, ServicePrincipalType -AutoSize
$displayName = $result.AppDisplayName
Write-Host "Successfully provisioned the Service Principal for $displayName" -ForegroundColor Green
} catch [Exception] {
Write-Error "Error provisioning Service Principal"
echo $_.Exception.GetType().FullName, $_.Exception.Message
Write-Host "Failed to provision the Service Principal" -ForegroundColor Red
exit 1
}