This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Cleanup.ps1
157 lines (133 loc) · 5.88 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
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
# --------------------------------------------------------------------------------------------
[CmdletBinding()]
param (
[string]$ResourceGroupName = "MSLearnLTI",
[string]$AppName = "MS-Learn-Lti-Tool-App",
[switch]$UseActiveAzureAccount,
[string]$SubscriptionNameOrId = $null
)
process {
function Write-Title([string]$Title) {
Write-Host "`n`n============================================================="
Write-Host $Title
Write-Host "=============================================================`n`n"
}
#region Show Learn LTI Banner
Write-Host ''
Write-Host ' _ ______ _____ _ _ _ _______ _____ '
Write-Host '| | | ____| /\ | __ \| \ | | | | |__ __|_ _|'
Write-Host '| | | |__ / \ | |__) | \| | ______ | | | | | | '
Write-Host '| | | __| / /\ \ | _ /| . ` | |______| | | | | | | '
Write-Host '| |____| |____ / ____ \| | \ \| |\ | | |____| | _| |_ '
Write-Host '|______|______/_/ \_\_| \_\_| \_| |______|_| |_____|'
Write-Host ''
Write-Host ''
#endregion
#region Setup Logging
. .\Write-Log.ps1
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$ExecutionStartTime = $(get-date -f dd-MM-yyyy-HH-mm-ss)
$LogRoot = Join-Path $ScriptPath "Log"
$LogFile = Join-Path $LogRoot "Log-$ExecutionStartTime.log"
Set-LogFile -Path $LogFile
$TranscriptFile = Join-Path $LogRoot "Transcript-$ExecutionStartTime.log"
Start-Transcript -Path $TranscriptFile;
#endregion
#region Login to Azure CLI
Write-Title 'STEP #1 - Logging into Azure'
function Test-LtiActiveAzAccount {
$account = az account show | ConvertFrom-Json
if(!$account) {
throw "Error while trying to get Active Account Info."
}
}
function Connect-LtiAzAccount {
$loginOp = az login | ConvertFrom-Json
if(!$loginOp) {
throw "Encountered an Error while trying to Login."
}
}
if ($UseActiveAzureAccount) {
Write-Log -Message "Using Active Azure Account"
Test-LtiActiveAzAccount
}
else {
Write-Log -Message "Logging in to Azure"
Connect-LtiAzAccount
}
Write-Log -Message "Successfully logged in to Azure."
#endregion
#region Choose Active Subscription
Write-Title 'STEP #2 - Choose Subscription'
function Get-LtiSubscriptionList {
$AzAccountList = ((az account list --all --output json) | ConvertFrom-Json)
if(!$AzAccountList) {
throw "Encountered an Error while trying to fetch Subscription List."
}
Write-Output $AzAccountList
}
function Set-LtiActiveSubscription {
param (
[string]$NameOrId,
$List
)
$subscription = ($List | Where-Object { ($_.name -ieq $NameOrId) -or ($_.id -ieq $NameOrId) })
if(!$subscription) {
throw "Invalid Subscription Name/ID Entered."
}
az account set --subscription $NameOrId
#Intentionally not catching an exception here since the set subscription commands behavior (output) is different from others
Write-Output $subscription
}
Write-Log -Message "Fetching List of Subscriptions in Users Account"
$SubscriptionList = Get-LtiSubscriptionList
Write-Log -Message "List of Subscriptions:-`n$($SubscriptionList | ConvertTo-Json -Compress)"
$SubscriptionCount = ($SubscriptionList | Measure-Object).Count
Write-Log -Message "Count of Subscriptions: $SubscriptionCount"
if ($SubscriptionCount -eq 0) {
throw "Please create at least ONE Subscription in your Azure Account"
}
elseif ($SubscriptionNameOrId) {
Write-Log -Message "Using User provided Subscription Name/ID: $SubscriptionNameOrId"
}
elseif ($SubscriptionCount -eq 1) {
$SubscriptionNameOrId = $SubscriptionList[0].id;
Write-Log -Message "Defaulting to Subscription ID: $SubscriptionNameOrId"
}
else {
$SubscriptionListOutput = $SubscriptionList | Select-Object @{ l="Subscription Name"; e={ $_.name } }, "id", "isDefault"
Write-Host ($SubscriptionListOutput | Out-String)
$SubscriptionNameOrId = Read-Host 'Enter the Name or ID of the Subscription from Above List'
#trimming the input for empty spaces, if any
$SubscriptionNameOrId = $SubscriptionNameOrId.Trim()
Write-Log -Message "User Entered Subscription Name/ID: $SubscriptionNameOrId"
}
$ActiveSubscription = Set-LtiActiveSubscription -NameOrId $SubscriptionNameOrId -List $SubscriptionList
#endregion
#region Delete Resource Group, if Exists
Write-Title 'STEP #3 - Delete Resource Group'
az group delete --name $ResourceGroupName --yes
if ($LASTEXITCODE -ne 0) {
throw "Unable to delete Resource Group [ $ResourceGroupName ] and its Child Resources"
}
Write-Host 'Resource Group Deleted Successfully'
#endregion
#region Delete App Registration, if Exists
Write-Title 'STEP #4 - Delete App Registration'
$AppInfo = (az ad app list --display-name $AppName) | ConvertFrom-Json
if (!$AppInfo) {
throw "Unable to find App Registration with Name [ $AppName ]"
}
az ad app delete --id $AppInfo.appId
if ($LASTEXITCODE -ne 0) {
throw "Unable to delete AAD App [ $AppName ]"
}
Write-Host 'App Registration Deleted Successfully'
#endregion
Write-Title '======== Successfully Deleted Resources from Azure ==========='
Write-Log -Message "Clean-up Complete"
Write-Warning 'Please use a different ResourceGroup name on re-deployment!'
}