CodexBloom - Programming Q&A Platform

PowerShell 7.3 - implementing Retrieving Azure VM Properties Using Az Module and Custom Filters

👀 Views: 15 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-08
PowerShell Azure AzModule VMs Filtering

I'm attempting to set up I can't seem to get I'm working with an scenario with retrieving specific properties of Azure VMs using the Az module in PowerShell 7.3... I want to filter the VMs based on their tags and display only selected properties like Name, ResourceGroup, and Location. However, when I run my script, I'm getting an empty result set even though I know the VMs exist with the specified tags. Here's what I have tried: ```powershell # Import the Az module Import-Module Az # Connect to Azure account Connect-AzAccount # Define the tag filter $tagFilter = @{ 'Environment' = 'Production' } # Fetch VMs with the tag filter $vms = Get-AzVM | Where-Object { $_.Tags['Environment'] -eq $tagFilter['Environment'] } # Select specific properties $results = $vms | Select-Object Name, ResourceGroupName, Location # Display the results $results ``` When I run this code, the `$results` variable is empty. If I run `Get-AzVM` without any filters, I can see all the VMs, including the ones tagged with `Environment: Production`. I've also tried different variations like directly using `Where-Object` and checking for the tag count, but nothing seems to work. I also tried using `Get-AzVM -Tag @{ 'Environment' = 'Production' }` for filtering directly in the cmdlet, but it returns all VMs instead of filtering based on tags. Am I missing something in my filtering logic, or is this a limitation of the Az module in PowerShell 7.3? Any insights would be greatly appreciated! This issue appeared after updating to Powershell 3.11. What are your experiences with this?