powershellazure-table-storageazure-powershell

How to add a property to Azure table storage row with Azure Powershell module


I need to add a new property to Azure table storage entities while updating records via Azure PowerShell module. I am hoping to achieve this through an update instead of deleting / adding a new entity.

The script I currently have follows Microsoft's updating entities example closely and works great for updating existing properties. Below is a modified version of their example where I attempt to add a new "nickname" property.

$user = Get-AzTableRow -table $cloudTable -customFilter $filter

# Change the entity.
$user.username = "Jessie2"

# Attempting to add a new property 
$user.nickname= "Jess"

# To commit the change, pipe the updated record into the update cmdlet.
$user | Update-AzTableRow -table $cloudTable

As you might expect PowerShell throws an error because the row does not have an existing nickname property: Exception setting "nickname": "The property 'nickname' cannot be found on this object. Verify that the property exists and can be set.

Is deleting / creating a new entity the only way to add a new property?


Solution

  • He says in his blog post that he converts the row entries into PSCustomObjects, so it makes sense to treat it like such and use Add-Member to add a new property. Try changing

    $user.nickname= "Jess"
    

    to

    Add-Member -InputObject $user -NotePropertyName 'nickname' -NotePropertyValue 'Jess'
    

    or the shorter

    $user|add-member 'nickname' 'Jess'