2

我正在尝试从存储在默认存储帐户中的 vhd 图像在我的 Azure DevTestLab 中创建自定义图像。

我开始在GitHub存储库中修改用于从 vhd 创建自定义图像的 ARM 模板文件。

我已经创建了 DevTest 实验室,我可以运行以下 Powershell 命令来获取其数据:

PS C:\git> $rid = Get-AzureRmResource -ResourceGroupName jgTl9-rg -ResourceType Microsoft.DevTestLab/labs -ResourceName jgTl9 -ApiVersion 2016-05-15

这会产生大量信息。如果要提取默认存储帐户名称,请执行以下操作:

PS C:\git> $rid.properties.defaultStorageAccount

所以我知道有可能得到它。现在我修改了 Azure 构建模板(一个 JSON 文件)的变量部分,如下所示:

"variables": {
  "resourceName": "[concat(variables('existingLabName'), '/', variables('imageName'))]",
  "resourceType": "Microsoft.DevTestLab/labs/customimages", 
  "existingLabName": "[replace(resourceGroup().name,'-rg','')]",
  "storageAccountID": "/subscriptions/xxxxxxxxxxxx/resourceGroups/jgTl9-rg/providers/Microsoft.Storage/storageAccounts/djgtl91795",
  "saidFrags": "[skip(split(variables('storageAccountID'), '/' ), add(length(split(variables('storageAccountID'), '/' )),-1) ) ]",  
  "storageAccountSuffix": "[take(variables('saidFrags'),1)[0]]",
  "existingVhdUri": "[concat('https://',variables('storageAccountSuffix'),'.blob.core.windows.net/uploads/',parameters('imageFileName'))]",
  "imageName": "JaysImage"

},

当我使用 azuredeploy.json 和 azuredeploy.parameters.json 调用 New-AzureRmResourceGroupDeployment 命令时,此代码实际上有效。

但是,我不想硬编码storageAccountID变量,而是想从环境中提取 defaultStorageAccount。我试过这样的事情:

   "storageAccountID": "[string( resourceId(resourceGroup().name,'MicrosoftDevTestLab/labs','jgTl9').properties.defaultStorageAccount  ))",

我对此尝试了一些变化,但没有任何效果。我需要使用什么语法来提取与我为 storageAccountID 硬编码的相同信息?

(我在微软对模板函数的描述中找不到任何东西)。

4

1 回答 1

0

您的以下表达有几个问题

"storageAccountID": "[string( resourceId(resourceGroup().name,'MicrosoftDevTestLab/labs','jgTl9').properties.defaultStorageAccount  ))",
  1. 存在语法问题,即。最后加一个)
  2. 资源提供者MicrosoftDevTestlab/labs应该是Microsoft.DevTestlab/labs
  3. 使用时resourceId,它不会为您提供整个对象。因此,当您这样做时properties.defaultStorageAccount,它将不起作用。因为它只为您提供了您的开发测试实验室的字符串,而没有其他任何内容,并且defaultStorageAccount.

现在你需要的是reference如下功能

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {    
  },
  "variables": {
    "vDefaultStorageAccount": "[string(resourceId('yourResourceGroupName','Microsoft.DevTestLab/labs','yourTestLabName'))]"
  },
  "resources": [],
  "outputs": {        
    "defaultStorageAccount":{
      "value": "[reference(variables('vDefaultStorageAccount'),'2016-05-15').defaultStorageAccount]",
      "type":"string"
    }        
  }
}

在变量中,vDefaultStorageAccount我们获取对象的 ID,DevTestLab然后使用reference函数获取对象。然后你可以做得到.属性。例如,您不需要执行 properties.defaultStorageAccount。

于 2017-01-21T01:41:22.487 回答