azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Unable to use New-AzureRmResourceGroupDeployment with parameter -TemplateParameterObject

Open kuomingwu opened this issue 9 years ago • 11 comments

Hi,

We have some issue need your help :

Issue describe when we use New-AzureRmResourceGroupDeployment with parameter -TemplateParameterObject such as : New-AzureRmResourceGroupDeployment -ResourceGroupName test -TemplateFile "C:\Users\a-mowu\Desktop\temp20170622\azuredeploy.json" -TemplateParameterObject $h

the $h is hashtable such as

PS C:\WINDOWS\system32> $h

Name				Value
----				-----
contentVersion		1.0.0.0
$schema				http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#
parameters 			{adminPassword, dnsLabelPrefix, adminUsername}



PS C:\WINDOWS\system32> $h.parameters

Name				Value
----				-----
adminPassword		@{value=xxxxxxxxxxxxxx}
dnsLabelPrefix		@{value=xxxx}
adminUsername		@{value=xxxxxxxxxx}



PS C:\WINDOWS\system32> $h.parameters.adminUsername

Value
-----
xxxxxxxxxx

but when we execute the script, it show need to type parameter as we had set :

image

so we change the parameter $h to $h.parameter such as -TemplateParameterObject $h.parameters, it skip the type action and this situation seems normal, but it occur error :

image

so we check any parameter and test to change $h(hashtable) to file(json) then modify parameter to -TemplateParameterFile "C:\Users\a-mowu\Desktop\temp20170622\temp.json" and provision VM successful :

image

ASK but we would like to ask, why we unable to use parameter TemplateParameterObject, thanks.

kuomingwu avatar Jun 23 '17 11:06 kuomingwu

@kuomingwu as an FYI, I removed the first image and replaced it with a PowerShell code block because the image contained values that you blurred out in the last picture

cormacpayne avatar Jun 23 '17 22:06 cormacpayne

@vivsriaus Hey Vivek, would you mind taking a look at this issue?

cormacpayne avatar Jun 23 '17 22:06 cormacpayne

I got the same issue, could anyone give me an answer?

wangduanyang avatar Jun 26 '17 02:06 wangduanyang

i just made another test which works fine.

$SecurePass = ConvertTo-SecureString $password -AsPlainText -Force $ParametersObj = @{
adminUsername = "yang" adminPassword = "$SecurePass" dnsLabelPrefix ="yangtestvm062333" } New-AzureRmResourceGroupDeployment -ResourceGroupName yangTest -TemplateFile "D:\desktop\azuredeploy.json" -TemplateParameterObject $ParametersObj

But i have to say, the previous error prompt makes me confused.

wangduanyang avatar Jun 26 '17 04:06 wangduanyang

@wangduanyang

Do you delete node "value" from the hash table and provision successful ?

Thanks

kuomingwu avatar Jun 26 '17 04:06 kuomingwu

@kuomingwu Yes, it is working when delete "value" key.

wangduanyang avatar Jun 28 '17 02:06 wangduanyang

The issue is quite old, so I'm not sure if the method for passing passing TemplateParameterObject hasn't changed etc., but I have never created an object that has properties like contentVersion, $schema, parameters and then in parameters property all the params that I need. I'm always just providing a plain hashtable to this parameter, so it looks like this:

$params = @{
    adminUsername = 'admin'
    adminPassword = 'somepassword'
    dnsLabelPrefix = 'someprefix'
}

New-AzureRmResourceGroupDeployment -ResourceGroupName 'test' -TemplateFile 'C:\azuredeploy.json' -TemplateParameterObject $params

I don't recall having any problems with this syntax.

KacperMucha avatar Mar 27 '19 09:03 KacperMucha

i just made another test which works fine.

$SecurePass = ConvertTo-SecureString $password -AsPlainText -Force $ParametersObj = @{ adminUsername = "yang" adminPassword = "$SecurePass" dnsLabelPrefix ="yangtestvm062333" } New-AzureRmResourceGroupDeployment -ResourceGroupName yangTest -TemplateFile "D:\desktop\azuredeploy.json" -TemplateParameterObject $ParametersObj

But i have to say, the previous error prompt makes me confused.

But the same syntax is not working for me. I am getting error - "Missing an argument for parameter 'TemplateParameterObject'. Specify a parameter of type 'System.Collections.Hashtable' and try again."

koder101 avatar Apr 07 '19 21:04 koder101

2020 and i'm hitting this today.

it looks like the specification for a parameter file != the actual json->hashtable format you are looking for in the command.

