-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove-AzVMDisksNICsPIPs.ps1
181 lines (130 loc) · 4.83 KB
/
Remove-AzVMDisksNICsPIPs.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
#------------------------------------------------------------------------------
#
# THIS MODULE AND ANY ASSOCIATED INFORMATION ARE PROVIDED 'AS IS' WITHOUT
# WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
# FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR
# RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#
#------------------------------------------------------------------------------
$ResourceGroup = Read-Host "Enter the Resource Group name"
$VMName = Read-Host "Enter the VM name"
$a = Get-Random -Maximum 9999 -Minimum 1
$vm = Get-AzVM -ResourceGroupName $ResourceGroup -Name $VMName
#Write Log
Start-Transcript -Path "$env:temp\DeleteAzVM-$a-$VMName.log" -NoClobber
write-host "
#------------------------------------------------------------------------------
#
# THIS MODULE AND ANY ASSOCIATED INFORMATION ARE PROVIDED 'AS IS' WITHOUT
# WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
# FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR
# RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#
#------------------------------------------------------------------------------
Running this module will cause deletion of resources, which cannot be recovered. Run this module at your own risk.
" -ForegroundColor Red
if(!$VM){Break}
Else{
#NIC
$nicname = @()
$nicname += $vm.NetworkProfile.NetworkInterfaces.id | Split-Path -Leaf
$nicRG = @()
$nicRG += $vm.NetworkProfile.NetworkInterfaces.id | ForEach-Object {$_ -replace ".*/resourceGroups/" -replace "/.*"}
$GetNIC = @()
For ($i=0;$i -lt $nicname.Count; $i++){
$GetNIC += Get-AzNetworkInterface -Name $nicname[$i] -ResourceGroupName $nicRG[$i]
}
$Ipconfname = $getnic.IpConfigurations.id
# PiP Check
$PiPName = @()
$PiPRG = @()
if(!$getnic.IpConfigurations.publicipaddress.id){
Write-Host "No Public IP Found"}
Else{
$PiPName += $getnic.IpConfigurations.publicipaddress.id | Split-Path -Leaf
$PiPRG += $getnic.IpConfigurations.publicipaddress.id | ForEach-Object {$_ -replace ".*/resourceGroups/" -replace "/.*"}
$getpip = @()
for($i=0;$i -lt $PiPName.count; $i++){
$getpip += Get-AzPublicIpAddress -Name $PiPName[$i] -ResourceGroupName $PiPRG[$i]
}}
#Disks:
$OSDisk = $vm.StorageProfile.OsDisk.Name
$dataDisk = $vm.StorageProfile.DataDisks.name
#Disks RG
$OSDiskRG = $vm.StorageProfile.OsDisk.ManagedDisk.Id | ForEach-Object {$_ -replace ".*/resourceGroups/" -replace "/.*"}
$dataDiskRG = $vm.StorageProfile.DataDisks.ManagedDisk.Id | ForEach-Object {$_ -replace ".*/resourceGroups/" -replace "/.*"}
#Disk Details
$disk = @()
$disk += $OSDisk
if(!$dataDisk){$null
}
Else{
$disk += $dataDisk
}
$diskRG = @()
$diskRG += $OSDiskRG
if(!$dataDiskRG)
{$null}
Else{$diskRG +=$dataDiskRG}
$diskIDs = @()
$diskIDs += $($vm.StorageProfile.OsDisk.ManagedDisk.Id),$($vm.StorageProfile.DataDisks.ManagedDisk.Id)
$getdisk = @()
For($i=0;$i -lt $disk.count; $i++){
$getdisk += Get-AzDisk -ResourceGroupName $diskRG[$i] -DiskName $disk[$i]
}
#Write-Host "Following Items will be deleted press Y to confirm" -ForegroundColor Red
Write-Host "VM" -ForegroundColor Green
$vm.Id
"`n"
Write-Host "NICs" -ForegroundColor Green
$($vm.NetworkProfile.NetworkInterfaces.id)
"`n"
Write-Host "Disks" -ForegroundColor Green
$disk
"`n"
Write-Host "IPConfigs" -ForegroundColor Green
$($getnic.IpConfigurations.id)
$GetNIC.ipconfigurations.privateipaddress
"`n"
Write-Host "Public IPs" -ForegroundColor Green
if(!$getnic.IpConfigurations.publicipaddress.id){
Write-Host "No Public IP Found"}
Else{
$($getnic.IpConfigurations.publicipaddress.id)
$getpip.IPaddress
"`n"
}
Write-Host "Resources listed above will be deteled, and cannot be recovered. Enter Y to proceed, press any other key to stop" -ForegroundColor Red
$conf = Read-Host "Enter Y to proceed, press any other key to stop"
if ($conf -ne 'y'){Break}
Else{
#Delay
Write-Host "You have selected $conf."
$delay = (Get-Date).AddSeconds(10)
while($delay -gt $(get-date)){
$sec = $delay - $(Get-Date)
Write-Host "Deleting the resources in $($sec.Seconds) Seconds..."
Start-Sleep -Seconds 1
#Write-Progress -Activity "Deleting resources in" -SecondsRemaining $sec.Seconds
}
#Removing VM
#Remove-AzVM -Name $VMName -ResourceGroupName $ResourceGroup -Force
#Removing Disks
For($i=0;$i -lt $disk.count; $i++){
Remove-AzDisk -ResourceGroupName $diskRG[$i] -DiskName $disk[$i] -Force
}
#Remove NIC
For ($i=0;$i -lt $nicname.Count; $i++){
Remove-AzNetworkInterface -Name $nicname[$i] -ResourceGroupName $nicRG[$i] -Force
}
#Remove IPConfig
#Remove PIP
for($i=0;$i -lt $PiPName.count; $i++){
Remove-AzPublicIpAddress -Name $PiPName[$i] -ResourceGroupName $PiPRG[$i] -Force
}
}
}
Stop-Transcript
Start-Process $env:temp