gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:53:46 25 4
gpt4 key购买 nike

我有一个带有以下参数的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com