git.lirion.de

Of git, get, and gud

aboutsummaryrefslogtreecommitdiffstats
path: root/AzureHelpers/Public/Stop-AzVm.ps1
blob: 1b3bd07ffe19be6ab8fbceeca49b366e5c439c7e (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
function Stop-AzVm {
	# Supply a VM name (or an unambiguous part of it), stop, and deallocate the machine.
	<#
	.SYNOPSIS
	Stop **and** deallocate 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 stop and deallocate
	the machine.
	
	This function was built as we want to have terse input and we do not want to
	be billed for machines we stop, the latter requiring two actual commands.
	
	.INPUTS
	String. The name or unambiguous part of the name of the VM we intend to
	stop and deallocate.
	
	.OUTPUTS
	String. Status messages and the actual AzureCLI outputs.
	#>
	[Alias(
		'azvmstop',
		'azvmd',
		'Deallocate-AzVm'
	)]
	Param (
		[Parameter(
			Mandatory=$true,
			ValueFromPipelineByPropertyName=$true,
			HelpMessage="String that is a VM name or is part of one unambiguous VM",
			Position=0
			)
		]
		[ValidateLength(1,64)]
		[string]
        $VmName
	)
	$ErrorActionPreference = 'Stop'
	$myvm = azvmidentify -VmName $VmName
	# Since az vm stop is taking quite its time even when the machine is started, we should check ourselves whether the machine is running:
	$myPowerState = (az vm show -d -g $myvm.resourceGroup -n $myvm.name -o json | ConvertFrom-Json).powerState
	if ( ($myPowerState -match '(stopped|deallocated)$') ) {
		Write-Host "VM $($myvm.name) (RG: $($myvm.resourceGroup)) is already stopped."
	} else {
		Write-Host "Stopping $($myvm.name) (RG: $($myvm.resourceGroup)):"
		az vm stop -g $myvm.resourceGroup -n $myvm.name
		Write-Host "...done."
	}
	if ( ($myPowerState -match 'deallocated$') ) {
		Write-Host "VM $($myvm.name) (RG: $($myvm.resourceGroup)) is already deallocated."
	} else {
		Write-Host "Deallocating $($myvm.name) (RG: $($myvm.resourceGroup)):"
		az vm deallocate -g $myvm.resourceGroup -n $myvm.name
		Write-Host "...done."
	}
}