通过powershell进行Azure模板部署: "Template parameter JToken type is not valid"

标签 azure azure-powershell azure-deployment azure-rm-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 - Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'Template parameter JToken type is not valid. Expected 'String, Uri'. Actual 'Object'. Please see https://aka.ms/resource-manager-parameter-files for usage details.'. At 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], Exception + FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

一开始我以为是字符串长度的问题。 ( key 的长度为 402 个字符)。但如果我用 402 个连续的“A”字符替换它,我就不会收到错误。

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

有什么办法可以让它发挥作用吗?

最佳答案

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

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

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

关于通过powershell进行Azure模板部署: "Template parameter JToken type is not valid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60470531/

相关文章:

Azure 服务器 FHIR : Search Sorting

azure - 逻辑应用 - 将 Blob 文件复制到本地服务器

azure - 在多个资源组中添加相同的资源

azure - 最佳方法: Azure Web Apps ( Static HTML sites )

azure - Arm 模板函数不适用于默认值

azure - 取消 azure 资源管理器部署

azure - 在 MS Azure 上部署 Acumatica ERP

azure - Get-AzRoleAssignment 在 Azure Runbook 中引发 Microsoft.Rest.Azure.CloudException

azure - 在 Azure PS Function 中添加 api 管理用户时出现错误 : Get-AzApiManagementProduct : The pipeline has been stopped.

azure - 在 Windows Azure 的何处部署 Web Api : as Website or as Cloud Service?