如何使用PowerShell在Azure存储表中插入行

如何使用PowerShell在Azure存储表中插入行,azure,powershell-3.0,azure-table-storage,Azure,Powershell 3.0,Azure Table Storage,我很高兴找到新的AzureStorageTable cmdlet,但我还没有弄清楚如何在表中插入新行 我在internet上找到了以下代码,但是CloudTable.Execute似乎失败了 它需要三个参数,如中所述,但我不知道如何调用该方法 任何想法都很感激 function InsertRow($table, [String]$partitionKey, [String]$rowKey, [int]$intValue) { $entity = New-Object "Microsoft.

我很高兴找到新的AzureStorageTable cmdlet,但我还没有弄清楚如何在表中插入新行

我在internet上找到了以下代码,但是CloudTable.Execute似乎失败了

它需要三个参数,如中所述,但我不知道如何调用该方法

任何想法都很感激

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 = "MyAccountName"
$StorageAccountKey = "MyAccountKey"

$context = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$table = New-AzureStorageTable test -Context $context

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

正如下面所建议的,您必须首先使用Get-AzureStorageTable检查该表是否已经存在

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

这里有一个仅使用存储客户端库(
Microsoft.WindowsAzure.Storage.dll
)的替代实现

更新


我刚刚尝试了上面的代码,如果存储中不存在该表,它就可以正常工作。如果我尝试为存储中已经存在的表运行此脚本,我可以重现您遇到的错误。我想这就是正在发生的事情:当您调用
newazurestoragetable
创建一个表时,如果该表已经存在,那么它会抛出一个错误,并为
$table
变量返回
null
值,然后当您尝试使用该
null
$table
变量执行
InsertRow
函数时,您会在
执行
函数上得到错误。理想情况下,如果新AzureStorageTable已存在,则不应出错。

谢谢!我必须添加以下内容才能正常工作:添加类型-路径“C:\Program Files\Microsoft SDK\Windows Azure\.NET SDK\v2.2\ref\Microsoft.WindowsAzure.Storage.dll”有没有办法使用新的AzureStorageTable使其正常工作?关于第一条评论:我使用Azure PowerShell cmdlet快捷方式启动了PowerShell控制台,该快捷方式为我加载了所有内容,因此我不需要手动添加此库,但如果没有,您需要这样做。关于第二条评论:由于某种原因,我找不到New AzureStorageTable返回的对象的元数据。一旦我知道对象公开的方法和属性类型,我当然可以尝试一下。更新了我的答案。嗯,你说得对!我已经更新了我的问题。谢谢。非常感谢!
#Insert row ... here $table is an object of type CloudTable
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.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}


$StorageAccountName = "accountname"
$StorageAccountKey = "accountkey"
$tableName = "MyTable3"

#Create instance of storage credentials object using account name/key
$accountCredentials = New-Object "Microsoft.WindowsAzure.Storage.Auth.StorageCredentials" $StorageAccountName, $StorageAccountKey

#Create instance of CloudStorageAccount object
$storageAccount = New-Object "Microsoft.WindowsAzure.Storage.CloudStorageAccount" $accountCredentials, $true

#Create table client
$tableClient = $storageAccount.CreateCloudTableClient()

#Get a reference to CloudTable object
$table = $tableClient.GetTableReference($tableName)

#Try to create table if it does not exist
$table.CreateIfNotExists()

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