based on reading this thread it looks like for the json file you accept the following format:

{
"parameters": {
        "parameter": {
            "value": "string"
        }
}

when directly translated to a hashtable this would look like:

$hashtable = @{parameters=@{parameter=@{value="string"}}}

however, it appears that the command asks us to supply our parameter object like so:

$hashtable = @{parameter="string"}

which when translated back to json would look like:

{
    "parameter": "string"
}

because of this we can not easily move back and forth between a json file to hashtable for this function. Also it is not very intuitive... A major limitation for this is that it makes it orders of magnitude more difficult to put things like the json config file from the azure console directly into our powershell scripts with light modifications.

i'd like to request that a single syntax is chosen for the parameter styling so that we can move seamlessly between json <-> hashtable OR support both options...

expected functionality in full view:

$azureParameters = @"
{
    "`$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "value": "$location"
        },
        "networkInterfaceName": {
            "value": "$nicname"
        },
        "networkSecurityGroupName": {
            "value": "$sgname"
        },
        "networkSecurityGroupRules": {
            "value": [
                {
                    "name": "SSH",
                    "properties": {
                        "priority": 300,
                        "protocol": "TCP",
                        "access": "Allow",
                        "direction": "Inbound",
                        "sourceAddressPrefix": "*",
                        "sourcePortRange": "*",
                        "destinationAddressPrefix": "*",
                        "destinationPortRange": "22"
                    }
                }
            ]
        },
        "subnetName": {
            "value": "default"
        },
        "virtualNetworkId": {
            "value": "$vnetid"
        },
        "publicIpAddressName": {
            "value": "$pubAddressName"
        },
        "publicIpAddressType": {
            "value": "Dynamic"
        },
        "publicIpAddressSku": {
            "value": "Basic"
        },
        "virtualMachineName": {
            "value": "$vmname"
        },
        "virtualMachineComputerName": {
            "value": "$vmname"
        },
        "virtualMachineRG": {
            "value": "$resourceGroup"
        },
        "osDiskType": {
            "value": "Premium_LRS"
        },
        "virtualMachineSize": {
            "value": "Standard_B1s"
        },
        "adminUsername": {
            "value": "$username"
        },
        "adminPassword": {
            "value": "$password"
        }
    }
}
"@  | ConvertFrom-Json -Depth 100 -AsHashtable

$azureTemplate = @"
{
    "`$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string"
        },
        "networkInterfaceName": {
            "type": "string"
        },
        "networkSecurityGroupName": {
            "type": "string"
        },
        "networkSecurityGroupRules": {
            "type": "array"
        },
        "subnetName": {
            "type": "string"
        },
        "virtualNetworkId": {
            "type": "string"
        },
        "publicIpAddressName": {
            "type": "string"
        },
        "publicIpAddressType": {
            "type": "string"
        },
        "publicIpAddressSku": {
            "type": "string"
        },
        "virtualMachineName": {
            "type": "string"
        },
        "virtualMachineComputerName": {
            "type": "string"
        },
        "virtualMachineRG": {
            "type": "string"
        },
        "osDiskType": {
            "type": "string"
        },
        "virtualMachineSize": {
            "type": "string"
        },
        "adminUsername": {
            "type": "string"
        },
        "adminPassword": {
            "type": "secureString"
        }
    },
    "variables": {
        "nsgId": "[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]",
        "vnetId": "[parameters('virtualNetworkId')]",
        "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
    },
    "resources": [
        {
            "name": "[parameters('networkInterfaceName')]",
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2019-07-01",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]",
                "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
            ],
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            },
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIpAddress": {
                                "id": "[resourceId(resourceGroup().name, 'Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]"
                            }
                        }
                    }
                ],
                "networkSecurityGroup": {
                    "id": "[variables('nsgId')]"
                }
            }
        },
        {
            "name": "[parameters('networkSecurityGroupName')]",
            "type": "Microsoft.Network/networkSecurityGroups",
            "apiVersion": "2019-02-01",
            "location": "[parameters('location')]",
            "properties": {
                "securityRules": "[parameters('networkSecurityGroupRules')]"
            }
        },
        {
            "name": "[parameters('publicIpAddressName')]",
            "type": "Microsoft.Network/publicIpAddresses",
            "apiVersion": "2019-02-01",
            "location": "[parameters('location')]",
            "properties": {
                "publicIpAllocationMethod": "[parameters('publicIpAddressType')]"
            },
            "sku": {
                "name": "[parameters('publicIpAddressSku')]"
            }
        },
        {
            "name": "[parameters('virtualMachineName')]",
            "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2019-07-01",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
            ],
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('virtualMachineSize')]"
                },
                "storageProfile": {
                    "osDisk": {
                        "createOption": "fromImage",
                        "managedDisk": {
                            "storageAccountType": "[parameters('osDiskType')]"
                        },
                        "diskSizeGB": 30
                    },
                    "imageReference": {
                        "publisher": "Canonical",
                        "offer": "UbuntuServer",
                        "sku": "18.04-LTS",
                        "version": "latest"
                    }
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]"
                        }
                    ]
                },
                "osProfile": {
                    "computerName": "[parameters('virtualMachineComputerName')]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "adminPassword": "[parameters('adminPassword')]"
                }
            }
        }
    ],
    "outputs": {
        "adminUsername": {
            "type": "string",
            "value": "[parameters('adminUsername')]"
        }
    }
}
"@  | ConvertFrom-Json -Depth 100 -AsHashtable

