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

TemplateParameterObject Serializes Hashtables & PSCustomObject Incorrectly

Open abombss opened this issue 10 years ago • 13 comments

Name | Version AzureRM.Resources | 1.0.1

If a template takes a parameter of type=object there is no way to send that object via TemplateParameterObject or through dynamic powershell parameters.

Typically in powershell there are two common ways to represent custom objects and have them serialized and deserialized to and from json. That is using standard powershell hashtables or using pscustomobject.

Using either of these formats when calling New-AzureRmResourceGroupDeployment fails to properly serialize the parameters. So currently, there is no way through the cmdlets to invoke a template that has parameters of type object.

Template

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageSettings": {
            "type": "object"
        }
    },
    "variables": {
        "storageSettings": "[parameters('storageSettings')]",
        "name": "[variables('storageSettings').name]",
        "resourceGroup": "[variables('storageSettings').resourceGroup]",
        "resourceId": "[resourceId(variables('resourceGroup'), 'Microsoft.Storage/storageAccounts', variables('name'))]"
    },
    "resources": [],
    "outputs": {
        "storageAccountInfo": {
            "value": "[reference(variables('resourceId'), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0])]",
            "type": "object"
        }
    }
}
New-AzureRmResourceGroupDeployment -TemplateParameterObject @{
  storageSettings=@{
    name='foo'
    resourceGroup='bar'
  }
}

The above command serializes the parameters like this in the request which is invalid, using a dynamic parameter has the same effect.

"parameters": {
      "storageSettings": {
        "resourceGroup": {
          "value": "bar"
        },
        "name": {
          "value": "foo"
        }
      }
    },

Trying to use a powershell custom object actually serializes the object to an xml like string. I assume its the default output of serializing an pscustomobject with json.net.

New-AzureRmResourceGroupDeployment -storageSettings ([pscustomobject]@{
    name='foo'
    resourceGroup='bar'
  })
    "parameters": {
      "storageSettings": {
        "value": {
          "CliXml": "<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\">\r\n  <Obj RefId=\"0\">\r\n    <TN RefId=\"0\">\r\n      <T>System.Management.Automation.PSCustomObject</T>\r\n      <T>System.Object</T>\r\n    </TN>\r\n    <ToString>@{name=foo; resourceGroup=bar}</ToString>\r\n    <Obj RefId=\"1\">\r\n      <TNRef RefId=\"0\" />\r\n      <MS>\r\n        <S N=\"name\">foo</S>\r\n        <S N=\"resourceGroup\">bar</S>\r\n      </MS>\r\n    </Obj>\r\n    <MS>\r\n      <S N=\"name\">foo</S>\r\n      <S N=\"resourceGroup\">bar</S>\r\n    </MS>\r\n  </Obj>\r\n</Objs>"
        }
      }
    },

Please fix the serialization so complex parameter objects can be used as templateparameters, using either hashtables or pscustomobjects.

abombss avatar Dec 21 '15 06:12 abombss

@abombss - Can you please verify again? I was able to define the template parameter object as follows and use it successfully in my deployment - -TemplateParameterObject @{storageSettings = @{name='foo'; resourceGroup='bar'}}

ravbhatnagar avatar Feb 03 '16 01:02 ravbhatnagar

Have you tried to put all arguments on the same line as the cmdlet? This fixed it in our case.

pgroene avatar Feb 05 '16 12:02 pgroene

I am seeing the same issue when passing an array inside of the Hashtable when executed from Powershell workflow

this works

$Template =  'C:\Linked Templates\Test\azuredeploy.json'
Add-AzureRmAccount
Get-AzureRmSubscription
Select-AzureRmSubscription -SubscriptionId $subId

[arary]$VMName = @()
$VMName += "Test362016"
$VMName += "Test462016"
$Param = @{

VMName = $VMName

}

New-AzureRmResourceGroupDeployment -Name Test -ResourceGroupName sfiaas -TemplateParameterObject $Param -TemplateFile $Template

but this does not

