triggering remediation tasks in Azure policy programatically and getting the display name

I have written a script that will automatically rigger policy remediation, the script below does that.

$InitiativeAssignmentName = "xxxx"
   
# Loop through each policy and create individual remediation tasks
foreach ($policy in $RemediatablePolicies) {
       $remediationName = "rem." + $policy.PolicyDefinitionReferenceId
       write-output "Starting the remediation with label ==> $remediationName at $(Get-Date)"
    if (!([string]::IsNullOrEmpty($policy.PolicyDefinitionReferenceId)))
    {
       Start-AzPolicyRemediation -Name $remediationName -PolicyAssignmentId $policy.PolicyAssignmentId -PolicyDefinitionReferenceId $policy.PolicyDefinitionReferenceId -ResourceDiscoveryMode ReEvaluateCompliance
   }
}

However the issue is that when I look at the Azure portal, I can see descriptive policy definition such as the following.

Configure diagnostic settings for Storage Accounts to Log Analytics workspace

Deploy Diagnostic Settings for Key Vault to Log Analytics workspace

When I look at the script above, it runs based on a PolicyAssignmentName which is essentially a GUID and difficult to read/make sense of, what I would like is to provide a list of descriptive policy definition rather than the PolicyAssignmentName, I tried to retrieve the name as displayed on the portal without much luck using the code below.

Get-AzPolicyState | Where-Object { $_.PolicyAssignmentName -eq $InitiativeAssignmentName -and ($_.PolicyDefinitionAction -eq "deployIfNotExists" -or $_.PolicyDefinitionAction -eq "modify" -or $_.PolicyDefinitionAction -eq "append") } | Select-Object -ExpandProperty AdditionalProperties | select-object * | Format-Table

or even

Get-AzPolicyState | Where-Object { $_.PolicyAssignmentName -eq $InitiativeAssignmentName -and ($_.PolicyDefinitionAction -eq "deployIfNotExists" -or $_.PolicyDefinitionAction -eq "modify" -or $_.PolicyDefinitionAction -eq "append") } | select-object PolicyDefinitionReferenceId, PolicyAssignmentId,PolicyDefinitionName -Unique

what am I trying to achieve?

Provide a descriptive list of policy names and the code can loop through and create remediation tasks. Using PolicyAssignmentName has a one to many relationship, and as such providing the value PolicyAssignmentName can lead to the creation of a lot remediation tasks, rather than specific ones.

Recognized by Microsoft Azure Collective

Here is the updated code to display the Policy Definition Name, Policy Definition Reference ID, Policy Assignment ID, and trigger the remediation using the names.

$policyassignmentNames = @("Storage enable Policy", "Network Security Group rule deny", "NSG_Rule_Deny", "JIT Port deny", "Azure Policy to modify Log analytics workspace destination of a data collection rule","JIM_Port_3389 and 22 Restricted")

$policyAssignments = Get-AzPolicyAssignment -BackwardCompatible | Where-Object { $policyassignmentNames -contains $_.DisplayName }

foreach ($policy in $policyAssignments) {
   
    write-output "Starting the remediation with label ==> $($policy.DisplayName) at $(Get-Date)"

    # Get the policy definition details for each assignment

    $policyDefinitions = Get-AzPolicyDefinition -BackwardCompatible -Id $policy.PolicyDefinitionId -WarningAction silentlyContinue


    $existingRemediations = Get-AzPolicyRemediation -WarningAction silentlyContinue | Where-Object { $_.PolicyAssignmentId -eq $policy.PolicyAssignmentId } 

    if ($existingRemediations) {
        Write-Output "Remediation already in progress for $($policy.DisplayName). Skipping..."
        continue
    }

   
    $remediationName = "rem." + $policyDefinitions.DisplayName

    if (!([string]::IsNullOrEmpty($policyDefinitions.DisplayName))) {
        try {
            Start-AzPolicyRemediation -Name $remediationName -PolicyAssignmentId $policy.PolicyAssignmentId -PolicyDefinitionReferenceId $policyDefinitions.PolicyDefinitionId -ResourceDiscoveryMode ReEvaluateCompliance
        } catch {
            Write-Output "Failed to start remediation for $($policy.DisplayName): $_"
        }
    }
}

Output:

Recognized by Microsoft Azure Collective

8

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật