This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCleanup.ps1
93 lines (80 loc) · 3.42 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
[CmdletBinding()]
param(
[PSCredential] $Credential,
[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 (it defaults to AzureCloud)')]
[string] $azureEnvironmentName
)
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
Install-Module "AzureAD" -Scope CurrentUser
}
Import-Module AzureAD
$ErrorActionPreference = "Stop"
Function Cleanup
{
if (!$azureEnvironmentName)
{
$azureEnvironmentName = "AzureCloud"
}
<#
.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.
# Login to Azure PowerShell (interactive if credentials are not already provided:
# you'll need to sign-in with creds enabling your to create apps in the tenant)
if (!$Credential -and $TenantId)
{
$creds = Connect-AzureAD -TenantId $tenantId -AzureEnvironmentName $azureEnvironmentName
}
else
{
if (!$TenantId)
{
$creds = Connect-AzureAD -Credential $Credential -AzureEnvironmentName $azureEnvironmentName
}
else
{
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential -AzureEnvironmentName $azureEnvironmentName
}
}
if (!$tenantId)
{
$tenantId = $creds.Tenant.Id
}
$tenant = Get-AzureADTenantDetail
$tenantName = ($tenant.VerifiedDomains | Where-Object { $_._Default -eq $True }).Name
# Removes the applications
Write-Host "Cleaning-up applications from tenant '$tenantName'"
Write-Host "Removing 'service' (ProfileAPI) if needed"
Get-AzureADApplication -Filter "DisplayName eq 'ProfileAPI'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
$apps = Get-AzureADApplication -Filter "DisplayName eq 'ProfileAPI'"
if ($apps)
{
Remove-AzureADApplication -ObjectId $apps.ObjectId
}
foreach ($app in $apps)
{
Remove-AzureADApplication -ObjectId $app.ObjectId
Write-Host "Removed ProfileAPI.."
}
# also remove service principals of this app
Get-AzureADServicePrincipal -filter "DisplayName eq 'ProfileAPI'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
Write-Host "Removing 'client' (ProfileSPA) if needed"
Get-AzureADApplication -Filter "DisplayName eq 'ProfileSPA'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
$apps = Get-AzureADApplication -Filter "DisplayName eq 'ProfileSPA'"
if ($apps)
{
Remove-AzureADApplication -ObjectId $apps.ObjectId
}
foreach ($app in $apps)
{
Remove-AzureADApplication -ObjectId $app.ObjectId
Write-Host "Removed ProfileSPA.."
}
# also remove service principals of this app
Get-AzureADServicePrincipal -filter "DisplayName eq 'ProfileSPA'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
}
Cleanup -Credential $Credential -tenantId $TenantId