Querying data needs always filtering capabilities. ARG, has a powerful query language that allows complex queries with just a few characters of typework. Knowing how to filter data is importtant, so that you can use the power of the ARG engine before you retrieve the data to your session (PowerShell in that case), especially with large datasets. This article explains the filter options provided by ARG.
The ‚where‘ statement has nine main operators to work with.
List of main ‚where‘ operators:
- = – equals
- has – whole term
- hasprefix – whole term has prefix
- hassuffix – whole term has suffix
- contains – string contains
- startswith – string starts with
- endswith – string ends with
- matches regex
- in – string array contains string element
When we combine the operators with teh case sensitive and negation options, we can see a lot of options to filter data. See below.
string operator | case insensitive | negated | case insensitive and negated |
= | =~ | != | !~ |
has | has_cs | !has | !has_cs |
hasprefix | hasprefix_cs | !hasprefix | !hasprefix_cs |
hassuffix | hassuffix_cs | !hassuffix | !hassuffix_cs |
contains | contains_cs | !contains | !contains_cs |
startswith | startswith_cs | !startswith | !startswith_cs |
endswith | endswith_cs | !endswith | !endswith_cs |
matchregex | — | — | — |
in | — | !in | — |
This leads to 31(!) possibilities, now lets start with a query of all resource tags in the database
1 2 |
$q = "project name,tags" Search-AzGraph -Query $q |
The output is a list of objects this (name,tags):
1 |
SampleWinBox @{CostCenter=ABC; Department=Cleaning; PCC-DoNotSync=False; SetbyMG=TRUE} |
Now lets play around with the „where“ statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# All the below queries are stored in the $q variable, just start them with: Search-AzGraph -Query $q #------------------------------------------------# # Filter where the resource Tag value contains 'ABC' (not case sensitive) $q = "project name,tags|where tags contains 'abc'" # Filter where the resource Tag value contains 'aBc' (case sensitive) $q = "project name,tags|where tags contains_cs 'aBc'" # Filter not containing 'CostCenter' case sensitive - so all resources where this tag is not set. $q = "project name,tags|where tags !contains 'CostCenter'" # Show all hidden resource tags (Set by Azure automatically, not visible to users in the Portal) $q = "project name,tags|where tags contains 'hidden'" # --- Examples with filtering the name --- # Filter all resources starting with 'Sample' $q = "project name,tags|where name startswith 'Sample'" # Combine name and type filter to get all VM´s containing 'Sample' in the name $q = "project name,type|where name contains 'Sample' and type contains 'virtualmachine'" |
I hope you got enough examples and info sto start on your own!
Happy experimenting !
R.