git.lirion.de

Of git, get, and gud

aboutsummaryrefslogtreecommitdiffstats
path: root/AzureHelpers/Public/New-AzVmOsSnapshot.ps1
blob: dcf65355f63c8df6db0adc2de89886ec47b68ec4 (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
function New-AzVmOsSnapshot {
	<#
	.SYNOPSIS
	Create a snapshot of the OS disk of an Azure VM.
	
	.DESCRIPTION
	If we can unambiguously determine a single VM name inside the subscription
	we are logged into through the input string, we will create a snapshot
	of its OS disk.
	
	.INPUTS
	System.Object. @{ VmName = VMNAME; SnapshotName = SNAPSHOTNAME }
	
	.OUTPUTS
	String. Status messages and the actual AzureCLI outputs.
	#>
	[Alias(
		'Create-AzVmOsSnapshot',
		'azvmsnapshot',
		'azvmsnap'
	)]
	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,
		[Parameter(
			Mandatory=$true,
			ValueFromPipelineByPropertyName=$true,
			HelpMessage="Name of the snapshot",
			Position=1
			)
		]
		[ValidateLength(1,64)]
		[string]
        $SnapshotName
	)
	$ErrorActionPreference = 'Stop'
	$myvm = Find-AzVm -VmName $VmName
	$myDisk = (az vm show -g $myvm.resourceGroup -n $myvm.name --query "storageProfile.osDisk.managedDisk.id" -o json | ConvertFrom-Json)
	Write-Host "Identified VM: ${myvm.name}"
	Write-Host "Creating OS disk snapshot `"${SnapshotName}`""
	az snapshot create -o jsonc -g $myvm.resourceGroup --source $myDisk --name $SnapshotName `
		--query '[].{creationData: creationData, diskSizeGB: diskSizeGB, encryptionType: encryption.type, hyperVGeneration: hyperVGeneration, id: id, name: name, resourceGroup: resourceGroup, publicNetworkAccess: publicNetworkAccess}'
}