共用方式為


Update-AzPolicyAssignment

此作業會以指定的範圍和名稱更新原則指派。 原則指派會套用至其範圍內所包含的所有資源。 例如,當您在資源群組範圍指派原則時,該原則會套用至群組中的所有資源。

語法

Update-AzPolicyAssignment
      -Name <String>
      [-Scope <String>]
      [-NotScope <String[]>]
      [-DisplayName <String>]
      [-Description <String>]
      [-Metadata <String>]
      [-Location <String>]
      [-EnforcementMode <String>]
      [-IdentityType <String>]
      [-IdentityId <String>]
      [-NonComplianceMessage <PSObject[]>]
      [-Override <IOverride[]>]
      [-ResourceSelector <IResourceSelector[]>]
      [-BackwardCompatible]
      [-DefaultProfile <PSObject>]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]
Update-AzPolicyAssignment
      -Name <String>
      [-Scope <String>]
      [-NotScope <String[]>]
      [-DisplayName <String>]
      [-Description <String>]
      [-Metadata <String>]
      [-Location <String>]
      [-EnforcementMode <String>]
      [-IdentityType <String>]
      [-IdentityId <String>]
      [-NonComplianceMessage <PSObject[]>]
      [-Override <IOverride[]>]
      [-ResourceSelector <IResourceSelector[]>]
      [-BackwardCompatible]
      -PolicyParameterObject <PSObject>
      [-DefaultProfile <PSObject>]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]
Update-AzPolicyAssignment
      -Name <String>
      [-Scope <String>]
      [-NotScope <String[]>]
      [-DisplayName <String>]
      [-Description <String>]
      [-Metadata <String>]
      [-Location <String>]
      [-EnforcementMode <String>]
      [-IdentityType <String>]
      [-IdentityId <String>]
      [-NonComplianceMessage <PSObject[]>]
      [-Override <IOverride[]>]
      [-ResourceSelector <IResourceSelector[]>]
      [-BackwardCompatible]
      -PolicyParameter <String>
      [-DefaultProfile <PSObject>]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]
Update-AzPolicyAssignment
      -Id <String>
      [-NotScope <String[]>]
      [-DisplayName <String>]
      [-Description <String>]
      [-Metadata <String>]
      [-Location <String>]
      [-EnforcementMode <String>]
      [-IdentityType <String>]
      [-IdentityId <String>]
      [-NonComplianceMessage <PSObject[]>]
      [-Override <IOverride[]>]
      [-ResourceSelector <IResourceSelector[]>]
      [-BackwardCompatible]
      [-DefaultProfile <PSObject>]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]
Update-AzPolicyAssignment
      -Id <String>
      [-NotScope <String[]>]
      [-DisplayName <String>]
      [-Description <String>]
      [-Metadata <String>]
      [-Location <String>]
      [-EnforcementMode <String>]
      [-IdentityType <String>]
      [-IdentityId <String>]
      [-NonComplianceMessage <PSObject[]>]
      [-Override <IOverride[]>]
      [-ResourceSelector <IResourceSelector[]>]
      [-BackwardCompatible]
      -PolicyParameterObject <PSObject>
      [-DefaultProfile <PSObject>]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]
Update-AzPolicyAssignment
      -Id <String>
      [-NotScope <String[]>]
      [-DisplayName <String>]
      [-Description <String>]
      [-Metadata <String>]
      [-Location <String>]
      [-EnforcementMode <String>]
      [-IdentityType <String>]
      [-IdentityId <String>]
      [-NonComplianceMessage <PSObject[]>]
      [-Override <IOverride[]>]
      [-ResourceSelector <IResourceSelector[]>]
      [-BackwardCompatible]
      -PolicyParameter <String>
      [-DefaultProfile <PSObject>]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]
