-
Notifications
You must be signed in to change notification settings - Fork 4
/
Remove-AzureDtlLabVMs.ps1
109 lines (85 loc) · 3.05 KB
/
Remove-AzureDtlLabVMs.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
<#
.SYNOPSIS
This script deletes the Azure virtual machines with the passed Ids in the DevTest Lab.
.DESCRIPTION
This script deletes the Azure virtual machines with the passed Ids in the DevTest Lab. It will
process all VMs in parallel divided into blocks.
.PARAMETER ids
Mandatory. Resource ids for the VMs to delete.
.PARAMETER profilePath
Optional. Path to file with Azure Profile. How to generate this file is explained at the end of the Readme for the repo (https://github.com/lucabol/University).
Default "$env:APPDATA\AzProfile.txt".
.EXAMPLE
Remove-AzureDtlLabVMs -ids $vms
.NOTES
#>
[cmdletbinding()]
param
(
[Parameter(Mandatory = $true, HelpMessage = "Resource ids for the VMs to delete")]
[string[]] $ids,
[Parameter(Mandatory = $false, HelpMessage = "Path to file with Azure Profile")]
[string] $profilePath = "$env:APPDATA\AzProfile.txt"
)
trap {
# NOTE: This trap will handle all errors. There should be no need to use a catch below in this
# script, unless you want to ignore a specific error.
Handle-LastError
}
. .\Common.ps1
function DeleteSingleVM() {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, HelpMessage = "Resource id for the VM to delete")]
[string] $id,
[Parameter(Mandatory = $false, HelpMessage = "Path to file with Azure Profile")]
[string] $profilePath = "$env:APPDATA\AzProfile.txt"
)
try {
$azVer = GetAzureModuleVersion
if ($azVer -ge "3.8.0") {
Save-AzureRmContext -Path $global:profilePath | Write-Verbose
}
else {
Save-AzureRmProfile -Path $global:profilePath | Write-Verbose
}
$name = $id.Split('/')[-1]
Write-Verbose "Removing virtual machine $name ..."
Remove-AzureRmResource -Force -ResourceId "$id" | Out-Null
Write-Verbose "Done Removing $name ."
} catch {
$posMessage = $_.ToString() + "`n" + $_.InvocationInfo.PositionMessage
Write-Error "`nERROR: $posMessage" -ErrorAction "Continue"
}
}
$deleteVmBlock = {
param($id, $profilePath)
DeleteSingleVM -id $id -profilePath $profilePath
}
try {
LogOutput "Start Removal"
$credentialsKind = InferCredentials
LogOutput "Credentials: $credentialsKind"
LoadAzureCredentials -credentialsKind $credentialsKind -profilePath $profilePath
$jobs = @()
foreach ($id in $ids) {
if ($credentialsKind -eq "File") {
LogOutput "Starting job to delete $id ..."
$jobs += Start-Job -Name $id -ScriptBlock $deleteVmBlock -ArgumentList $id, $profilePath
LogOutput "$id deleted."
}
else {
DeleteSingleVM -id $id -profilePath $profilePath
}
}
if ($credentialsKind -eq "File") {
Wait-Job -Job $jobs | Write-Verbose
}
LogOutput "VM Deletion jobs have completed"
} finally {
if ($credentialsKind -eq "File") {
1..3 | % { [console]::beep(2500, 300) } # Make a sound to indicate we're done.
}
popd
}