aberdeen angus
aberdeen angus

Reputation: 191

Azure template to create automation account variable won't accept strings, but integers ok

Please help me before I go completely mad and bite one of my arms off! I'm trying to use an azure template to create a variable in an automation account. My little template is creating integer variables ok, but I need a string. I'm getting the error message:

New-AzureRmResourceGroupDeployment : 22:24:06 - Resource Microsoft.Automation/automationAccounts/variables 'Start-Stop-VMs-Test/myVariableName' failed with message '{ "code": "BadRequest", "message": "{\"Message\":\"The request is invalid.\",\"ModelState\":{\"variable.properties.value\":[\"Invalid JSON primitive: myVariableValue.\"]}}" }'

The automation account already exists by the way, I don't want to create or reconfigure it so I want to keep it out of the template. I want my VM template to create a string variable that will be used by a DSC configuration to finish of the VM build.

So creating an integer variable like this works ok, although it's completely useless to me:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [
        {
            "type": "Microsoft.Automation/automationAccounts/variables",
            "name": "Start-Stop-VMs-Test/myVariableName",
            "apiVersion": "2015-10-31",
            "location": "westeurope",
            "properties": {
                "description": "myVariableDesc",
                "value": 17
            }
        }
    ]
}

However trying to create a string variable like this fails with the "Invalid JSON primitive" message:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [
        {
            "type": "Microsoft.Automation/automationAccounts/variables",
            "name": "Start-Stop-VMs-Test/myVariableName",
            "apiVersion": "2015-10-31",
            "location": "westeurope",
            "properties": {
                "description": "myVariableDesc",
                "value": "myVariableValue"
            }
        }
    ]
}

I've tried adding another property "type": "string" but this makes no difference, and I don't see it mentioned in https://learn.microsoft.com/en-us/azure/templates/microsoft.automation/automationaccounts/variables. My automation account is called "Start-Stop-VMs-Test" of course. Maybe I'll try and get a job as a bus driver.

Upvotes: 0

Views: 1490

Answers (2)

aberdeen angus
aberdeen angus

Reputation: 191

I seem to have stumbled across the answer. If you want a string variable you have to put it inside single quotes, and put all of that inside double quotes for the json. I think that's a bit weird actually, if it can tell that 17 is an integer, why can't it tell that myVariableValue is a string? So a working template is below, note the single quotes inside the double quotes around the variable value. I wish Microsoft had documented that, I lost at least an hour. "\"myVariableValue\"" also works by the way.

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [
        {
            "type": "Microsoft.Automation/automationAccounts/variables",
            "name": "Start-Stop-VMs-Test/myVariableName",
            "apiVersion": "2015-10-31",
            "location": "westeurope",
            "properties": {
                "description": "myVariableDesc",
                "value": "'myVariableValue'"
            }
        }
    ]
}

Upvotes: 4

Jason Ye
Jason Ye

Reputation: 13954

"name": "Start-Stop-VMs-Test/myVariableName",

Because a runbook name can contain only letters, numbers, underscores, and dashes, and must begin with a letter(not support /).

Please try to use this name "Start-stop-VMs-Test-myVariableName".

enter image description here

Upvotes: 1

Related Questions