Update-AzPolicyAssignment
      [-NotScope <String[]>]
      [-DisplayName <String>]
      [-Description <String>]
      [-Metadata <String>]
      [-Location <String>]
      [-EnforcementMode <String>]
      [-IdentityType <String>]
      [-IdentityId <String>]
      [-NonComplianceMessage <PSObject[]>]
      [-Override <IOverride[]>]
      [-ResourceSelector <IResourceSelector[]>]
      [-BackwardCompatible]
      -InputObject <IPolicyAssignment>
      [-DefaultProfile <PSObject>]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]

Description

此作業會以指定的範圍和名稱更新原則指派。 原則指派會套用至其範圍內所包含的所有資源。 例如,當您在資源群組範圍指派原則時,該原則會套用至群組中的所有資源。

範例

範例 1:更新顯示名稱

$ResourceGroup = Get-AzResourceGroup -Name 'ResourceGroup11'
$PolicyAssignment = Get-AzPolicyAssignment -Name 'PolicyAssignment' -Scope $ResourceGroup.ResourceId
Update-AzPolicyAssignment -Id $PolicyAssignment.Id -DisplayName 'Do not allow VM creation'

第一個命令會使用 Get-AzResourceGroup Cmdlet 來取得名為 ResourceGroup11 的資源群組。 命令會將該物件儲存在 $ResourceGroup 變數中。 第二個命令會使用 Get-AzPolicyAssignment Cmdlet 來取得名為 PolicyAssignment 的原則指派。 命令會將該物件儲存在$PolicyAssignment變數中。 最後一個命令會更新 ResourceId $ResourceGroup 屬性所識別之資源群組上的原則指派顯示名稱。

範例 2:將系統指派的受控識別新增至原則指派

$PolicyAssignment = Get-AzPolicyAssignment -Name 'PolicyAssignment'
Update-AzPolicyAssignment -Id $PolicyAssignment.Id -IdentityType 'SystemAssigned' -Location 'westus'

第一個命令會使用 Get-AzPolicyAssignment Cmdlet,從目前的訂用帳戶取得名為 PolicyAssignment 的原則指派。 命令會將該物件儲存在$PolicyAssignment變數中。 最後一個命令會將系統指派的受控識別指派給原則指派。

範例 3:將使用者指派的受控識別新增至原則指派

$PolicyAssignment = Get-AzPolicyAssignment -Name 'PolicyAssignment'
$UserAssignedIdentity = Get-AzUserAssignedIdentity -ResourceGroupName 'ResourceGroup1' -Name 'UserAssignedIdentity1'
 Update-AzPolicyAssignment -Id $PolicyAssignment.Id -IdentityType 'UserAssigned' -Location 'westus' -IdentityId $UserAssignedIdentity.Id

第一個命令會使用 Get-AzPolicyAssignment Cmdlet,從目前的訂用帳戶取得名為 PolicyAssignment 的原則指派。 命令會將該物件儲存在$PolicyAssignment變數中。 第二個命令會使用 Get-AzUserAssignedIdentity Cmdlet 取得名為 UserAssignedIdentity1 的使用者指派受控識別,並將它儲存在$UserAssignedIdentity變數中。 最後一個命令會將$UserAssignedIdentity 標識符 屬性所識別的使用者指派受控識別指派給原則指派。

範例 4:使用新的原則參數物件更新原則指派參數

$Locations = Get-AzLocation | Where-Object {($_.displayname -like 'france*') -or ($_.displayname -like 'uk*')}
$AllowedLocations = @{'listOfAllowedLocations'=($Locations.location)}
$PolicyAssignment = Get-AzPolicyAssignment -Name 'PolicyAssignment'
Update-AzPolicyAssignment -Id $PolicyAssignment.Id -PolicyParameterObject $AllowedLocations

第一個和第二個命令會建立物件,其中包含名稱開頭為 “france” 或 “uk” 的所有 Azure 區域。 第二個命令會將該物件儲存在 $AllowedLocations 變數中。 第三個命令會取得名為 『PolicyAssignment』 的原則指派:命令會將該物件儲存在$PolicyAssignment變數中。 最後一個命令會更新原則指派上名為 PolicyAssignment 的參數值。