New-AzResourceGroupDeployment -ResourceGroupName $resourcegroup -TemplateObject $azureTemplate -TemplateParameterObject $azureParameters

current functionality full view

$azureParameters = @"
{
    "`$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "value": "$location"
        },
        "networkInterfaceName": {
            "value": "$nicname"
        },
        "networkSecurityGroupName": {
            "value": "$sgname"
        },
        "networkSecurityGroupRules": {
            "value": [
                {
                    "name": "SSH",
                    "properties": {
                        "priority": 300,
                        "protocol": "TCP",
                        "access": "Allow",
                        "direction": "Inbound",
                        "sourceAddressPrefix": "*",
                        "sourcePortRange": "*",
                        "destinationAddressPrefix": "*",
                        "destinationPortRange": "22"
                    }
                }
            ]
        },
        "subnetName": {
            "value": "default"
        },
        "virtualNetworkId": {
            "value": "$vnetid"
        },
        "publicIpAddressName": {
            "value": "$pubAddressName"
        },
        "publicIpAddressType": {
            "value": "Dynamic"
        },
        "publicIpAddressSku": {
            "value": "Basic"
        },
        "virtualMachineName": {
            "value": "$vmname"
        },
        "virtualMachineComputerName": {
            "value": "$vmname"
        },
        "virtualMachineRG": {
            "value": "$resourceGroup"
        },
        "osDiskType": {
            "value": "Premium_LRS"
        },
        "virtualMachineSize": {
            "value": "Standard_B1s"
        },
        "adminUsername": {
            "value": "$username"
        },
        "adminPassword": {
            "value": "$password"
        }
    }
}
"@  | ConvertFrom-Json -Depth 100 -AsHashtable

$azureTemplate = @"
{
    "`$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "location": {
            "type": "string"
        },
        "networkInterfaceName": {
            "type": "string"
        },
        "networkSecurityGroupName": {
            "type": "string"
        },
        "networkSecurityGroupRules": {
            "type": "array"
        },
        "subnetName": {
            "type": "string"
        },
        "virtualNetworkId": {
            "type": "string"
        },
        "publicIpAddressName": {
            "type": "string"
        },
        "publicIpAddressType": {
            "type": "string"
        },
        "publicIpAddressSku": {
            "type": "string"
        },
        "virtualMachineName": {
            "type": "string"
        },
        "virtualMachineComputerName": {
            "type": "string"
        },
        "virtualMachineRG": {
            "type": "string"
        },
        "osDiskType": {
            "type": "string"
        },
        "virtualMachineSize": {
            "type": "string"
        },
        "adminUsername": {
            "type": "string"
        },
        "adminPassword": {
            "type": "secureString"
        }
    },
    "variables": {
        "nsgId": "[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]",
        "vnetId": "[parameters('virtualNetworkId')]",
        "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
    },
    "resources": [
        {
            "name": "[parameters('networkInterfaceName')]",
            "type": "Microsoft.Network/networkInterfaces",
            "apiVersion": "2019-07-01",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]",
                "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
            ],
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "subnet": {
                                "id": "[variables('subnetRef')]"
                            },
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIpAddress": {
                                "id": "[resourceId(resourceGroup().name, 'Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]"
                            }
                        }
                    }
                ],
                "networkSecurityGroup": {
                    "id": "[variables('nsgId')]"
                }
            }
        },
        {
            "name": "[parameters('networkSecurityGroupName')]",
            "type": "Microsoft.Network/networkSecurityGroups",
            "apiVersion": "2019-02-01",
            "location": "[parameters('location')]",
            "properties": {
                "securityRules": "[parameters('networkSecurityGroupRules')]"
            }
        },
        {
            "name": "[parameters('publicIpAddressName')]",
            "type": "Microsoft.Network/publicIpAddresses",
            "apiVersion": "2019-02-01",
            "location": "[parameters('location')]",
            "properties": {
                "publicIpAllocationMethod": "[parameters('publicIpAddressType')]"
            },
            "sku": {
                "name": "[parameters('publicIpAddressSku')]"
            }
        },
        {
            "name": "[parameters('virtualMachineName')]",
            "type": "Microsoft.Compute/virtualMachines",
            "apiVersion": "2019-07-01",
            "location": "[parameters('location')]",
            "dependsOn": [
                "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
            ],
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[parameters('virtualMachineSize')]"
                },
                "storageProfile": {
                    "osDisk": {
                        "createOption": "fromImage",
                        "managedDisk": {
                            "storageAccountType": "[parameters('osDiskType')]"
                        },
                        "diskSizeGB": 30
                    },
                    "imageReference": {
                        "publisher": "Canonical",
                        "offer": "UbuntuServer",
                        "sku": "18.04-LTS",
                        "version": "latest"
                    }
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]"
                        }
                    ]
                },
                "osProfile": {
                    "computerName": "[parameters('virtualMachineComputerName')]",
                    "adminUsername": "[parameters('adminUsername')]",
                    "adminPassword": "[parameters('adminPassword')]"
                }
            }
        }
    ],
    "outputs": {
        "adminUsername": {
            "type": "string",
            "value": "[parameters('adminUsername')]"
        }
    }
}
"@  | ConvertFrom-Json -Depth 100 -AsHashtable

