-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathCleanup-WithCertificates.ps1
155 lines (120 loc) · 5.21 KB
/
Cleanup-WithCertificates.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#Requires -Version 7
[CmdletBinding()]
param(
[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')]
[string] $tenantId,
[Parameter(Mandatory=$False, HelpMessage='Azure environment to use while running the script. Default = Global')]
[string] $azureEnvironmentName
)
Function Cleanup
{
if (!$azureEnvironmentName)
{
$azureEnvironmentName = "Global"
}
<#
.Description
This function removes the Azure AD applications for the sample. These applications were created by the Configure.ps1 script
#>
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
# Connect to the Microsoft Graph API
Write-Host "Connecting to Microsoft Graph"
if ($tenantId -eq "")
{
Connect-MgGraph -Scopes "User.Read.All Organization.Read.All Application.ReadWrite.All" -Environment $azureEnvironmentName
}
else
{
Connect-MgGraph -TenantId $tenantId -Scopes "User.Read.All Organization.Read.All Application.ReadWrite.All" -Environment $azureEnvironmentName
}
$context = Get-MgContext
$tenantId = $context.TenantId
# Get the user running the script
$currentUserPrincipalName = $context.Account
$user = Get-MgUser -Filter "UserPrincipalName eq '$($context.Account)'"
# get the tenant we signed in to
$Tenant = Get-MgOrganization
$tenantName = $Tenant.DisplayName
$verifiedDomain = $Tenant.VerifiedDomains | where {$_.Isdefault -eq $true}
$verifiedDomainName = $verifiedDomain.Name
$tenantId = $Tenant.Id
Write-Host ("Connected to Tenant {0} ({1}) as account '{2}'. Domain is '{3}'" -f $Tenant.DisplayName, $Tenant.Id, $currentUserPrincipalName, $verifiedDomainName)
# Removes the applications
Write-Host "Cleaning-up applications from tenant '$tenantId'"
Write-Host "Removing 'client' (msal-node-webapp) if needed"
try
{
Get-MgApplication -Filter "DisplayName eq 'msal-node-webapp'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
}
catch
{
$message = $_
Write-Warning $Error[0]
Write-Host "Unable to remove the application 'msal-node-webapp'. Error is $message. Try deleting manually." -ForegroundColor White -BackgroundColor Red
}
Write-Host "Making sure there are no more (msal-node-webapp) applications found, will remove if needed..."
$apps = Get-MgApplication -Filter "DisplayName eq 'msal-node-webapp'" | Format-List Id, DisplayName, AppId, SignInAudience, PublisherDomain
if ($apps)
{
Remove-MgApplication -ApplicationId $apps.Id
}
foreach ($app in $apps)
{
Remove-MgApplication -ApplicationId $app.Id
Write-Host "Removed msal-node-webapp.."
}
# also remove service principals of this app
try
{
Get-MgServicePrincipal -filter "DisplayName eq 'msal-node-webapp'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false}
}
catch
{
$message = $_
Write-Warning $Error[0]
Write-Host "Unable to remove ServicePrincipal 'msal-node-webapp'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
}
# remove self-signed certificate
Write-Host "Removing CN=msal-node-webapp certificate from Cert:/CurrentUser/My"
Get-ChildItem -Path Cert:\CurrentUser\My | where { $_.subject -eq "CN=msal-node-webapp" } | Remove-Item
}
# Pre-requisites
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph")) {
Install-Module "Microsoft.Graph" -Scope CurrentUser
}
#Import-Module Microsoft.Graph
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Authentication")) {
Install-Module "Microsoft.Graph.Authentication" -Scope CurrentUser
}
Import-Module Microsoft.Graph.Authentication
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Identity.DirectoryManagement")) {
Install-Module "Microsoft.Graph.Identity.DirectoryManagement" -Scope CurrentUser
}
Import-Module Microsoft.Graph.Identity.DirectoryManagement
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Applications")) {
Install-Module "Microsoft.Graph.Applications" -Scope CurrentUser
}
Import-Module Microsoft.Graph.Applications
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Groups")) {
Install-Module "Microsoft.Graph.Groups" -Scope CurrentUser
}
Import-Module Microsoft.Graph.Groups
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Users")) {
Install-Module "Microsoft.Graph.Users" -Scope CurrentUser
}
Import-Module Microsoft.Graph.Users
$ErrorActionPreference = "Stop"
try
{
Cleanup -tenantId $tenantId -environment $azureEnvironmentName
}
catch
{
$_.Exception.ToString() | out-host
$message = $_
Write-Warning $Error[0]
Write-Host "Unable to register apps. Error is $message." -ForegroundColor White -BackgroundColor Red
}
Write-Host "Disconnecting from tenant"
Disconnect-MgGraph