範例 5:使用原則參數檔案更新原則指派參數

{
  "listOfAllowedLocations":  {
    "value": [
      "uksouth",
      "ukwest",
      "francecentral",
      "francesouth"
    ]
  }
}

Update-AzPolicyAssignment -Name 'PolicyAssignment' -PolicyParameter .\AllowedLocations.json

此命令會使用從本機工作目錄 AllowedLocations.json 的原則參數檔案,更新名為 『PolicyAssignment』 的原則指派。

範例 6:更新 enforcementMode

$ResourceGroup = Get-AzResourceGroup -Name 'ResourceGroup11'
$PolicyAssignment = Get-AzPolicyAssignment -Name 'PolicyAssignment' -Scope $ResourceGroup.ResourceId
Update-AzPolicyAssignment -Id $PolicyAssignment.Id -EnforcementMode Default

第一個命令會使用 Get-AzResourceGroup Cmdlet 來取得名為 ResourceGroup11 的資源群組。 命令會將該物件儲存在 $ResourceGroup 變數中。 第二個命令會使用 Get-AzPolicyAssignment Cmdlet 來取得名為 PolicyAssignment 的原則指派。 命令會將該物件儲存在$PolicyAssignment變數中。 最後一個命令會更新 ResourceId 屬性所識別之資源群組上的原則指派上的 enforcementMode 屬性$ResourceGroup。

範例 7:更新不符合規範的訊息

$PolicyAssignment = Get-AzPolicyAssignment -Name 'VirtualMachinePolicy'
Update-AzPolicyAssignment -Id $PolicyAssignment.Id -NonComplianceMessage @{Message="All resources must follow resource naming guidelines."}

第一個命令會使用 Get-AzPolicyAssignment Cmdlet 來取得名為 VirtualMachinePolicy 的原則指派,並將它儲存在$PolicyAssignment變數中。 最後一個命令會以原則拒絕資源時顯示的新訊息,更新原則指派上的不符合規範訊息。

範例 8:更新資源選取器

$ResourceSelector = @{Name = "MyLocationSelector"; Selector = @(@{Kind = "resourceLocation"; NotIn = @("eastus", "eastus2")})}
Update-AzPolicyAssignment -Name 'VirtualMachinePolicyAssignment' -ResourceSelector $ResourceSelector

第一個命令會建立資源選取器物件,該物件將用來指定工作分派應該只套用至位於美國東部或美國東部 2 的資源,並將它儲存在$ResourceSelector變數中。 最後一個命令會使用$ResourceSelector所指定的資源選取器,更新名為 VirtualMachinePolicyAssignment 的原則指派。

範例 9:更新覆寫

$Selector = @{Kind = "resourceLocation"; NotIn = @("eastus", "eastus2")}
$Override = @(@{Kind = "policyEffect"; Value = 'Disabled'; Selector = @($Selector)})
Update-AzPolicyAssignment -Name 'VirtualMachinePolicyAssignment' -Override $Override

第一個命令會建立位置選取器,指定美國東部或美國東部 2 以外的位置,並將儲存在 $Selector 變數中。 第二個命令會建立覆寫物件,用來指定指派的定義應該在$Selector所識別的位置產生 Disabled 效果。 最後一個命令會使用$Override所指定的覆寫來更新名為 VirtualMachinePolicyAssignment 的原則指派。

範例 10:[Backcompat] 更新 enforcementMode

$ResourceGroup = Get-AzResourceGroup -Name 'ResourceGroup11'
$PolicyAssignment = Get-AzPolicyAssignment -Name 'PolicyAssignment' -Scope $ResourceGroup.ResourceId -BackwardCompatible
Set-AzPolicyAssignment -Id $PolicyAssignment.ResourceId -EnforcementMode Default

