用于天蓝色表存储的Powershell更新实体命令

时间:2016-01-24 18:25:49

标签: powershell azure azure-table-storage

我需要一些关于在PowerShell中使用azure表存储的示例或教程。我知道如何创建表,插入实体和显示实体。但是,任何人都可以给我任何更新案例的方向。如何在PowerShell中更新表中的现有实体?如何在PowerShell中检查/访问表中实体的特定列?

添加实体的脚本:

function InsertRow($table, [String]$partitionKey, [String]$rowKey, [int]$intValue)
{
  $entity = New-Object "Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity" $partitionKey, $rowKey
  $entity.Properties.Add("IntValue", $intValue)
  $result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}

$StorageAccountName = "storageName"
$StorageAccountKey = "StorageKey"

$context = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

$tablename = "test"

$table = Get-AzureStorageTable $tablename -Context $context -ErrorAction Ignore
if ($table -eq $null)
{
    New-AzureStorageTable $tablename -Context $context
}

for ($p = 1; $p -le 1; $p++)
{
  for ($r = 1; $r -le 1; $r++)
  {
    InsertRow $table "P$p" "R$r" $r
  }
}

显示表中所有实体的脚本:

$query = New-Object Microsoft.WindowsAzure.Storage.Table.TableQuery

#Define columns to select.
$list = New-Object System.Collections.Generic.List[string]
$list.Add("RowKey")
$list.Add("IntValue")

#Set query details.
$query.SelectColumns = $list
$query.TakeCount = 20

#Execute the query.
$entities = $table.CloudTable.ExecuteQuery($query)

#Display entity properties with the table format.
$entities  | Format-Table PartitionKey, RowKey, @{ Label = "IntValue"; Expression={$_.Properties["IntValue"].Int32Value}} -AutoSize

我还需要命令来更新特定实体。任何帮助?

1 个答案:

答案 0 :(得分:1)

知道了。通过$entity.Properties["IntValue"].Int32Value改变实体的价值 然后使用此命令更新该实体:

$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::InsertOrReplace($entity))

$Query.FilterString可用于从表中获取特定实体。