首页 > 解决方案 > 通过 powershell 部署 Azure 模板:“模板参数 JToken 类型无效”

问题描述

我正在尝试使用 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 - 错误:代码=无效模板;消息=部署模板验证失败:'模板参数 JToken 类型无效。应为“字符串,Uri”。实际的“对象”。请参阅 https://aka.ms/resource-manager-parameter-files了解使用详情。在 C:\users\sheph\Documents\GitHub\aspnet-core-linux-exercise\setup-ubuntu-nginx\deploy.ps1:20 char:1 + New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName - ... + ~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [New-AzResourceGroupDeployment], 异常 + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

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

我有另一个使用模板参数文件而不是模板参数对象的示例,这也有效。

有没有办法让这个工作?

标签: azureazure-powershellazure-deploymentazure-template

解决方案


The problem is that my $publicKey value contained a newline character.

I fixed it by calling the Trim method.

$publicKey = (Get-Content .\keys\id_rsa.pub);
$publicKey = $publicKey.Trim();
$parametersObject = @{"sshPublicKey" = $publicKey }

推荐阅读