I have an Azure Automation Account. In my Runbook, I want to read the properties of a certain Policy Definition. Unfortunately, when the AA runs the PowerShell Runbook, it can only read the definition name, not its description or policy rule. My Automation Account ID has Reader on the management group. The script sees that there is an object, but only the name comes back with any value.
Please let me know how I can read properties of a policy definition like DisplayName and policyRule. Thanks.
I wrote this script to illustrate the problem. See the output below:
Connect-AzAccount -Identity | Out-Null
$policyDefs = Get-AzPolicyDefinition | Select-Object -First 3
if ($null -eq $policyDefs) {
Write-Error "Policy definition named '$policyName' was not found."
return $null
}
foreach ($policyDef in $policyDefs) {
$propCount = ($policyDef.PsObject.Properties | Measure-Object).count
Write-Output ("Property count: " + $propCount)
Write-Output ("ID: " + $policyDef.Id)
Write-Output ("name: " + $policyDef.Name)
Write-Output ("displayname: " + $policyDef.DisplayName)
Write-Output ("Policy Type: " + $policyDef.PolicyType)
Write-Output ("Policy if: " + $policyDef.PolicyRule.if)
Write-Output ("Policy then: " + $policyDef.PolicyRule.then)
Write-Output ("Policy if count: " + $policyDef.PolicyRule.if.count)
Write-Output ("Policy if equals: " + $policyDef.PolicyRule.if.equals)
$policyDef.PolicyRule.if.count.value | ForEach-Object {
Write-Output ("Policy if count value: " + $_)
}
}
6/23/2025, 12:09:46 PM - Output: Property count: 7
6/23/2025, 12:09:46 PM - Output: ID:
6/23/2025, 12:09:46 PM - Output: name: 0004bbf0-5099-4179-869e-e9ffe5fb0945
6/23/2025, 12:09:46 PM - Output: displayname:
6/23/2025, 12:09:47 PM - Output: Policy Type:
6/23/2025, 12:09:47 PM - Output: Policy if:
6/23/2025, 12:09:47 PM - Output: Policy then:
6/23/2025, 12:09:47 PM - Output: Policy if count: 0
6/23/2025, 12:09:47 PM - Output: Policy if equals:
6/23/2025, 12:09:47 PM - Output: Policy if count value:
6/23/2025, 12:09:47 PM - Output: Property count: 7
6/23/2025, 12:09:47 PM - Output: ID:
6/23/2025, 12:09:47 PM - Output: name: 0015ea4d-51ff-4ce3-8d8c-f3f8f0179a56
6/23/2025, 12:09:47 PM - Output: displayname:
6/23/2025, 12:09:47 PM - Output: Policy Type:
6/23/2025, 12:09:47 PM - Output: Policy if:
6/23/2025, 12:09:47 PM - Output: Policy then:
6/23/2025, 12:09:47 PM - Output: Policy if count: 0
6/23/2025, 12:09:47 PM - Output: Policy if equals:
6/23/2025, 12:09:47 PM - Output: Policy if count value:
6/23/2025, 12:09:47 PM - Output: Property count: 7
6/23/2025, 12:09:47 PM - Output: ID:
6/23/2025, 12:09:47 PM - Output: name: 001802d1-4969-4c82-a700-c29c6c6f9bbd
6/23/2025, 12:09:47 PM - Output: displayname:
6/23/2025, 12:09:47 PM - Output: Policy Type:
6/23/2025, 12:09:47 PM - Output: Policy if:
6/23/2025, 12:09:47 PM - Output: Policy then:
6/23/2025, 12:09:47 PM - Output: Policy if count: 0
6/23/2025, 12:09:47 PM - Output: Policy if equals:
6/23/2025, 12:09:47 PM - Output: Policy if count value: