-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathCleanup.ps1
244 lines (194 loc) · 8.01 KB
/
Cleanup.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#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
)
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph.Groups")) {
Install-Module "Microsoft.Graph.Groups" -Scope CurrentUser
}
Import-Module Microsoft.Graph.Groups
<#.Description
This function creates a new Azure AD Security Group with provided values
#>
Function CreateSecurityGroup([string] $name, [string] $description)
{
Write-Host "Creating a security group by the name '$name'."
$newGroup = New-MgGroup -Description $description -DisplayName $name -MailEnabled:$false -SecurityEnabled:$true -MailNickName $name
return Get-MgGroup -Filter "DisplayName eq '$name'"
}
<#.Description
This function first checks and then creates a new Azure AD Security Group with provided values, if required
#>
Function CreateIfNotExistsSecurityGroup([string] $name, [string] $description, [switch] $promptBeforeCreate)
{
# check if Group exists
$group = Get-MgGroup -Filter "DisplayName eq '$name'"
if( $group -eq $null)
{
if ($promptBeforeCreate)
{
$confirmation = Read-Host "Proceed to create a new security group named '$name' in the tenant ? (Y/N)"
if($confirmation -eq 'y')
{
$group = CreateSecurityGroup -name $name -description $description
}
}
else
{
Write-Host "No Security Group created!"
}
}
return $group
}
<#.Description
This function first checks and then deletes an existing Azure AD Security Group, if required
#>
Function RemoveSecurityGroup([string] $name, [switch] $promptBeforeDelete)
{
# check if Group exists
$group = Get-MgGroup -Filter "DisplayName eq '$name'"
if( $group -ne $null)
{
if ($promptBeforeDelete)
{
$confirmation = Read-Host "Proceed to delete an existing group named '$name' in the tenant ?(Y/N)"
if($confirmation -eq 'y')
{
Remove-MgGroup -GroupId $group.Id
Write-Host "Security group '$name' successfully deleted"
}
}
else
{
Write-Host "No Security group by name '$name' exists in the tenant, no deletion needed."
}
}
return $group.Id
}
<#.Description
This function assigns a provided user to a security group
#>
Function AssignUserToGroup([Microsoft.Graph.PowerShell.Models.MicrosoftGraphUser]$userToAssign, [Microsoft.Graph.PowerShell.Models.MicrosoftGraphGroup]$groupToAssign)
{
$owneruserId = $userToAssign.Id
$params = @{
"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/{$owneruserId}"
}
New-MgGroupMemberByRef -GroupId $groupToAssign.Id -BodyParameter $params
Write-Host "Successfully assigned user '$($userToAssign.UserPrincipalName)' to group '$($groupToAssign.DisplayName)'"
}
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 Group.ReadWrite.All" -Environment $azureEnvironmentName
}
else
{
Connect-MgGraph -TenantId $tenantId -Scopes "User.Read.All Organization.Read.All Application.ReadWrite.All Group.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 security groups, if relevant to the sample
RemoveSecurityGroup -name 'GroupAdmin' -promptBeforeDelete 'Y'
RemoveSecurityGroup -name 'GroupMember' -promptBeforeDelete 'Y'
}
# 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