powershell - 无法通过 Powershell 在 ARM 模板中传递数组类型参数

标签 powershell azure azure-resource-manager azure-powershell

我有一个带有以下参数的 ARM 模板:

"parameters": {
    "projectName": {
      "type": "string",
      "metadata": {
        "description": "Name of the project"
      }
    },
    "environmentName": {
      "type": "string",
      "defaultValue": "testing",
      "metadata": {
        "description": "Name/Type of the environment"
      }
    },
    "vmResourceGroupName": {
      "type": "string",
      "metadata": {
        "description": "Resource Group in which VMs wil be deployed"
      }
    },
    "vmName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Virtual Machine"
      }
    },
    "vmIPAddress": {
      "type": "string",
      "metadata": {
        "description": "IP Address of the Virtual Machine"
      }
    },
    "osDiskVhdUri": {
      "type": "string",
      "metadata": {
        "description": "Uri of the existing VHD in ARM standard or premium storage"
      }
    },
    "dataDiskVhdUri": {
      "type": "array",
      "metadata": {
        "description": "Uri of the existing VHD in ARM standard or premium storage"
      }
    },
    "vmSize": {
      "type": "string",
      "metadata": {
        "description": "Size of the Virtual Machine"
      }
    },
    "osType": {
      "type": "string",
      "allowedValues": [
        "Windows",
        "Linux"
      ],
      "metadata": {
        "description": "OS of the VM - Typically Windows or Linux"
      }
    },
    "ManagedDisk": {
      "type": "string",
      "allowedValues": [
        "Yes",
        "No"
      ],
      "metadata": {
        "description":  "Yes for Managed Disks, No for VHDs"
      }
    }

显然,$dataDiskVHDURI 的类型为:array,我尝试使用 -TemplateParameterObject 和 Powershell 中的 New-AzureRMresourceGroupDeployment cmdlet 使用以下代码来部署模板:

{
        $vmWithDDTemplate = 'azuredeploy.json'
    $vmWithoutDDTemplate = 'azuredeploy-withoutdd.json'

    $dataDiskVhdUri = New-Object -TypeName System.Collections.ArrayList
    $dataDiskVhdUri.Add("$VM.dataDiskVhduri")


    #Creating VM param object    
    $VMTemplateObject = @{"projectname" = $projectName; `  
                          "environmentname" = $environmentName; `
                          "vmResourceGroupName" = $vmResourceGroupName; `
                          "vmName" = $VM.vmName; `
                          "vmIPAddress" = $VM.vmIPAddress; `
                          "osDiskVhdUri" = $VM.osDiskVhdUri; `
                          "dataDiskVhdUri" = ,$dataDiskVhdUri; `
                          "vmSize" = $VM.vmSize; `
                          "osType" = $VM.osType; `
                          "ManagedDisk" = $VM.ManagedDisk
                         }
    #$VMTemplateObject

    # Checking if VM contains a data disk
    if($VM.dataDiskVhdUri -ne $null) 
    {
        Write Output "$VM contains data disk"

        New-AzureRmResourceGroupDeployment -Name ('vmwithdd' + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) `
                                   -ResourceGroupName $ResourceGroupName `
                                   -TemplateParameterObject $VMTemplateObject `
                                   -TemplateFile $vmWithDDTemplate `
                                    -Force -Verbose -ErrorVariable ErrorMessages `
                                    -ErrorAction Stop -DefaultProfile $azureCred

    }
    else
    {
        Write-output '$VM does not contain data disk'
    }
}

但是,我每次都会收到以下错误:

Microsoft.PowerShell.Utility\Write-Error : 4:46:14 PM - Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The provided value for the template parameter 'dataDiskVhdUri' at line '44' and column '27' is not valid.'. At Create-Environment:73 char:73 + + CategoryInfo : NotSpecified: (:) [Write-Error], RemoteException + FullyQualifiedErrorId : System.Management.Automation.RemoteException,Microsoft.PowerShell.Commands.WriteErrorCommand + PSComputerName : [localhost]

有人知道如何解决这个问题吗?

最佳答案

不确定,但也许使用前面的 , 向其提供包装在数组中的 ArrayList 对象并不是它想要的。如果我查找示例,我只会看到其中添加的单个值,例如 "dataDiskVhdUri"= $VM.dataDiskVhduri;

关于powershell - 无法通过 Powershell 在 ARM 模板中传递数组类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50894446/

相关文章:

azure - 嵌套资源的嵌套资源

azure-resource-manager - 使用 ARM 一次性部署多个 Azure 虚拟网络和子网时出现 AnotherOperationInProgress

azure - 通过 PowerShell 查找 Azure SQL 数据库异地复制辅助数据库

java - Java使用proc.waitFor()调用Powershell,无法获得正确的返回码

ruby-on-rails - SSH 连接关闭后,Rails 服务器(命令)不会持续存在

c# - 一张表、多个 View 、一个 C# 类

.net - 遥测采样而不影响错误/故障

powershell - 测试路径返回 True 但 ExtractToDirectory 找不到路径

windows - 为什么 GetCommandLineArgs 在运行 pwsh.exe 时返回一个 DLL?

Azure Policy 将 Linux VM 报告为混合权益投诉