Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell Azure:构建模板:如何提取defaultStorageAccount_Powershell_Azure_Build Templates - Fatal编程技术网

Powershell Azure:构建模板:如何提取defaultStorageAccount

Powershell Azure:构建模板:如何提取defaultStorageAccount,powershell,azure,build-templates,Powershell,Azure,Build Templates,我正在尝试从默认存储帐户中存储的vhd映像在Azure DevTestLab中创建自定义映像 我开始修改ARM模板的文件,以便从vhd创建自定义映像 我已经创建了DevTest实验室,可以运行以下Powershell命令获取其数据: PS C:\git> $rid = Get-AzureRmResource -ResourceGroupName jgTl9-rg -ResourceType Microsoft.DevTestLab/labs -ResourceName jgTl9 -Api

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

我开始修改ARM模板的文件,以便从vhd创建自定义映像

我已经创建了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调用新的AzureRmResourceGroupDeployment命令时,这段代码实际上可以工作

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

   "storageAccountID": "[string( resourceId(resourceGroup().name,'MicrosoftDevTestLab/labs','jgTl9').properties.defaultStorageAccount  ))",
我尝试了一些变化,但没有任何效果。提取与我硬编码的storageAccountID相同的信息需要使用什么语法


(我在中找不到任何内容)。

以下表达式有几个问题

"storageAccountID": "[string( resourceId(resourceGroup().name,'MicrosoftDevTestLab/labs','jgTl9').properties.defaultStorageAccount  ))",
  • 有语法问题,即结尾处有一个额外的
  • 资源提供者
    MicrosoftDevTestlab/labs
    应该是
    Microsoft.DevTestlab/labs
  • 使用
    resourceId
    时,它不会提供整个对象。因此,当您执行
    properties.defaultStorageAccount
    操作时,它将不起作用。因为它只提供dev测试实验室的字符串,没有其他内容,而且
    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
    获取
    DevTestLab
    的ID,然后使用
    reference
    函数获取对象。然后您可以执行
    以获取属性。例如,您不需要执行properties.defaultStorageAccount