我需要在通过 Azure PowerShell 模块更新记录时向 Azure 表存储实体添加新属性。我希望通过更新来实现这一点,而不是删除/添加新实体。
我当前拥有的脚本紧密遵循 Microsoft 的“更新实体示例”,并且非常适合更新现有属性。下面是他们示例的修改版本,我尝试添加新的“昵称”属性。
$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
正如您所料,PowerShell 会抛出错误,因为该行没有现有的昵称属性:
Exception setting "nickname": "The property 'nickname' cannot be found on this object. Verify that the property exists and
can be set.
删除/创建新实体是添加新属性的唯一方法吗?
Add-Member
添加新属性是有意义的。尝试改变
$user.nickname= "Jess"
到
Add-Member -InputObject $user -NotePropertyName 'nickname' -NotePropertyValue 'Jess'
或更短的
$user|add-member 'nickname' 'Jess'