-
Notifications
You must be signed in to change notification settings - Fork 4
/
purgeresourcegroup.ps1
73 lines (59 loc) · 1.85 KB
/
purgeresourcegroup.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
param ([Parameter(Mandatory)][string]$ResourceGroup)
<#
This PS script will delete all resources from the specified resource group
Does it use Azure CLI?
-----------------------
Yes
How does it work ?
-----------------
It attempts to delete in an infite loop till no more resources are left. This strategy takes care of resources
which are hard to delete due to interdependencies
#>
function DeleteResources()
{
Write-Host "Building list of resources in the resource group '$ResourceGroup'"
$count_deleted=0
$resources=(az resource list --resource-group $ResourceGroup | ConvertFrom-Json -AsHashTable)
if ($null -eq $resources)
{
Write-Host "No resources found"
return $count_deleted
}
Write-host ("Found {0} resources" -f $resources.Count)
foreach($resource in $resources)
{
try {
$id=$resource["id"]
Write-Host "Going to delete resource with id: $id"
& az resource delete --ids $id --verbose
$count_deleted+=1
}
catch {
Write-Host $_
}
}
return $count_deleted
}
function DeleteInLoop {
while ($true)
{
$deleted=DeleteResources
if (0 -eq $deleted)
{
Write-Host "No more resources to delete"
break;
}
Write-Host "No of resources deleted=$deleted Going to try again"
}
}
$subscription=& az account show --query name
Write-Host "Current subscription is: $subscription"
Write-Host "You entered Resource Grou: $ResourceGroup"
$passphrase= ("iamsure{0}" -f (Get-date).Millisecond)
$choice=Read-Host -Prompt "This script will delete all resources from the specified resource group. Type $passphrase to coninue"
if ($passphrase -ne $choice)
{
Write-Host "Does not match the expected pass phrase $passphrase. Quitting"
exit
}
DeleteInLoop