Skip to content

Instantly share code, notes, and snippets.

@irlperu
Created April 30, 2019 14:05
Show Gist options
  • Save irlperu/20d3f2c3ab19702c33160929a7b24aa7 to your computer and use it in GitHub Desktop.
Save irlperu/20d3f2c3ab19702c33160929a7b24aa7 to your computer and use it in GitHub Desktop.
Search Azure at Scale
# Querying Azure at scale --
# Make sure to install this module - there is currently only one command available inside this module - 'search'
# The advantage of using this module is that is will query across subscriptions without needing to define them
#Install-Module -Name Az.ResourceGraph
#Get-Command -Module Az.ResourceGraph
#Login-AzAccount -UseDeviceAuthentication
#$SubscriptionId = (Get-AzureRmSubscription | select Name, State, SubscriptionId, TenantId | Out-GridView -Title "Azure Subscription Selector" -PassThru).SubscriptionId
#Get-AzureRmSubscription -SubscriptionId $SubscriptionId | Select-AzureRmSubscription
# Show all virtual machines ordered by name in descending order
Search-AzGraph -Query "project name, location, type| where type =~ 'Microsoft.Compute/virtualMachines' | order by name desc" | Format-Table -AutoSize
# Show first five virtual machines by name and their OS type
Search-AzGraph -Query "where type =~ 'Microsoft.Compute/virtualMachines' | project name, properties.storageProfile.osDisk.osType | top 5 by name desc"
# Show all Virtual Machines
Search-AzGraph -Query "where type =~ 'Microsoft.Compute/virtualMachines' | project name, properties.storageProfile.osDisk.osType"
# Count virtual machines by OS type
Search-AzGraph -Query "where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.storageProfile.osDisk.osType)"
# Show resources that contain storage
Search-AzGraph -Query "where type contains 'storage' | distinct type"
# List all public IP addresses
Search-AzGraph -Query "where type contains 'publicIPAddresses' and properties.ipAddress != '' | project properties.ipAddress | limit 100"
# Count resources that have IP addresses configured by subscription
Search-AzGraph -Query "where type contains 'publicIPAddresses' and properties.ipAddress != '' | summarize count () by subscriptionId"
# Get virtual machine scale set capacity and size
Search-AzGraph -Query "where type=~ 'microsoft.compute/virtualmachinescalesets' | where name contains 'contoso' | project subscriptionId, name, location, resourceGroup, Capacity = toint(sku.capacity), Tier = sku.name | order by Capacity desc"
# List all tag names
Search-AzGraph -Query "project tags | summarize buildschema(tags)" | ConvertTo-Json
# Virtual machines matched by regex -- setting 100 below means it expands out all properties
Search-AzGraph -Query "where type =~ 'Microsoft.Compute/virtualMachines' | limit 1" | ConvertTo-Json -Depth 100
# Virtual machines by location
Search-AzGraph -Query "where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by location"
# Virtual machines by SKU -- Change Server from Standard_B2s to whatever series you want!
Search-AzGraph -Query "where type =~ 'Microsoft.Compute/virtualMachines' and properties.hardwareProfile.vmSize == 'Standard_B2s' | project name, resourceGroup"
# Virtual machines connected to premium-managed disks
Search-AzGraph -Query "where type =~ 'Microsoft.Compute/virtualmachines' | extend disk = properties.storageProfile.osDisk.managedDisk | where disk.storageAccountType == 'Premium_LRS' | project disk.id"
# Total of all Azure Resources
Search-AzGraph -Query "summarize count()"
# Summarize by Location
Search-AzGraph -Query "summarize count () by location"
# Count Resources by Type
Search-AzGraph -Query "summarize count () by type"
# Total of VM's
Search-AzGraph -Query "where type =~ 'Microsoft.Compute/virtualMachines' | summarize count()" | Format-Table -AutoSize
# Total by OS
search-AzGraph -Query "where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.storageProfile.osDisk.osType) | project OS=properties_storageProfile_osDisk_osType, total=count_"
# Total by SKU
search-AzGraph -Query "where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by tostring(properties.hardwareProfile.vmSize)| project SKU=properties_hardwareProfile_vmSize, total=count_" | Format-Table -AutoSize
# Get Machines by Regex
search-AzGraph -Query "where type =~ 'Microsoft.Compute/virtualmachines' and properties.hardwareProfile.vmSize matches regex 'Standard_DS[0-9]*_v2'| project name"
# Number of VM's by Location i.e. East US2, West US, Central etc
Search-AzGraph "project id, name, type, location | where type =~ 'Microsoft.Compute/virtualMachines' | summarize count() by location | top 3 by count_"
Search-AzGraph "project id, name, type, location, tags" -First 5
# Get a count of all Websites
Search-AzGraph -Query "where type =~ 'Microsoft.Web/sites'" -First 5000
# Display all storage accounts that have the option to “Allow Access from all networks”
search-AzGraph -Query "where type =~ 'microsoft.storage/storageaccounts' | where aliases['Microsoft.Storage/storageAccounts/networkAcls.defaultAction'] =='Allow'| summarize count()"
# Display linux VMs with OS version 16.04 -- Change out the Linux version at the end witht the version you are looking for!
search-AzGraph -Query “where type =~ 'microsoft.compute/virtualmachines'| project OS = tostring(aliases['Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType']) , OsVer = tostring(properties.storageProfile.imageReference.version) | where OS == 'Linux' and OsVer startswith '16.04' | summarize count()"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment