自動將實驗室使用者新增至 Azure DevTest Labs 中的實驗室

Azure DevTest Labs 可讓您使用 Azure 入口網站快速建立自助開發測試環境。 不過,如果您有數個小組和數個 DevTest Labs 執行個體,自動建立流程就可以節省時間。 Azure Resource Manager 範本可讓您以自動化方式建立實驗室、實驗室 VM、自訂映像、公式,以及新增使用者。 本文特別著重於將使用者新增至 DevTest Labs 執行個體。

若要將使用者新增至實驗室,請將使用者新增至實驗室的 DevTest Labs 使用者角色。 本文說明如何使用下列任一方式,將使用者自動新增至實驗室:

  • Azure 資源管理員範本
  • Azure PowerShell Cmdlet
  • Azure CLI。

使用 Azure 資源管理員範本

下列樣本 Resource Manager 範本指定了要新增至實驗室 DevTest Labs 使用者角色的使用者。

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "principalId": {
      "type": "string",
      "metadata": {
        "description": "The objectId of the user, group, or service principal for the role."
      }
    },
    "labName": {
      "type": "string",
      "metadata": {
        "description": "The name of the lab instance to be created."
      }
    },
    "roleAssignmentGuid": {
      "type": "string",
      "metadata": {
        "description": "Guid to use as the name for the role assignment."
      }
    }
  },
  "variables": {
    "devTestLabUserRoleId": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/111111111-0000-0000-11111111111111111')]",
    "fullDevTestLabUserRoleName": "[concat(parameters('labName'), '/Microsoft.Authorization/', parameters('roleAssignmentGuid'))]",
    "roleScope": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', resourceGroup().name, '/providers/Microsoft.DevTestLab/labs/', parameters('labName'))]"
  },
  "resources": [
    {
      "apiVersion": "2016-05-15",
      "type": "Microsoft.DevTestLab/labs",
      "name": "[parameters('labName')]",
      "location": "[resourceGroup().location]"
    },
    {
      "apiVersion": "2016-07-01",
      "type": "Microsoft.DevTestLab/labs/providers/roleAssignments",
      "name": "[variables('fullDevTestLabUserRoleName')]",
      "properties": {
        "roleDefinitionId": "[variables('devTestLabUserRoleId')]",
        "principalId": "[parameters('principalId')]",
        "scope": "[variables('roleScope')]"
      },
      "dependsOn": [
        "[resourceId('Microsoft.DevTestLab/labs', parameters('labName'))]"
      ]
    }
  ]
}

如果您要在建立實驗室的相同範本中指派角色,請務必在角色指派資源與實驗室之間新增相依性。 如需詳細資訊,請參閱定義 Azure Resource Manager 範本中的相依性一文。

角色指派資源資訊

角色指派資源必須指定類型和名稱。

首先要注意的,便是資源的類型不是 Microsoft.Authorization/roleAssignments,因為這適用於資源群組。 相反地,資源類型會遵循模式 {provider-namespace}/{resource-type}/providers/roleAssignments。 在此情況下,資源類型會是 Microsoft.DevTestLab/labs/providers/roleAssignments

角色指派名稱本身必須為全域唯一。 指派的名稱會使用模式 {labName}/Microsoft.Authorization/{newGuid}newGuid 是範本的參數值。 可以確保角色指派名稱為唯一。 由於建立 GUID 沒有範本函式,因此您必須使用任意 GUID 產生器工具來自行產生 GUID。

在範本中,角色指派的名稱是由 fullDevTestLabUserRoleName 變數所定義。 範本中的具體內容為:

"fullDevTestLabUserRoleName": "[concat(parameters('labName'), '/Microsoft.Authorization/', parameters('roleAssignmentGuid'))]"

角色指派資源屬性

角色指派本身會定義三種屬性。 需要 roleDefinitionIdprincipalIdscope

角色定義

角色定義識別碼是現有角色定義的字串識別碼。 角色識別碼的格式為 /subscriptions/{subscription-id}/providers/Microsoft.Authorization/roleDefinitions/{role-definition-id}

訂閱識別碼可使用 subscription().subscriptionId 範本函式取得。

您需要取得 DevTest Labs User 內建角色的角色定義。 若要取得 DevTest Labs 使用者角色的 GUID,您可以使用角色指派 REST APIGet-AzRoleDefinition Cmdlet。

$dtlUserRoleDefId = (Get-AzRoleDefinition -Name "DevTest Labs User").Id

角色識別碼於變數區段中定義,且命名為 devTestLabUserRoleId。 在範本中,角色識別碼設定為:111111111-0000-0000-11111111111111111。

"devTestLabUserRoleId": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/111111111-0000-0000-11111111111111111')]",

主體識別碼

您想做為實驗室使用者新增至實驗室的 Active Directory 使用者、群組或服務主體,其物件識別碼即為主體識別碼。 範本使用 ObjectId 做為參數。

