-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathRemove-AzVM.ps1
161 lines (139 loc) · 6.51 KB
/
Remove-AzVM.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
#Requires -Version 5.0
#Requires -Modules Az.Compute
<#
.SYNOPSIS
Removes a virtual machine from Azure
.DESCRIPTION
This script is inspired by the article "Delete an Azure VM with objects using PowerShell" by Adam Bertram published by 4sysops.
.NOTES
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
The terms of use for ScriptRunner do not apply to this script. In particular, ScriptRunner Software GmbH assumes no liability for the function,
the use and the consequences of the use of this freely available script.
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of ScriptRunner Software GmbH.
© ScriptRunner Software GmbH
.COMPONENT
Requires Module Az.Compute
Requires Library script AzureAzLibrary.ps1
.LINK
https://github.com/scriptrunner/ActionPacks/blob/master/Azure/Compute
https://4sysops.com/archives/delete-an-azure-vm-with-objects-using-powershell/
https://github.com/adbertram/Random-PowerShell-Work/blob/master/Azure/Remove-AzrVirtualMachine.ps1
.Parameter Name
[sr-en] Specifies the name of the virtual machine
[sr-de] Name der virtuellen Maschine
.Parameter ResourceGroupName
[sr-en] Specifies the name of the resource group of the virtual machine
[sr-de] Name der resource group die die virtuelle Maschine enthält
.Parameter RemoveAssociatedResources
[sr-en] Remove all associated resources
[sr-de] Löschen aller verwendeter Ressourcen der virtuellen Maschine
#>
param(
[Parameter(Mandatory = $true)]
[string]$Name,
[Parameter(Mandatory = $true)]
[string]$ResourceGroupName,
[switch]$RemoveAssociatedResources
)
Import-Module Az.Compute
try{
$Script:VM = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $Name -ErrorAction Stop
$Script:Disks = Get-AzDisk | Where-Object { $_.ManagedBy -eq $Script:VM.Id }
function RemoveResourcesAfterDelete(){
try{
# remove vNICs
Write-Output "remove vNICs"
foreach($nicID in $Script:VM.NetworkProfile.NetworkInterfaces.Id) {
$tmpNic = Get-AzNetworkInterface -ResourceGroupName $Script:VM.ResourceGroupName -Name $nicID.Split('/')[-1]
$null = Remove-AzNetworkInterface -Name $tmpNic.Name -ResourceGroupName $Script:VM.ResourceGroupName -Force
foreach($ipAddress in $tmpNic.IpConfigurations) {
if($null -ne $ipAddress.PublicIpAddress) {
$null = Remove-AzPublicIpAddress -ResourceGroupName $Script:VM.ResourceGroupName -Name $ipAddress.PublicIpAddress.Id.Split('/')[-1] -Force
}
}
}
# remove os disk
if ('Uri' -in $Script:VM.StorageProfile.OSDisk.Vhd) {
Write-Output "remove os blob"
$diskId = $Script:VM.StorageProfile.OSDisk.Vhd.Uri
$conName = $diskId.Split('/')[-2]
$stoAcc = Get-AzStorageAccount | Where-Object { $_.StorageAccountName -eq $diskId.Split('/')[2].Split('.')[0] }
$stoAcc | Remove-AzStorageBlob -Container $conName -Blob $diskId.Split('/')[-1]
$stoAcc | Get-AzStorageBlob -Container $conName -Blob "$($Script:VM.Name)*.status" | Remove-AzStorageBlob
}
else {
if($null -ne $Script:Disks){
$Script:Disks | Remove-AzDisk -Force -Confirm:$false
}
}
# Remove other disks
Write-Output "remove other disks"
if ('DataDiskNames' -in $Script:VM.PSObject.Properties.Name -and @($Script:VM.DataDiskNames).Count -gt 0) {
foreach ($item in $Script:VM.StorageProfile.DataDisks.Vhd.Uri) {
$stoAcc = Get-AzStorageAccount -Name $item.Split('/')[2].Split('.')[0]
$stoAcc | Remove-AzStorageBlob -Container $item.Split('/')[-2] -Blob $item.Split('/')[-1]
}
}
# remove network securtity group
Write-Output "remove network securtity group"
$secGroup = Get-AzNetworkSecurityGroup -Name "$($Script:VM.Name)*" -ResourceGroupName $ResourceGroupName
if($null -ne $secGroup){
$null = Remove-AzNetworkSecurityGroup -Name $secGroup.Name -ResourceGroupName $ResourceGroupName -Confirm:$false -Force
}
}
catch{
Write-Output $_.Exception.Message
}
}
function RemoveResourcesBeforeDelete(){
try{
if ($null -eq $Script:VM.DiagnosticsProfile.bootDiagnostics) {
return
}
# remove bootDiagnostics
Write-Output "remove boot diagnostics"
[string]$stoName = [regex]::match($Script:VM.DiagnosticsProfile.bootDiagnostics.storageUri, '^http[s]?://(.+?)\.').groups[1].value
[int]$nameLength = 9
if($Script:VM.Name.Length -lt 9){
$nameLength = ($Script:VM.Name.Length - 1)
}
[string]$conName = ('bootdiagnostics-{0}-{1}' -f $Script:VM.Name.ToLower().Substring(0, $nameLength), $Script:VM.vmId)
[string]$resgrpName = (Get-AzStorageAccount | Where-Object { $_.StorageAccountName -eq $stoName }).ResourceGroupName
[hashtable]$cmdArgs = @{
'ResourceGroupName' = $resgrpName
'Name' = $stoName
}
Get-AzStorageAccount @cmdArgs | `
Get-AzStorageContainer | Where-Object { $_.Name -eq $conName } | `
Remove-AzStorageContainer –Force -ErrorAction Stop
}
catch{
Write-Output $_.Exception.Message
}
}
[hashtable]$cmdArgs = @{'ErrorAction' = 'Stop'
'Confirm' = $false
'Force' = $null
'Name' = $Name
'ResourceGroupName' = $ResourceGroupName}
if($RemoveAssociatedResources.IsPresent){ # delete resources that must be removed before remove vm
RemoveResourcesBeforeDelete
}
$null = Remove-AzVM @cmdArgs
if($RemoveAssociatedResources.IsPresent){ # delete resources that must be removed after remove vm
RemoveResourcesAfterDelete
}
$ret = "Virtual machine $($Name) removed"
if($SRXEnv) {
$SRXEnv.ResultMessage = $ret
}
else{
Write-Output $ret
}
}
catch{
throw
}
finally{
}