无法在PowerShell中使用Azure命令删除Azure策略分配

hkmswyz6  于 2023-11-21  发布在  Shell
关注(0)|答案(1)|浏览(162)

我在这个问题上卡住了2天,但我似乎无法使用Azure powershell命令或az命令删除策略日志。
所有命令都成功执行,我能够列出所有分配。az delete命令也成功执行,但不会删除分配,因为它们仍然存在于Azure门户策略中。
尝试以下PowerShell脚本:

$ResourceGroup = "PoliciesTest"
$query = "assignment"

# List policy assignments and extract the name and id
$assignments = az policy assignment list --resource-group $ResourceGroup --output json --only-show-errors | ConvertFrom-Json | Where-Object { $_.name -like "*$query*" }

# Iterate through the assignments and display name and id
foreach ($assignment in $assignments) {
    Write-Host "Name: $($assignment.name)"
    Write-Host "ID: $($assignment.id)"
    Write-Host "--------------------"
    az policy assignment delete --name $assignment.name --scope $assignment.id  --debug

}

字符串
result of executing the script
有一个关于堆栈溢出的问题也有同样的问题,但是他的问题通过给出范围得到了解决。他的解决方案对我不起作用。

xpcnnkqh

xpcnnkqh1#

无法在Powershell中使用Azure命令删除Azure策略分配
如果在尝试删除Azure Policy分配时收到204响应,则门户可能需要一些时间才能将其从显示中删除。要进行验证,请运行$assignments变量。
在运行PowerShell代码之前,我已在门户中使用venkat名称分配了以下策略。


的数据

$scope = "/subscriptions/b83c1ed3-xxxxfjgjgj-b5ba-2bfhn4n5n6n"
$query = "venkat"
$assignments = az policy assignment list --scope $scope --output json --only-show-errors | ConvertFrom-Json | Where-Object { $_.displayName -like "*$query*" }

foreach ($assignment in $assignments) {
$name= $assignment.name
Write-Host "Name: $($name)"
$scopeid = $assignment.id
Write-Host "ID: $($scopeid)"
Write-Host "--------------------"
az policy assignment delete --name $name   
}

字符串

回复:



运行上述代码后,策略分配已从门户中成功删除。



此脚本将删除策略分配的作用域:Resource group

$scope = "/subscriptions/b83xxxxx-xxxxx-44fb-xxxx-2b83aabcdgfc23f/resourceGroups/venkat"
$query = "allowed"
$assignments = az policy assignment list --scope $scope --output json --only-show-errors | ConvertFrom-Json | Where-Object { $_.displayName -like "*$query*" }
foreach ($assignment in $assignments) {
$name= $assignment.name
Write-Host "Name: $($name)"
$resourcegroup = $assignment.resourceGroup
Write-Host "Resource Group: $($resourcegroup)"
Write-Host "--------------------"
az policy assignment delete --name $name --resource-group $resourcegroup
}

相关问题