您可以使用 Get-AzureRMADUser、Get-AzureRMADGroup 或 Get-AzureRMADServicePrincipal PowerShell Cmdlet 取得 ObjectId。 這些 Cmdlet 會傳回具識別碼屬性的單一 Active Directory 物件或其清單,也就是您需要的物件識別碼。 下列範例說明如何取得公司中單一使用者的物件識別碼。

$userObjectId = (Get-AzureRmADUser -UserPrincipalName 'email@company.com').Id

您也可以使用包含Get-MgUser、Get-MgGroupGet-MgServicePrincipal的 Microsoft Graph PowerShell Cmdlet。

範圍

範圍會指定角色指派應套用的資源或資源群組。 資源的範圍格式為:/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/{provider-namespace}/{resource-type}/{resource-name}。 範本會使用 subscription().subscriptionId 函式來填入 subscription-id 組件,而 resourceGroup().name 範本函式則會填入 resource-group-name 組件。 使用這些函式表示您要指派角色的實驗室必須存在於目前的訂閱,以及範本部署所在的相同資源群組中。 最後一部分,resource-name 即為實驗室的名稱。 此範例會透過範本參數獲得這個值。

範本中的角色範圍:

"roleScope": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', resourceGroup().name, '/providers/Microsoft.DevTestLab/labs/', parameters('labName'))]"

部署範本

首先,建立參數檔案 (例如:azuredeploy.parameters.json) 以傳遞 Resource Manager 範本中的參數值。

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "principalId": {
      "value": "11111111-1111-1111-1111-111111111111"
    },
    "labName": {
      "value": "MyLab"
    },
    "roleAssignmentGuid": {
      "value": "22222222-2222-2222-2222-222222222222"
    }
  }
}

然後,使用 New-AzureRmResourceGroupDeployment PowerShell Cmdlet 來部署 Resource Manager 範本。 下列範例命令會將人員、群組或服務主體指派給實驗室的 DevTest Labs 使用者角色。

New-AzureRmResourceGroupDeployment -Name "MyLabResourceGroup-$(New-Guid)" -ResourceGroupName 'MyLabResourceGroup' -TemplateParameterFile .\azuredeploy.parameters.json -TemplateFile .\azuredeploy.json

注意

群組部署名稱和角色指派 GUID 必須為唯一。 如果您嘗試使用非唯一 GUID 來部署資源指派,就會看到 RoleAssignmentUpdateNotPermitted 錯誤。

如果您打算多次使用範本,並將數個 Active Directory 物件新增至實驗室的 DevTest Labs 使用者角色,請考慮在 PowerShell 命令中使用動態物件。 下列範例會使用 New-Guid Cmdlet 動態指定資源群組部署名稱和角色指派 GUID。

New-AzureRmResourceGroupDeployment -Name "MyLabResourceGroup-$(New-Guid)" -ResourceGroupName 'MyLabResourceGroup' -TemplateFile .\azuredeploy.json -roleAssignmentGuid "$(New-Guid)" -labName "MyLab" -principalId "11111111-1111-1111-1111-111111111111"

使用 Azure PowerShell

如簡介中所述,您會建立新的 Azure 角色指派,並將使用者新增至實驗室的 DevTest Labs 使用者角色。 您可以在 PowerShell 中使用 New-AzureRMRoleAssignment Cmdlet 來執行此動作。 此 Cmdlet 有許多選擇性參數可提供彈性。 可以指定 ObjectIdSigninNameServicePrincipalName 為被授與權限的物件。

以下是將使用者新增至指定實驗室中 DevTest Labs 使用者角色的樣本 Azure PowerShell 命令。

New-AzureRmRoleAssignment -UserPrincipalName <email@company.com> -RoleDefinitionName 'DevTest Labs User' -ResourceName '<Lab Name>' -ResourceGroupName '<Resource Group Name>' -ResourceType 'Microsoft.DevTestLab/labs'

若要指定授與權限的資源,可以透過 ResourceNameResourceTypeResourceGroup 的組合或 scope 參數來指定。 無論使用何種參數組合,請提供足夠的資訊給 Cmdlet,以唯一識別 Active Directory 物件 (使用者、群組或服務主體)、範圍 (資源群組或資源),以及角色定義。

使用 Azure CLI

在 Azure CLI 中,可以使用 az role assignment create 命令將實驗室使用者新增至實驗室。 如需 Azure CLI Cmdlet 的詳細資訊,請參閱使用 Azure CLI 新增或移除 Azure 角色指派

正在授與存取權的物件可由 objectIdsignInNamespn 參數指定。 要授與物件的存取權實驗室可以透過 scope URL 或 resource-nameresource-typeresource-group 參數的組合來識別。

下列 Azure CLI 範例說明如何將人員新增至指定實驗室的 DevTest Labs 使用者角色。

az role assignment create --roleName "DevTest Labs User" --signInName <email@company.com> -–resource-name "<Lab Name>" --resource-type "Microsoft.DevTestLab/labs" --resource-group "<Resource Group Name>" --role Contributor --scope /subscriptions/<SubscriptionID>/resourceGroups/<ResourceGroupName>

後續步驟

查看下列文章: