blob: 125e3d46e8f512a9f48e8d6c25d3e3c0a0576669 (
plain)
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
|
function Start-AzureVm {
# Supply a VM name (or an unambiguous part of it) and start the machine.
<#
.SYNOPSIS
Start an Azure VM whose name contains the input string.
.DESCRIPTION
If we can unambiguously determine a single VM name inside the subscription
we are logged into through the input string, we will start
the machine.
This function was built as we want to have terse input and a counterpart
to Stop-AzVm.
.INPUTS
String. The names or unambiguous parts of the names of the VMs we intend to
start.
.OUTPUTS
String. Status messages and the actual AzureCLI outputs.
#>
[Alias(
'azvmstart',
'azvmb',
)]
Param (
[Parameter(
Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
HelpMessage="Strings that are VM names or are part of single unambiguous VMs",
Position=0
)
]
[ValidateLength(1,64)]
[string[]]
$VmName
)
$ErrorActionPreference = 'Stop'
$resVms = @()
Write-Host "Checking state of VM(s)..."
foreach ($singlevm in $VmName) {
$myvm = azvmidentify -VmName $singlevm
# Since az vm start is taking quite its time even when the machine is started, we should check ourselves whether the machine is running:
if ( (az vm show -d -g $myvm.resourceGroup -n $myvm.name -o json | COnvertFrom-Json).powerState.Contains('running')) {
Write-Host "VM $($myvm.name) (RG: $($myvm.resourceGroup)) is already running."
} else {
Write-Host "VM $($myvm.name) (RG: $($myvm.resourceGroup)) is not started."
$resVms += $myvm
}
}
Write-Host "Starting remaining $($resVms.Count) machine(s)..."
$jobs = (
$resVms | ForEach-Object {
# Write-Host "Starting $($_.name) (RG: $($_.resourceGroup)):"
$myvm = $_
Start-ThreadJob -ScriptBlock {
$this = $using:myvm
az vm start -o jsonc -g $this.resourceGroup -n $this.name
}
# Write-Host "...done."
}
)
$jobs | Receive-Job -Wait -AutoRemoveJob
}
|