无法通过Powershell

时间:2018-06-17 07:03:47

标签: 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,我尝试使用以下代码在Powershell中使用 -TemplateParameterObject 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 - 错误:   代码= InvalidTemplate; Message =部署模板验证失败:   '模板参数提供的值' dataDiskVhdUri'在   线' 44'和专栏' 27'无效。'。在创造环境:73   焦炭:73   +       + CategoryInfo:NotSpecified:(:) [Write-Error],RemoteException       + FullyQualifiedErrorId:System.Management.Automation.RemoteException,Microsoft.PowerShell.Commands.WriteErrorCommand       + PSComputerName:[localhost]

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

2 个答案:

答案 0 :(得分:1)

不确定,但可能使用前面的,将数据包装在一个ArrayList对象中,并不是它想要的。如果我查找示例,我看到只添加了单个值,如"dataDiskVhdUri" = $VM.dataDiskVhduri;

答案 1 :(得分:0)

尝试一下:

$OptionalParameters = New-Object -TypeName Hashtable

$OptionalParameters.Add("aParam", @(1,2,3))

New-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName `
                                   -TemplateFile azuredeply.json `
                                   @OptionalParameters

使用:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "aParam": {
        "type": "array"
    }
},
"variables": { },
"resources": [ ],
"outputs": {
    "dump": {
        "type": "array",
        "value": "[parameters('aParam')]"
    }
}

}