我正在尝试使用 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/