Skip to content

Get-VM

Azure: Gets properties of Azure virtual machines

param(
	[Parameter(Mandatory = $true, ParameterSetName = 'ResourceGroup')]
	[string]$Name,

	[Parameter(Mandatory = $true, ParameterSetName = 'ResourceGroup')]
	[string]$ResourceGroupName,

	[Parameter(Mandatory = $true, ParameterSetName = 'Location')]
	[string]$Location,

	[Parameter(ParameterSetName = 'All')]
	[Parameter(ParameterSetName = 'Location')]
	[Parameter(ParameterSetName = 'ResourceGroup')]
	[ValidateSet('*', 'Name', 'Location', 'ResourceGroupName', 'Tags', 'VmId', 'StatusCode', 'ID')]
	[string[]]$Properties = @('Name', 'Location', 'ResourceGroupName', 'Tags', 'VmId', 'StatusCode', 'ID')
)

try {
	Import-Module Az.Compute -ErrorAction Stop

	if ($Properties -contains '*') {
		$Properties = @('*')
	}

	[hashtable]$cmdArgs = @{ 'ErrorAction' = 'Stop' }

	if ($PSCmdlet.ParameterSetName -eq 'ResourceGroup') {
		$cmdArgs.Add('Name', $Name)
		$cmdArgs.Add('ResourceGroupName', $ResourceGroupName)
	} elseif ($PSCmdlet.ParameterSetName -eq 'Location') {
		$cmdArgs.Add('Location', $Location)
	}

	$ret = Get-AzVM @cmdArgs | Select-Object $Properties
	Write-Output $ret
} catch {
	Write-Error $_
	exit 1
}

The name of the virtual machine to retrieve.

The name of the resource group containing the virtual machine.

The Azure location to filter the virtual machines.

List of properties to include in the output. Use * for all properties.

An interactive directory of PowerShell scripts.