Skip to content

Instantly share code, notes, and snippets.

@ChendrayanV
Created February 7, 2021 08:59
Show Gist options
  • Save ChendrayanV/9c2d8fae6c6f56820a61e24421f70fca to your computer and use it in GitHub Desktop.
Save ChendrayanV/9c2d8fae6c6f56820a61e24421f70fca to your computer and use it in GitHub Desktop.
Azure Virtual Machines Power State (Resource Graph Query)
$Query = "Resources
| where type =~ 'microsoft.compute/virtualMachines'
| project id, VMName = tostring(name), Location = tostring(location),
ResourceGroup = tostring(resourceGroup),
SubscriptionId = tostring(subscriptionId),
OSType = tostring(properties.storageProfile.osDisk.osType),
PowerState = iff(
tostring(
split(
properties.extended.instanceView.powerState.code, '/'
)[1]
) != '', tostring(split(properties.extended.instanceView.powerState.code, '/')[1]), 'transitioning'
)"
$PageSize = 1000
$Iteration = 0
$SearchParams = @{
Query = $($Query)
First = $PageSize
}
[System.Collections.ArrayList]$Results = @()
do {
$Iteration += 1
$PageResults = Search-AzGraph @searchParams -Verbose
$SearchParams.Skip += $pageResults.Count
$Results.AddRange($pageResults)
} while ($PageResults.Count -eq $PageSize)
$Results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment