Andrew Shepherd
Andrew Shepherd

Reputation: 45262

Azure Template Deployment via powershell: "Template parameter JToken type is not valid"

I am attempting to deploy a virtual machine via Powershell using ARM Templates.

I want to pass the SSH public key into the template using Template Parameters.

The parameters are defined in the template file as so:

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

And here is the powershell script where I load the public key from a file and add it as a template parameter.

$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

I get the following frustrating error:

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

At first I thought it was an issue with the string length. (The key has a length of 402 characters). But then if I replaced it with 402 consecutive 'A' characters I don't get the error.

I have another example which uses a template parameter file rather than a template parameter object, and this works too.

Is there a way I can get this to work?

Upvotes: 1

Views: 3071

Answers (1)

Andrew Shepherd
Andrew Shepherd

Reputation: 45262

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 }

Upvotes: 1

Related Questions