### Stupid Fix
$bogusparameterobject = @{ }
$azureparameters.parameters.keys | % { $bogusparameterobject[$_] = $azureparameters.parameters[$_]['value'] }

New-AzResourceGroupDeployment -ResourceGroupName $resourcegroup -TemplateObject $azureTemplate -TemplateParameterObject $bogusparameterobject

TL;DR

for anyone else that hits this issue here is your fix:

$azureparameters = get-content c:\your\parameter\file.json | convertfrom-json -ashashtable -depth 100
$bogusparameterobject = @{ }
$azureparameters.parameters.keys | % { $bogusparameterobject[$_] = $azureparameters.parameters[$_]['value'] }

# -TemplateParameterObject $bogusParameterObject

0xW1sKy avatar Jun 10 '20 04:06 0xW1sKy

:bell: Routing this issue to @act-identity-squad.

Thanks for your patience here — apologies for the very long wait. This was recently picked up as an outstanding open issue for the ARM Deployments team to review.

Two parts to this, and the first is a straight answer (thanks @wangduanyang and @KacperMucha for landing on it years ago): -TemplateParameterObject expects a flat hashtable of name = value, not the full parameters-file shape with $schema / contentVersion / parameters / value wrappers. I verified both on Az.Resources 9.0.3:

# ✅ works
New-AzResourceGroupDeployment -ResourceGroupName $rg -TemplateFile .\azuredeploy.json `
    -TemplateParameterObject @{ company = 'FlatCo'; location = 'eastus' }

# ❌ the full file shape gets treated as parameters named $schema/contentVersion/parameters:
#   InvalidTemplate ... "The following parameters were supplied, but do not correspond to any
#   parameters defined in the template: '$schema, contentVersion, parameters'"

So if you have a parameters JSON file and want to feed it via -TemplateParameterObject, flatten it first:

$raw  = Get-Content .\azuredeploy.parameters.json -Raw | ConvertFrom-Json
$flat = @{}
$raw.parameters.PSObject.Properties | ForEach-Object { $flat[$_.Name] = $_.Value.value }
New-AzResourceGroupDeployment -ResourceGroupName $rg -TemplateFile .\azuredeploy.json -TemplateParameterObject $flat

(Or just use -TemplateParameterFile .\azuredeploy.parameters.json directly, which takes the file shape as-is.)

The second part is @0xW1sKy's 2020 point, which is a legitimate request: it'd be nicer if -TemplateParameterObject also accepted the parameters-file shape so you can round-trip JSON↔hashtable without re-shaping. That's a reasonable usability enhancement we'll look into. Would the flatten helper above unblock you for now, while we consider native file-shape support?

Closing this out and will open/link another issue in the Bicep repo to track this.

torreymicrosoft avatar Jun 08 '26 22:06 torreymicrosoft