Querying Azure PowerShell Output for DevOps & SRE (2025)
Azure PowerShell is a core tool for DevOps and SREs managing Azure infrastructure. Efficiently querying and filtering output is essential for automation, reporting, and troubleshooting.
Selecting Properties
Use Select-Object to choose which properties to display. This is useful for scripting, dashboards, and CI/CD pipelines.
Get-AzVM -Name TestVM -ResourceGroupName TestGroup |
Select-Object -Property *
Select specific properties for concise output:
Get-AzVM -Name TestVM -ResourceGroupName TestGroup |
Select-Object -Property Name, VmId, ProvisioningState
Selecting Nested Properties
You can extract nested properties using calculated expressions:
Get-AzVM -ResourceGroupName TestGroup |
Select-Object -Property Name, @{label='OSType'; expression={$_.StorageProfile.OSDisk.OSType}}
Filtering Results
Use Where-Object to filter objects based on property values. This is critical for automation and compliance checks.
Get-AzVM -ResourceGroupName TestGroup |
Where-Object {$_.StorageProfile.OsDisk.OsType -eq 'Linux'} |
Select-Object -Property Name, VmID, ProvisioningState
Output as JSON or CSV
For integration with other tools or pipelines, output as JSON or CSV:
Get-AzVM -ResourceGroupName TestGroup |
Select-Object Name, VmId, ProvisioningState |
ConvertTo-Json | Out-File vms.json
Get-AzVM -ResourceGroupName TestGroup |
Select-Object Name, VmId, ProvisioningState |
Export-Csv -Path vms.csv -NoTypeInformation
Best Practices (2025)
- Always filter and select only the properties you need for performance and clarity.
- Use
ConvertTo-Jsonfor API integrations and automation. - Use
Export-Csvfor reporting and audits. - Leverage calculated properties for nested or custom data.
- Use
-ExpandPropertywithSelect-Objectto flatten output when needed. - For large datasets, consider using
-Firstor-Lastto limit results.
Common Pitfalls
- Forgetting to filter can result in large, unwieldy outputs.
- Not handling nested properties can lead to missing critical data.
- Always check for nulls in nested properties to avoid errors in scripts.
References
Powershell Joke: Why did the DevOps engineer refuse to use PowerShell aliases? Because they wanted to avoid unnecessary shortcuts in life!