共用方式為


New-AzureRmPolicyAssignment

建立原則指派。

警告

自 2024 年 2 月 29 日起,AzureRM PowerShell 模組已正式淘汰。 建議使用者從 AzureRM 遷移至 Az PowerShell 模組,以確保持續支援和更新。

雖然 AzureRM 模組可能仍可運作,但不再維護或支援它,但會根據用戶的判斷權和風險放置任何繼續使用。 如需轉換至 Az 模組的指引,請參閱我們的 移轉資源

語法

New-AzureRmPolicyAssignment
   -Name <String>
   -Scope <String>
   [-NotScope <String[]>]
   [-DisplayName <String>]
   [-Description <String>]
   [-PolicyDefinition <PSObject>]
   [-PolicySetDefinition <PSObject>]
   [-Metadata <String>]
   [-Sku <Hashtable>]
   [-AssignIdentity]
   [-Location <String>]
   [-ApiVersion <String>]
   [-Pre]
   [-DefaultProfile <IAzureContextContainer>]
   [-InformationAction <ActionPreference>]
   [-InformationVariable <String>]
   [<CommonParameters>]
New-AzureRmPolicyAssignment
   -Name <String>
   -Scope <String>
   [-NotScope <String[]>]
   [-DisplayName <String>]
   [-Description <String>]
   -PolicyDefinition <PSObject>
   [-PolicySetDefinition <PSObject>]
   -PolicyParameterObject <Hashtable>
   [-Metadata <String>]
   [-Sku <Hashtable>]
   [-AssignIdentity]
   [-Location <String>]
   [-ApiVersion <String>]
   [-Pre]
   [-DefaultProfile <IAzureContextContainer>]
   [-InformationAction <ActionPreference>]
   [-InformationVariable <String>]
   [<CommonParameters>]
New-AzureRmPolicyAssignment
   -Name <String>
   -Scope <String>
   [-NotScope <String[]>]
   [-DisplayName <String>]
   [-Description <String>]
   -PolicyDefinition <PSObject>
   [-PolicySetDefinition <PSObject>]
   -PolicyParameter <String>
   [-Metadata <String>]
   [-Sku <Hashtable>]
   [-AssignIdentity]
   [-Location <String>]
   [-ApiVersion <String>]
   [-Pre]
   [-DefaultProfile <IAzureContextContainer>]
   [-InformationAction <ActionPreference>]
   [-InformationVariable <String>]
   [<CommonParameters>]
New-AzureRmPolicyAssignment
   -Name <String>
   -Scope <String>
   [-NotScope <String[]>]
   [-DisplayName <String>]
   [-Description <String>]
   [-PolicyDefinition <PSObject>]
   -PolicySetDefinition <PSObject>
   -PolicyParameterObject <Hashtable>
   [-Metadata <String>]
   [-Sku <Hashtable>]
   [-AssignIdentity]
   [-Location <String>]
   [-ApiVersion <String>]
   [-Pre]
   [-DefaultProfile <IAzureContextContainer>]
   [-InformationAction <ActionPreference>]
   [-InformationVariable <String>]
   [<CommonParameters>]
New-AzureRmPolicyAssignment
   -Name <String>
   -Scope <String>
   [-NotScope <String[]>]
   [-DisplayName <String>]
   [-Description <String>]
   [-PolicyDefinition <PSObject>]
   -PolicySetDefinition <PSObject>
   -PolicyParameter <String>
   [-Metadata <String>]
   [-Sku <Hashtable>]
   [-AssignIdentity]
   [-Location <String>]
   [-ApiVersion <String>]
   [-Pre]
   [-DefaultProfile <IAzureContextContainer>]
   [-InformationAction <ActionPreference>]
   [-InformationVariable <String>]
   [<CommonParameters>]

Description

New-AzureRmPolicyAssignment Cmdlet 會建立原則指派。 指定原則和範圍。

範例

範例 1:資源群組層級的原則指派

PS C:\> $ResourceGroup = Get-AzureRmResourceGroup -Name 'ResourceGroup11'
PS C:\> $Policy = Get-AzureRmPolicyDefinition -Name 'VirtualMachinePolicy'
PS C:\> New-AzureRmPolicyAssignment -Name 'VirtualMachinePolicyAssignment' -PolicyDefinition $Policy -Scope $ResourceGroup.ResourceId

第一個命令會使用 Get-AzureRMResourceGroup Cmdlet 取得名為 ResourceGroup11 的資源群組,並將它儲存在$ResourceGroup變數中。 第二個命令會使用 Get-AzureRmPolicyDefinition Cmdlet 取得名為 VirtualMachinePolicy 的原則定義,並將它儲存在$Policy變數中。 最後一個命令會在 $Policy 中,指派$ResourceGroup ResourceId 屬性所識別之資源群組層級的原則。

範例 2:具有原則參數對象的資源群組層級原則指派

PS C:\> $ResourceGroup = Get-AzureRmResourceGroup -Name 'ResourceGroup11'
PS C:\> $Policy = Get-AzureRmPolicyDefinition -BuiltIn | Where-Object {$_.Properties.DisplayName -eq 'Allowed locations'}
PS C:\> $Locations = Get-AzureRmLocation | where displayname -like '*east*'
PS C:\> $AllowedLocations = @{'listOfAllowedLocations'=($Locations.location)}
PS C:\> New-AzureRmPolicyAssignment -Name 'RestrictLocationPolicyAssignment' -PolicyDefinition $Policy -Scope $ResourceGroup.ResourceId -PolicyParameterObject $AllowedLocations

第一個命令會使用 Get-AzureRMResourceGroup Cmdlet 來取得名為 ResourceGroup11 的資源群組。 命令會將該物件儲存在 $ResourceGroup 變數中。 第二個命令會使用 Get-AzureRmPolicyDefinition Cmdlet 取得允許位置的內建原則定義。 命令會將該物件儲存在 $Policy 變數中。 第三和第四個命令會建立物件,其中包含名稱中具有 「east」 的所有 Azure 區域。 命令會將該物件儲存在$AllowedLocations變數中。 最後一個命令會使用 $AllowedLocations 中的原則參數物件,在資源群組層級$Policy指派原則。 $ResourceGroup的 ResourceId 屬性會識別資源群組。

範例 3:具有原則參數檔案的資源群組層級原則指派

使用下列內容,在本機工作目錄中建立名為 AllowedLocations.json 的檔案。

{
    "listOfAllowedLocations":  {
      "value": [
        "westus",
        "westeurope",
        "japanwest"
      ]
    }
}

PS C:\> $ResourceGroup = Get-AzureRmResourceGroup -Name 'ResourceGroup11'
PS C:\> $Policy = Get-AzureRmPolicyDefinition -BuiltIn | Where-Object {$_.Properties.DisplayName -eq 'Allowed locations'}
PS C:\> New-AzureRmPolicyAssignment -Name 'RestrictLocationPolicyAssignment' -PolicyDefinition $Policy -Scope $ResourceGroup.ResourceId -PolicyParameter .\AllowedLocations.json

第一個命令會使用 Get-AzureRMResourceGroup Cmdlet 取得名為 ResourceGroup11 的資源群組,並將它儲存在$ResourceGroup變數中。 第二個命令會使用 Get-AzureRmPolicyDefinition Cmdlet 取得允許位置的內建原則定義,並將它儲存在 $Policy 變數中。 最後一個命令會使用從本機工作目錄AllowedLocations.json的原則參數檔案,在 $ResourceGroup 的 ResourceId 屬性所識別的資源群組$Policy中指派原則。

範例 4:使用受控識別的原則指派

PS C:\> $ResourceGroup = Get-AzureRmResourceGroup -Name 'ResourceGroup11'
PS C:\> $Policy = Get-AzureRmPolicyDefinition -Name 'VirtualMachinePolicy'
PS C:\> New-AzureRmPolicyAssignment -Name 'VirtualMachinePolicyAssignment' -PolicyDefinition $Policy -Scope $ResourceGroup.ResourceId -Location 'eastus' -AssignIdentity

第一個命令會使用 Get-AzureRMResourceGroup Cmdlet 取得名為 ResourceGroup11 的資源群組,並將它儲存在$ResourceGroup變數中。 第二個命令會使用 Get-AzureRmPolicyDefinition Cmdlet 取得名為 VirtualMachinePolicy 的原則定義,並將它儲存在$Policy變數中。 最後一個命令會將 $Policy 中的原則指派給資源群組。 受控識別會自動建立並指派給原則指派。

參數

-ApiVersion

指定要使用的資源提供者 API 版本。 如果您未指定版本,此 Cmdlet 會使用最新的可用版本。

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

-AssignIdentity

產生並指派此原則指派的 Microsoft Entra 身分識別。 執行 'deployIfNotExists' 原則的部署時,將會使用身分識別。 指派身分識別時需要位置。

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

-DefaultProfile

用於與 azure 通訊的認證、帳戶、租用戶和訂用帳戶

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

-Description

原則指派的描述

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

-DisplayName

指定原則指派的顯示名稱。

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

-InformationAction

指定此 Cmdlet 如何回應資訊事件。 此參數可接受的值為:

  • 繼續
  • 忽略
  • 詢問
  • SilentlyContinue
  • 停止
  • 暫止
類型:ActionPreference
別名:infa
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-InformationVariable

指定資訊變數。

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

-Location

原則指派資源識別的位置。 這是使用 -AssignIdentity 參數時的必要專案。

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

-Metadata

新原則指派的元數據。 這可以是包含元數據的檔名路徑,或是作為字串的元數據。

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

-Name

指定原則指派的名稱。

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

-NotScope

原則指派的不是範圍。

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

-PolicyDefinition

指定原則,做為 包含原則規則的 PsPolicyDefinition 物件。

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

-PolicyParameter

原則參數檔案路徑或原則參數位符串。

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

-PolicyParameterObject

原則參數物件。

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

-PolicySetDefinition

原則集定義物件。

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

-Pre

指出此 Cmdlet 會在自動判斷要使用的版本時,考慮發行前版本 API 版本。

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

-Scope

指定要指派原則的範圍。 例如,若要將原則指派給資源群組,請指定下列專案: /subscriptions/訂用帳戶標識符/resourcegroups/資源組名

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

-Sku

表示 SKU 屬性的哈希表。 默認為免費 SKU,其值為: @{Name = 'A0'; Tier = 'Free'}。 若要使用標準 SKU,請使用值: @{Name = 'A1'; Tier = 'Standard'}

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