Skip to content

Commit fc216cf

Browse files
authored
Updated PowerShell scripts to be compatible with PowerShell 7 (#691)
1 parent 8b52d84 commit fc216cf

File tree

3 files changed

+278
-126
lines changed

3 files changed

+278
-126
lines changed
Lines changed: 120 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,152 @@
1+
#Requires -Version 7
2+
13
[CmdletBinding()]
24
param(
3-
[PSCredential] $Credential,
45
[Parameter(Mandatory=$False, HelpMessage='Tenant ID (This is a GUID which represents the "Directory ID" of the AzureAD tenant into which you want to create the apps')]
5-
[string] $tenantId
6+
[string] $tenantId,
7+
[Parameter(Mandatory=$False, HelpMessage='Azure environment to use while running the script. Default = Global')]
8+
[string] $azureEnvironmentName
69
)
710

8-
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
9-
Install-Module "AzureAD" -Scope CurrentUser
10-
}
11-
Import-Module AzureAD
12-
$ErrorActionPreference = "Stop"
1311

1412
Function Cleanup
1513
{
16-
<#
17-
.Description
18-
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script
19-
#>
14+
if (!$azureEnvironmentName)
15+
{
16+
$azureEnvironmentName = "Global"
17+
}
18+
19+
<#
20+
.Description
21+
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script
22+
#>
2023

2124
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
2225
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
2326

24-
# Login to Azure PowerShell (interactive if credentials are not already provided:
25-
# you'll need to sign-in with creds enabling your to create apps in the tenant)
26-
if (!$Credential -and $TenantId)
27+
# Connect to the Microsoft Graph API
28+
Write-Host "Connecting to Microsoft Graph"
29+
30+
31+
if ($tenantId -eq "")
2732
{
28-
$creds = Connect-AzureAD -TenantId $tenantId
33+
Connect-MgGraph -Scopes "User.Read.All Organization.Read.All Application.ReadWrite.All" -Environment $azureEnvironmentName
2934
}
30-
else
35+
else
3136
{
32-
if (!$TenantId)
33-
{
34-
$creds = Connect-AzureAD -Credential $Credential
35-
}
36-
else
37-
{
38-
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential
39-
}
37+
Connect-MgGraph -TenantId $tenantId -Scopes "User.Read.All Organization.Read.All Application.ReadWrite.All" -Environment $azureEnvironmentName
4038
}
39+
40+
$context = Get-MgContext
41+
$tenantId = $context.TenantId
4142

42-
if (!$tenantId)
43-
{
44-
$tenantId = $creds.Tenant.Id
45-
}
46-
$tenant = Get-AzureADTenantDetail
47-
$tenantName = ($tenant.VerifiedDomains | Where-Object { $_._Default -eq $True }).Name
43+
# Get the user running the script
44+
$currentUserPrincipalName = $context.Account
45+
$user = Get-MgUser -Filter "UserPrincipalName eq '$($context.Account)'"
46+
47+
# get the tenant we signed in to
48+
$Tenant = Get-MgOrganization
49+
$tenantName = $Tenant.DisplayName
4850

51+
$verifiedDomain = $Tenant.VerifiedDomains | where {$_.Isdefault -eq $true}
52+
$verifiedDomainName = $verifiedDomain.Name
53+
$tenantId = $Tenant.Id
54+
55+
Write-Host ("Connected to Tenant {0} ({1}) as account '{2}'. Domain is '{3}'" -f $Tenant.DisplayName, $Tenant.Id, $currentUserPrincipalName, $verifiedDomainName)
56+
4957
# Removes the applications
50-
Write-Host "Cleaning-up applications from tenant '$tenantName'"
58+
Write-Host "Cleaning-up applications from tenant '$tenantId'"
5159

5260
Write-Host "Removing 'webApp' (WebApp) if needed"
53-
Get-AzureADApplication -Filter "DisplayName eq 'WebApp'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54-
$apps = Get-AzureADApplication -Filter "DisplayName eq 'WebApp'"
61+
try
62+
{
63+
Get-MgApplication -Filter "DisplayName eq 'WebApp'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
64+
}
65+
catch
66+
{
67+
$message = $_
68+
Write-Warning $Error[0]
69+
Write-Host "Unable to remove the application 'WebApp'. Error is $message. Try deleting manually." -ForegroundColor White -BackgroundColor Red
70+
}
71+
72+
Write-Host "Making sure there are no more (WebApp) applications found, will remove if needed..."
73+
$apps = Get-MgApplication -Filter "DisplayName eq 'WebApp'" | Format-List Id, DisplayName, AppId, SignInAudience, PublisherDomain
74+
5575
if ($apps)
5676
{
57-
Remove-AzureADApplication -ObjectId $apps.ObjectId
77+
Remove-MgApplication -ApplicationId $apps.Id
5878
}
5979

6080
foreach ($app in $apps)
6181
{
62-
Remove-AzureADApplication -ObjectId $app.ObjectId
82+
Remove-MgApplication -ApplicationId $app.Id
6383
Write-Host "Removed WebApp.."
6484
}
85+
6586
# also remove service principals of this app
66-
Get-AzureADServicePrincipal -filter "DisplayName eq 'WebApp'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
67-
87+
try
88+
{
89+
Get-MgServicePrincipal -filter "DisplayName eq 'WebApp'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false}
90+
}
91+
catch
92+
{
93+
$message = $_
94+
Write-Warning $Error[0]
95+
Write-Host "Unable to remove ServicePrincipal 'WebApp'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
96+
}
97+
}
98+
99+
# Pre-requisites
100+
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph")) {
101+
Install-Module "Microsoft.Graph" -Scope CurrentUser
102+
}
103+
104+
#Import-Module Microsoft.Graph
105+
106+
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Authentication")) {
107+
Install-Module "Microsoft.Graph.Authentication" -Scope CurrentUser
108+
}
109+
110+
Import-Module Microsoft.Graph.Authentication
111+
112+
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Identity.DirectoryManagement")) {
113+
Install-Module "Microsoft.Graph.Identity.DirectoryManagement" -Scope CurrentUser
114+
}
115+
116+
Import-Module Microsoft.Graph.Identity.DirectoryManagement
117+
118+
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Applications")) {
119+
Install-Module "Microsoft.Graph.Applications" -Scope CurrentUser
120+
}
121+
122+
Import-Module Microsoft.Graph.Applications
123+
124+
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Groups")) {
125+
Install-Module "Microsoft.Graph.Groups" -Scope CurrentUser
126+
}
127+
128+
Import-Module Microsoft.Graph.Groups
129+
130+
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Users")) {
131+
Install-Module "Microsoft.Graph.Users" -Scope CurrentUser
132+
}
133+
134+
Import-Module Microsoft.Graph.Users
135+
136+
$ErrorActionPreference = "Stop"
137+
138+
139+
try
140+
{
141+
Cleanup -tenantId $tenantId -environment $azureEnvironmentName
142+
}
143+
catch
144+
{
145+
$_.Exception.ToString() | out-host
146+
$message = $_
147+
Write-Warning $Error[0]
148+
Write-Host "Unable to register apps. Error is $message." -ForegroundColor White -BackgroundColor Red
68149
}
69150

70-
Cleanup -Credential $Credential -tenantId $TenantId
151+
Write-Host "Disconnecting from tenant"
152+
Disconnect-MgGraph

0 commit comments

Comments
 (0)