I'm looking to create a report on all policies and initiatives in my Azure tenant. To do this, I'm using Search-AzGraph in PowerShell to pull from Azure Resource Graph Explorer. I'm running into an issue when I have a query that needs joined using $left or $right. I always get a badrequest error of ParserFailure.
The same query runs flawlessly in azure resource graph explorer. Need to use powershell to automate.
I am running a sample query from Microsoft Azure Resource Graph Explorer:
// Policy assignments and information about each of its respective definitions
// Gets policy assignments in your environment with the respective assignment name, definition associated, category of definition (if applicable), as well as whether the definition type is an initiative or a single policy.
// Click the "Run query" command above to execute the query and see results.
policyResources
| where type =~'Microsoft.Authorization/PolicyAssignments'
| project policyAssignmentId = tolower(tostring(id)), policyAssignmentDisplayName = tostring(properties.displayName), policyAssignmentDefinitionId = tolower(properties.policyDefinitionId)
| join kind=leftouter(
policyResources
| where type =~'Microsoft.Authorization/PolicySetDefinitions' or type =~'Microsoft.Authorization/PolicyDefinitions'
| project definitionId = tolower(id), category = tostring(properties.metadata.category), definitionType = iff(type =~ 'Microsoft.Authorization/PolicysetDefinitions', 'initiative', 'policy')
) on $left.policyAssignmentDefinitionId == $right.definitionId
Error: Powershell Error
I can remove the error by either putting a backtick prior to $left/$right or initializing the variables prior to $left = '$left'. When I do that, my join functions very strangely. The data from PolicyDefinitions is sporadic . I will sometimes get the type and category back and other times not. I would have expected the join to work or not.
using the here-string:
$Policies = Search-AzGraph -Query @'
policyResources
| where type =~'Microsoft.Authorization/PolicyAssignments'
| project policyAssignmentId = tolower(tostring(id)), policyAssignmentDisplayName = tostring(properties.displayName), policyAssignmentDefinitionId = tolower(properties.policyDefinitionId), policyDescription = tostring(properties.description), properties.scope
| join kind=leftouter(
policyResources
| where type =~'Microsoft.Authorization/PolicySetDefinitions' or type =~'Microsoft.Authorization/PolicyDefinitions'
| project definitionId = tolower(id), category = tostring(properties.metadata.category), definitionType = iff(type =~ 'Microsoft.Authorization/PolicysetDefinitions', 'initiative', 'policy')
) on $left.policyAssignmentDefinitionId == $right.definitionId
'@