git.lirion.de

Of git, get, and gud

aboutsummaryrefslogtreecommitdiffstats
path: root/AzureHelpers/Public/Find-AzVm.ps1
blob: 73467c0562e5159d2c5176858ea4043d1a7f085e (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
65
66
67
68
69
function Find-AzVm {
	<#
	.SYNOPSIS
	Return an Azure VM object if we find exactly one match for a given VM name string.

	.DESCRIPTION
	We'll search for all VMs inside the subsctription we are logged into containing
	the input string. If we find exactly one match, we will output an object as
	delivered by `az vm list`.

	.EXAMPLE
		Find-AzVm -VmName "substring-01"

	.INPUTS
	String. The VM name we want to investigate. Part of the name is sufficient if
	unambiguous inside the active subscription.

	.OUTPUTS
	PSCustomObject. `az vm list` object of the resulting Azure virtual machine.	
	#>
	[OutputType([PSCustomObject])]
	[Alias(
		'azvmidentify',
		'azvmf',
		'azvmi'
	)]
	Param (
		[Parameter(
			Mandatory=$true,
			ValueFromPipeline=$true,
			HelpMessage="String that is a VM name or is part of one unambiguous VM",
			Position=0
			)
		]
		[ValidateLength(1,64)]
		[string]
        $VmName
	)
	$ErrorActionPreference = 'Stop'
	$count = 0
	$result = @()
	foreach ($myvm in (List-AzVms | ConvertFrom-Json) ) {
		if ($myvm.name.Contains($VmName)) {
			$count++
			$result += $myvm
		}
	}
	switch ($result.Count) {
		0 {
			throw [System.ArgumentNullException]::New("No VM found with its name containing `"$($VmName)`"")
		}
		1 {
			$true | Out-Null
		}
		Default {
			throw [System.ArgumentException]::New("More than one VM found with their names containing `"$($VmName)`"")
		}
	}
	# We throw exceptions whenever we don't have exactly one element - here we return the first element as there
	# should be only one at this stage.
	# Also, Powershell (again!): If you "return" something from a function anything else that produces output
	# will also be returned. Why, Microsoft, why ლ(ಠ_ಠლ)
	# > PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around:
	# >   * All output is captured, and returned
    # >   * The return keyword really just indicates a logical exit point
	# ( https://stackoverflow.com/a/10288256 )
	# This sorry state of a "shell" or "programming language" ...
	return $result[0]
}