Workflow Test {
$Template =  'C:\Linked Templates\Test\azuredeploy.json'
Add-AzureRmAccount
Get-AzureRmSubscription
Select-AzureRmSubscription -SubscriptionId $subId

[arary]$VMName = @()
$VMName += "Test362016"
$VMName += "Test462016"
$Param = @{

VMName = $VMName

}

New-AzureRmResourceGroupDeployment -Name Test -ResourceGroupName sfiaas -TemplateParameterObject $Param -TemplateFile $Template 
}
Test

robplank avatar Feb 07 '16 18:02 robplank

This is by design for now. Lets investigate the work to support this. Investigate.

ravbhatnagar avatar Feb 11 '16 22:02 ravbhatnagar

Have a similar issue when passing a large object (multi-level nested dictionary and array objects). The exception thrown is also similar (xml....)... The workaround I adopted was assign the final dictionary object (contains multiple key value pairs) I constructed by powershell to a single key-value dictionary (doing something like $dictobjparam = @{'value'=$originalMultiKeyValueDict} and then use the $dictobjparam as the parameter everything goes fine.

mignonsys avatar Jul 06 '16 05:07 mignonsys

Hi, has there been any progress on this?

If the ticket is still relevant, it would be interesting to know which formatting of an object is supported and what isn't.

AlexKeySmith avatar Jun 23 '17 11:06 AlexKeySmith

This isn't exactly passing a parameter of type "object" but passing an array as a parameter did the trick for me:

In PowerShell, make an array of hashtables and pass it to New-AzureRmResourceGroupDeployment

[array]$nicConfigs = @{name="nic1"; ip = "10.3.4.21"}
[array]$nicConfigs += @{name="nic2"; ip = "10.3.4.22"}

New-AzureRmResourceGroupDeployment -ResourceGroupName rgName `
-TemplateFile C:\Azure\JSON\nicCreateObjectLoopParam.json `
-nicConfigs $nicConfigs

in the JSON, set the parameter as array type:

  "parameters": {
    "nicConfigs": {
      "type": "array"
    }
  },

Then you can use them in your resource creation with copyindex like so: "privateIPAddress": "[parameters('nicConfigs')[copyIndex()].ip]"

gotmoo avatar Sep 01 '17 02:09 gotmoo

Adding to gotmoo's answer above, make sure the values of your hashtable are strings otherwise serialization does not happen properly

AyoOlubeko avatar Oct 02 '17 17:10 AyoOlubeko

@ravbhatnagar ping

cormacpayne avatar Jul 05 '18 23:07 cormacpayne

I am experiencing the XML serialization issue when trying to pass securestring parameters in the TemplateParameterObject. Specifically,

$secret = ConvertTo-SecureString "secret" -Force -AsPlainText
$templateParameters = @{
    secret = $secret
}

serializes to something like

"secret": {
    "value": {
        "CliXml": "<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\">\r\n  <SS>01000000d08c9ddf0115d1118c7a00c04fc297eb01000000729c4a38b9e1504a8553cb163e7db4ea0000000002
000000000003660000c000000010000000b69acba24d02a40845707fe228c2ea0c0000000004800000a0000000100000006114f7443d62aa35a0c346a98c5ddce020000000355b8cb6249eff99d0181349e023379da1bf8de70763f68e3af4d80163b1c2e0140
00000a5313293d68e6403f41d83fd16fbe9212d501a14</SS>\r\n</Objs>"
    }
}

It works to pass the parameter as a direct PowerShell command argument though.

barborico avatar Mar 04 '19 06:03 barborico

Ditto, I'm finding that using TemplateParameterObject with a hashtable containing a securestring fails with error saying the parameter (that is a secure string) is invalid. Using the @parameters, instead of TemplateParameterObject works

simonsabin avatar Apr 16 '19 11:04 simonsabin

Same as https://github.com/Azure/azure-powershell/issues/6292.

shenglol avatar Jul 24 '20 23:07 shenglol

This should be fixed in the latest powershell release

alex-frankel avatar Sep 23 '20 17:09 alex-frankel