Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.27k stars 3.87k forks source link

Unable to use New-AzureRmResourceGroupDeployment with parameter -TemplateParameterObject #4186

Open kuomingwu opened 7 years ago

kuomingwu commented 7 years ago

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.

cormacpayne commented 7 years ago

@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 commented 7 years ago

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

wangduanyang commented 7 years ago

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

wangduanyang commented 7 years ago

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.

kuomingwu commented 7 years ago

@wangduanyang

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

Thanks

wangduanyang commented 7 years ago

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

KacperMucha commented 5 years ago

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.

koder101 commented 5 years ago

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."

0xW1sKy commented 4 years ago

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