Skip to content

Instantly share code, notes, and snippets.

@roberto-mardeni
Created September 24, 2021 19:11
Show Gist options
  • Save roberto-mardeni/c1988b97777629f7173326334ab5df5a to your computer and use it in GitHub Desktop.
Save roberto-mardeni/c1988b97777629f7173326334ab5df5a to your computer and use it in GitHub Desktop.
Find all Azure Resources that include all tags given
<#
.SYNOPSIS
Find Azure Resources with all the given tags
.DESCRIPTION
Will query for all resources using the given tags and return only those that have all tags specified
Will return all resources found
.PARAMETER Tags
A hashtable of key/value pairs of the tags to query for
.EXAMPLE
./Find-AzResourceByTags -Tags @{ "Application" = "demo"; "Environment" = "dev" }
.NOTES
Author: Roberto Mardeni
Last Updated: 09/24/2021
#>
param
(
# HashTable with all the tags to filter by
[parameter(Mandatory = $true)]
[HashTable] $Tags
)
$ErrorActionPreference = "Stop"
$resources = $null
$Tags.keys | ForEach-Object {
$key = $_
$value = $Tags[$key]
if ($resources -eq $null) {
$resources = Get-AzResource -Tag @{ $_ = $value }
} else {
$resources = $resources | Where-Object { $_.Tags.ContainsKey($key) -and $_.Tags[$key] -eq $value }
}
}
$resources
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment