快速入門:使用 PowerShell 定義和指派 Azure 藍圖

重要

在 2026 年 7 月 11 日,藍圖 (預覽) 將會淘汰。 將現有的藍圖定義和指派移轉至範本規格部署堆疊。 藍圖成品會轉換成用來定義部署堆疊的 ARM JSON 範本或 Bicep 檔案。 若要了解如何將成品撰寫為 ARM 資源,請參閱:

在本教學課程中,您將了解如何使用 Azure 藍圖在您的組織中處理藍圖的建立、發佈和指派等常見工作。 此技能可協助您根據 Azure Resource Manager (ARM) 範本、原則和安全性定義常用模式,開發可重複使用並可快速部署的設定。

必要條件

  • 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶
  • 如果尚未安裝 Az.Blueprint 模組,請依照新增 Az.Blueprint 模組中的指示,從 PowerShell 資源庫安裝並驗證它。
  • 如果您未使用過 Azure 藍圖,請透過 Azure PowerShell 向 Register-AzResourceProvider -ProviderNamespace Microsoft.Blueprint 註冊資源提供者。

Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。 您可以使用 Cloud Shell 預先安裝的命令,執行本文提到的程式碼,而不必在本機環境上安裝任何工具。

要啟動 Azure Cloud Shell:

選項 範例/連結
選取程式碼或命令區塊右上角的 [試試看]。 選取 [試試看] 並不會自動將程式碼或命令複製到 Cloud Shell 中。 Azure Cloud Shell 的「試試看」範例螢幕擷取畫面。
請前往 https://shell.azure.com,或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。 用於啟動 Azure Cloud Shell 的按鈕。
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。 顯示 Azure 入口網站中 Cloud Shell 按鈕的螢幕擷取畫面

若要使用 Azure Cloud Shell:

  1. 啟動 Cloud Shell。

  2. 選取程式碼區塊 (或命令區塊) 上的 [複製] 按鈕以複製程式碼或命令。

  3. 透過在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼或命令貼到 Cloud Shell 工作階段中。

  4. 選取 Enter 鍵執行程式碼或命令。

建立藍圖

定義合規性標準模式的第一個步驟,即是以可用的資源規劃藍圖。 讓我們建立名為 MyBlueprint 的藍圖,設定訂用帳戶的角色和原則指派。 接著,您新增資源群組、ARM 範本,以及資源群組的角色指派。

注意

當您使用 PowerShell 時,會先建立藍圖物件。 對於要新增的具有參數的每個成品,您會在初始藍圖上預先定義參數。

  1. 建立初始藍圖物件。 BlueprintFile 參數會取用一個 JSON 檔案,其中包含藍圖的相關屬性、要建立的任何資源群組,以及所有藍圖層級參數。 您會在指派期間設定參數,這些參數會由您在後續步驟中新增的成品使用。

    • JSON 檔案:blueprint.json

      {
          "properties": {
              "description": "This blueprint sets tag policy and role assignment on the subscription, creates a ResourceGroup, and deploys a resource template and role assignment to that ResourceGroup.",
              "targetScope": "subscription",
              "parameters": {
                  "storageAccountType": {
                      "type": "string",
                      "defaultValue": "Standard_LRS",
                      "allowedValues": [
                          "Standard_LRS",
                          "Standard_GRS",
                          "Standard_ZRS",
                          "Premium_LRS"
                      ],
                      "metadata": {
                          "displayName": "storage account type.",
                          "description": null
                      }
                  },
                  "tagName": {
                      "type": "string",
                      "metadata": {
                          "displayName": "The name of the tag to provide the policy assignment.",
                          "description": null
                      }
                  },
                  "tagValue": {
                      "type": "string",
                      "metadata": {
                          "displayName": "The value of the tag to provide the policy assignment.",
                          "description": null
                      }
                  },
                  "contributors": {
                      "type": "array",
                      "metadata": {
                          "description": "List of AAD object IDs that is assigned Contributor role at the subscription",
                          "strongType": "PrincipalId"
                      }
                  },
                  "owners": {
                      "type": "array",
                      "metadata": {
                          "description": "List of AAD object IDs that is assigned Owner role at the resource group",
                          "strongType": "PrincipalId"
                      }
                  }
              },
              "resourceGroups": {
                  "storageRG": {
                      "description": "Contains the resource template deployment and a role assignment."
                  }
              }
          }
      }
      
    • PowerShell 命令

      # Login first with Connect-AzAccount if not using Cloud Shell
      
      # Get a reference to the new blueprint object, we'll use it in subsequent steps
      $blueprint = New-AzBlueprint -Name 'MyBlueprint' -BlueprintFile .\blueprint.json
      

      注意

      當您以程式設計方式建立藍圖定義時,請使用檔案名稱 blueprint.json。 當您呼叫 Import-AzBlueprintWithArtifact 時,會用到這個檔案名稱。

      根據預設,藍圖物件會建立在預設訂用帳戶中。 若要指定管理群組,請使用參數 ManagementGroupId。 若要指定訂閱,請使用參數 SubscriptionId

  2. 在訂用帳戶新增角色指派。 ArtifactFile 會定義成品的種類,屬性會對應至角色定義識別碼,且主體身分識別會以值陣列的形式傳遞。 在下列範例中,授與指定角色的主體身分識別是設定給藍圖指派期間設定的參數。 此範例使用 GUID 為 b24988ac-6180-42a0-ab88-20f7382dd24cContributor 內建角色。

    • JSON 檔案:\artifacts\roleContributor.json

      {
          "kind": "roleAssignment",
          "properties": {
              "roleDefinitionId": "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c",
              "principalIds": "[parameters('contributors')]"
          }
      }
      
    • PowerShell 命令

      # Use the reference to the new blueprint object from the previous steps
      New-AzBlueprintArtifact -Blueprint $blueprint -Name 'roleContributor' -ArtifactFile .\artifacts\roleContributor.json
      
  3. 在訂用帳戶新增原則指派。 ArtifactFile 會定義成品的種類、對應至原則或方案定義的屬性,並將原則指派設定為使用在藍圖指派期間定義的藍圖參數。 此範例使用 GUID 為 49c88fc8-6fd1-46fd-a676-f12d1d3a4c71Apply tag and its default value to resource groups 內建原則。

    • JSON 檔案:\artifacts\policyTags.json

      {
          "kind": "policyAssignment",
          "properties": {
              "displayName": "Apply tag and its default value to resource groups",
              "description": "Apply tag and its default value to resource groups",
              "policyDefinitionId": "/providers/Microsoft.Authorization/policyDefinitions/49c88fc8-6fd1-46fd-a676-f12d1d3a4c71",
              "parameters": {
                  "tagName": {
                      "value": "[parameters('tagName')]"
                  },
                  "tagValue": {
                      "value": "[parameters('tagValue')]"
                  }
              }
          }
      }
      
    • PowerShell 命令

      # Use the reference to the new blueprint object from the previous steps
      New-AzBlueprintArtifact -Blueprint $blueprint -Name 'policyTags' -ArtifactFile .\artifacts\policyTags.json
      
  4. 在訂用帳戶為儲存體標記新增其他原則指派 (藉由重複使用 storageAccountType_ parameter)。 這個新增的原則指派成品示範藍圖上定義的參數可供多個成品使用。 在此範例中,您會使用 storageAccountType 在資源群組上設定標記。 此值會提供您在下一個步驟中所建立儲存體帳戶的相關資訊。 此範例使用 GUID 為 49c88fc8-6fd1-46fd-a676-f12d1d3a4c71Apply tag and its default value to resource groups 內建原則。

    • JSON 檔案:\artifacts\policyStorageTags.json

      {
          "kind": "policyAssignment",
          "properties": {
              "displayName": "Apply storage tag to resource group",
              "description": "Apply storage tag and the parameter also used by the template to resource groups",
              "policyDefinitionId": "/providers/Microsoft.Authorization/policyDefinitions/49c88fc8-6fd1-46fd-a676-f12d1d3a4c71",
              "parameters": {
                  "tagName": {
                      "value": "StorageType"
                  },
                  "tagValue": {
                      "value": "[parameters('storageAccountType')]"
                  }
              }
          }
      }
      
    • PowerShell 命令

      # Use the reference to the new blueprint object from the previous steps
      New-AzBlueprintArtifact -Blueprint $blueprint -Name 'policyStorageTags' -ArtifactFile .\artifacts\policyStorageTags.json
      
  5. 在資源群組下新增範本。 ARM 範本的 TemplateFile 包含範本的一般 JSON 元件。 此範本也會將 storageAccountTypetagNametagValue 藍圖參數傳至範本,以重複使用這些參數。 藍圖參數可藉由使用 TemplateParameterFile 提供給範本使用,並且可在使用索引鍵/值配對來插入值的 JSON 範本內使用。 藍圖和範本參數名稱可能相同。

    • JSON ARM 範本檔案:\artifacts\templateStorage.json

      {
          "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {
              "storageAccountTypeFromBP": {
                  "type": "string",
                  "metadata": {
                      "description": "Storage Account type"
                  }
              },
              "tagNameFromBP": {
                  "type": "string",
                  "defaultValue": "NotSet",
                  "metadata": {
                      "description": "Tag name from blueprint"
                  }
              },
              "tagValueFromBP": {
                  "type": "string",
                  "defaultValue": "NotSet",
                  "metadata": {
                      "description": "Tag value from blueprint"
                  }
              }
          },
          "variables": {
              "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'standardsa')]"
          },
          "resources": [{
              "type": "Microsoft.Storage/storageAccounts",
              "name": "[variables('storageAccountName')]",
              "apiVersion": "2016-01-01",
              "tags": {
                  "[parameters('tagNameFromBP')]": "[parameters('tagValueFromBP')]"
              },
              "location": "[resourceGroup().location]",
              "sku": {
                  "name": "[parameters('storageAccountTypeFromBP')]"
              },
              "kind": "Storage",
              "properties": {}
          }],
          "outputs": {
              "storageAccountSku": {
                  "type": "string",
                  "value": "[variables('storageAccountName')]"
              }
          }
      }
      
    • JSON ARM 範本參數檔案:\artifacts\templateStorageParams.json

      {
          "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {
              "storageAccountTypeFromBP": {
                  "value": "[parameters('storageAccountType')]"
              },
              "tagNameFromBP": {
                  "value": "[parameters('tagName')]"
              },
              "tagValueFromBP": {
                  "value": "[parameters('tagValue')]"
              }
          }
      }
      
    • PowerShell 命令

      # Use the reference to the new blueprint object from the previous steps
      New-AzBlueprintArtifact -Blueprint $blueprint -Type TemplateArtifact -Name 'templateStorage' -TemplateFile .\artifacts\templateStorage.json -TemplateParameterFile .\artifacts\templateStorageParams.json -ResourceGroupName storageRG
      
  6. 在資源群組下新增角色指派。 類似於先前的角色指派項目,下列範例為 Owner 角色使用定義識別碼,並為其提供藍圖的不同參數。 此範例使用 GUID 為 8e3af657-a8ff-443c-a75c-2fe8c4bcb635Owner 內建角色。

    • JSON 檔案:\artifacts\roleOwner.json

      {
          "kind": "roleAssignment",
          "properties": {
              "resourceGroup": "storageRG",
              "roleDefinitionId": "/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635",
              "principalIds": "[parameters('owners')]"
          }
      }
      
    • PowerShell 命令

      # Use the reference to the new blueprint object from the previous steps
      New-AzBlueprintArtifact -Blueprint $blueprint -Name 'roleOwner' -ArtifactFile .\artifacts\roleOwner.json
      

發佈藍圖

將成品新增至藍圖之後,即可發佈藍圖。 發佈之後,藍圖即可指派給訂用帳戶。

# Use the reference to the new blueprint object from the previous steps
Publish-AzBlueprint -Blueprint $blueprint -Version '{BlueprintVersion}'

{BlueprintVersion} 的值是字母、數字和連字號組成的字串 (無空格或其他特殊字元)。 長度上限為 20 個字元。 使用如 v20180622-135541 這類唯一且包含資訊的字串。

指派藍圖

使用 PowerShell 發佈藍圖之後,即可將藍圖指派給訂閱。 將您建立的藍圖指派給管理群組階層下的其中一個訂用帳戶。 如果將藍圖儲存到某訂用帳戶,則只能將其指派給該訂用帳戶。 Blueprint 參數會指定要指派的藍圖。 若要提供 namelocationidentitylockblueprint 參數,請在 New-AzBlueprintAssignment Cmdlet 上使用相符的 PowerShell 參數,或將其提供於 AssignmentFile 參數 JSON 檔案中。

  1. 將藍圖部署指派給訂用帳戶以執行它。 因為 contributorsowners 參數需要原則的 objectIds 陣列獲得授與角色指派,請使用 Azure Active Directory Graph API 收集 objectIds,以用於您自己的使用者、群組或服務主體的 AssignmentFile 中。

    • JSON 檔案:blueprintAssignment.json

      {
          "properties": {
              "blueprintId": "/providers/Microsoft.Management/managementGroups/{YourMG}/providers/Microsoft.Blueprint/blueprints/MyBlueprint",
              "resourceGroups": {
                  "storageRG": {
                      "name": "StorageAccount",
                      "location": "eastus2"
                  }
              },
              "parameters": {
                  "storageAccountType": {
                      "value": "Standard_GRS"
                  },
                  "tagName": {
                      "value": "CostCenter"
                  },
                  "tagValue": {
                      "value": "ContosoIT"
                  },
                  "contributors": {
                      "value": [
                          "7be2f100-3af5-4c15-bcb7-27ee43784a1f",
                          "38833b56-194d-420b-90ce-cff578296714"
                      ]
                  },
                  "owners": {
                      "value": [
                          "44254d2b-a0c7-405f-959c-f829ee31c2e7",
                          "316deb5f-7187-4512-9dd4-21e7798b0ef9"
                      ]
                  }
              }
          },
          "identity": {
              "type": "systemAssigned"
          },
          "location": "westus"
      }
      
    • PowerShell 命令

      # Use the reference to the new blueprint object from the previous steps
      New-AzBlueprintAssignment -Blueprint $blueprint -Name 'assignMyBlueprint' -AssignmentFile .\blueprintAssignment.json
      
    • 使用者指派的受控識別

      藍圖指派也可以使用指派使用者的受控識別。 在此情況下,JSON 指派檔案的 identity 部分將會變更,如下所示。 請分別將 {tenantId}{subscriptionId}{yourRG}{userIdentity} 取代為您的租用戶識別碼、訂閱識別碼、資源群組名稱,以及使用者指派的受控識別名稱。

      "identity": {
          "type": "userAssigned",
          "tenantId": "{tenantId}",
          "userAssignedIdentities": {
              "/subscriptions/{subscriptionId}/resourceGroups/{yourRG}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{userIdentity}": {}
          }
      },
      

      使用者指派的受控識別可位於使用者指派藍圖具有權限的任何訂用帳戶和資源群組中。

      重要

      Azure 藍圖不會管理使用者指派的受控識別。 使用者需負責指派足夠的角色和權限,否則藍圖指派將會失敗。

清除資源

您可以從訂用帳戶中移除藍圖。 移除作業通常會在成品資源已不再需要時執行。 移除藍圖時,會將指派為該藍圖一部份的成品保留下來。 若要移除藍圖指派,請使用 Remove-AzBlueprintAssignment Cmdlet:

assignMyBlueprint

Remove-AzBlueprintAssignment -Name 'assignMyBlueprint'

下一步

在本快速入門中,您已使用 PowerShell 建立、指派及移除藍圖。 若要深入了解 Azure 藍圖,請繼續閱讀藍圖生命週期文章。