第一個命令會使用 Get-AzResourceGroup Cmdlet 來取得名為 ResourceGroup11 的資源群組。 命令會將該物件儲存在 $ResourceGroup 變數中。 第二個命令會使用 Get-AzPolicyAssignment Cmdlet 來取得名為 PolicyAssignment 的原則指派。 命令會將該物件儲存在$PolicyAssignment變數中。 最後一個命令會更新 ResourceId 屬性所識別之資源群組上的原則指派上的 enforcementMode 屬性$ResourceGroup。

參數

-BackwardCompatible

導致 Cmdlet 使用舊版格式將原則特定屬性放在屬性包對象中傳回成品。

類型:SwitchParameter
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-Confirm

在執行 Cmdlet 之前,提示您進行確認。

類型:SwitchParameter
別名:cf
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-DefaultProfile

DefaultProfile 參數無法運作。 如果針對不同的訂用帳戶執行 Cmdlet,請使用 SubscriptionId 參數。

類型:PSObject
別名:AzureRMContext, AzureCredential
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-Description

如果違反原則,此訊息將會是回應的一部分。

類型:String
Position:Named
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-DisplayName

原則指派的顯示名稱。

類型:String
Position:Named
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-EnforcementMode

原則指派強制模式。 可能的值為 Default 和 DoNotEnforce。

類型:String
Position:Named
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-Id

要更新的原則指派標識碼。 使用格式 '{scope}/providers/Microsoft.Authorization/policyAssignments/{policyAssignmentName}'。

類型:String
別名:ResourceId, PolicyAssignmentId
Position:Named
預設值:None
必要:True
接受管線輸入:True
接受萬用字元:False

-IdentityId

與原則相關聯的使用者身分識別。 使用者身分識別字典索引鍵參考的格式為 ARM 資源標識符:'/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}'。

類型:String
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-IdentityType

識別類型。 將系統或使用者指派的身分識別新增至資源時,這是唯一的必要字段。

類型:String
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-InputObject

類型:IPolicyAssignment
Position:Named
預設值:None
必要:True
接受管線輸入:True
接受萬用字元:False

-Location

原則指派的位置。 只有在使用受控識別時才需要。

類型:String
Position:Named
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-Metadata

原則指派元數據。 元數據是開放式物件,通常是索引鍵值組的集合。

類型:String
Position:Named
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-Name

原則指派的名稱。

類型:String
別名:PolicyAssignmentName
Position:Named
預設值:None
必要:True
接受管線輸入:True
接受萬用字元:False

-NonComplianceMessage

描述資源不符合原則之原因的訊息。 若要建構,請參閱 NONCOMPLIANCEMESSAGE 屬性的 NOTES 區段並建立哈希表。

類型:PSObject[]
Position:Named
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-NotScope

原則的排除範圍。

類型:String[]
Position:Named
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-Override

原則屬性值覆寫。

類型:IOverride[]
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-PolicyParameter

指派原則規則的參數值。 索引鍵是參數名稱。

類型:String
Position:Named
預設值:None
必要:True
接受管線輸入:True
接受萬用字元:False

-PolicyParameterObject

指派原則規則的參數值。 索引鍵是參數名稱。

類型:PSObject
Position:Named
預設值:None
必要:True
接受管線輸入:False
接受萬用字元:False

-ResourceSelector

依資源屬性篩選原則的資源選取器清單。

類型:IResourceSelector[]
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-Scope

原則指派的範圍。 有效範圍包括:管理群組(格式:'/providers/Microsoft.Management/managementGroups/{managementGroup}'),訂用帳戶(格式:'/subscriptions/{subscriptionId}'),資源群組(格式: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}', 或資源 (格式: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/[{parentResourcePath}/]{resourceType}/{resourceName}'

類型:String
Position:Named
預設值:None
必要:False
接受管線輸入:True
接受萬用字元:False

-WhatIf

顯示 Cmdlet 執行時會發生什麼事。 Cmdlet 未執行。

類型:SwitchParameter
別名:wi
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

輸入

String[]

輸出

備註

別名

Set-AzPolicyAssignment