forked from petehauge/DTL-VM-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stop-LabVms.ps1
47 lines (34 loc) · 1.12 KB
/
Stop-LabVms.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
param
(
[Parameter(Mandatory=$true, HelpMessage="Name of lab to stop VMs into")]
[string] $DevTestLabName,
[Parameter(Mandatory=$true, HelpMessage="RG of lab to stop VMs into")]
[string] $ResourceGroupName
)
Write-Host "Stopping VMs in $DevTestLabName in RG $ResourceGroupName ..."
Write-Host "This might take a while ..."
# Get only the running VMs
$existingLab = Get-AzDtlLab -Name $DevTestLabName -ResourceGroupName $ResourceGroupName
if (-not $existingLab) {
throw "'$DevTestLabName' doesn't exist"
}
$runningVms = Get-AzDtlVm -Lab $existingLab -Status "Running"
if (-not $runningVms) {
throw "'$DevTestLabName' doesn't contain any VMs"
}
$jobs = @()
$runningVms | ForEach-Object {
$sb = [scriptblock]::create(
@"
Stop-AzDtlVm $_
if ($returnStatus.Status -eq 'Succeeded') {
Write-Output "Successfully stopped DTL machine: $dtlName"
}
else {
throw "Failed to stop DTL machine: $dtlName"
}
"@)
$jobs += Start-RSJob -ScriptBlock $sb -Name $_.Name -ModulesToImport $AzDtlModulePath
Start-Sleep -Seconds 2
}
Wait-RSJobWithProgress -secTimeout (2 * 60 * 60) -jobs $jobs