forked from FBoucher/AzurePowerTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete-AllResources.ps1
62 lines (40 loc) · 2.02 KB
/
Delete-AllResources.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
#================================================================
#= Very dangerous interactive script that delete all rescources
#= from all rescourcegroup in a specific subscription
#================================================================
# How to install and configure Azure PowerShell
# https://docs.microsoft.com/en-us/powershell/azureps-cmdlets-docs/
# Login
Login-AzureRmAccount
# Get a list of all Azure subscript that the user can access
$allSubs = Get-AzureRmSubscription
$allSubs | Sort-Object Name | Format-Table -Property Name, SubscriptionId, State
$theSub = Read-Host "Enter the subscriptionId you want to clean"
Write-Host "You select the following subscription. (it will be display 15 sec.)" -ForegroundColor Cyan
Get-AzureRmSubscription -SubscriptionId $theSub | Select-AzureRmSubscription
# Get all the resources groups
$allRG = Get-AzureRmResourceGroup
foreach ( $g in $allRG){
Write-Host $g.ResourceGroupName -ForegroundColor Yellow
Write-Host "------------------------------------------------------`n" -ForegroundColor Yellow
# Using -ODataQuery is compatible with AzureRM 5.x and AzureRM 6.x. -ResourceGroupName is only in 6.x
$query = "`$filter=resourceGroup eq '{0}'" -f $g.ResourceGroupName
$allResources = Get-AzureRmResource -ODataQuery $query
if($allResources){
$allResources | Format-Table -Property Name, ResourceName
}else{
Write-Host "-- empty--`n"
}
Write-Host "`n`n------------------------------------------------------" -ForegroundColor Yellow
}
exit
$lastValidation = Read-Host "Do you wich to delete ALL the resouces previously listed? (YES/ NO)"
if($lastValidation.ToLower().Equals("yes")){
foreach ( $g in $allRG){
Write-Host "Deleting " $g.ResourceGroupName
# Last safety, you need to remove the -WhatIf parameter to really delete the resources
Remove-AzureRmResourceGroup -Name $g.ResourceGroupName -Force -WhatIf
}
}else{
Write-Host "Aborded. Nothing was deleted." -ForegroundColor Cyan
}