通过Powershell进行Azure模板部署:“模板参数JToken类型无效”

时间:2020-02-29 23:15:01

标签: azure azure-powershell azure-deployment azure-template

我正在尝试使用ARM模板通过Powershell部署虚拟机。

我想使用模板参数将SSH公钥传递到模板中。

参数在模板文件中的定义如下:

    "parameters": {
        "sshPublicKey": {
            "type": "string"
        }
    },

这是Powershell脚本,我在其中从文件加载公钥并将其添加为模板参数。

$publicKey = (Get-Content .\keys\id_rsa.pub)

"Public Key is string of length {0}" -f $publicKey.Length

$parametersObject = @{"sshPublicKey" = $publicKey }

"Running the Deployment now"
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile .\template.json -TemplateParameterObject $parametersObject

我收到以下令人沮丧的错误:

New-AzResourceGroupDeployment:9:02:09 AM-错误: 代码=无效模板;消息=部署模板验证失败: '模板参数JToken类型无效。预期为“字符串,Uri”。 实际的“对象”。请参阅 https://aka.ms/resource-manager-parameter-files了解使用情况的详细信息。”。 在 C:\ users \ sheph \ Documents \ GitHub \ aspnet-core-linux-exercise \ setup-ubuntu-nginx \ deploy.ps1:20 字符数:1 + New-AzResourceGroupDeployment -ResourceGroupName $ resourceGroupName-... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:未指定:(:) [New-AzResourceGroupDeployment],异常 + FullyQualifiedErrorId:Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

起初,我认为这是字符串长度的问题。 (密钥的长度为402个字符)。但是,如果我用402个连续的'A'字符替换它,我不会收到错误消息。

我还有另一个示例,该示例使用模板参数文件而不是模板参数对象,并且它也有效。

有什么办法可以使它正常工作吗?

1 个答案:

答案 0 :(得分:1)

问题是我的$ publicKey值包含换行符。

我通过调用upstream/master方法对其进行了修复。

Trim