【问题标题】:Azure Template Deployment via powershell: "Template parameter JToken type is not valid"通过 powershell 部署 Azure 模板:“模板参数 JToken 类型无效”
【发布时间】:2020-06-13 16:01:56
【问题描述】:

我正在尝试使用 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 - 错误: 代码=无效模板; Message=部署模板验证失败: '模板参数 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”字符替换它,我不会收到错误。

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

有什么办法可以让它工作吗?

【问题讨论】:

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


    【解决方案1】:

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

    我通过调用Trim 方法修复了它。

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

    【讨论】:

    • 当输入无法转换为 arm 模板引擎所期望的类型时,这是一个一般性错误,因此在遇到此错误时仅修剪所有内容(通常)不会真正有帮助
    猜你喜欢
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多