Question

I was happy to find the New-AzureStorageTable cmdlet, but I haven't figured out how to insert a new row in the table.

I found the following code on the internet, but the CloudTable.Execute seems to fail.

It needs three arguments as described in http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.cloudtable.execute(v=azure.10).aspx but I cannot figure out how to call the method.

Any ideas are appreciated!

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
  }
}

UPDATE

As suggested below you have to use Get-AzureStorageTable to check if the table already exists first.

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

Solution

Here's an alternate implementation using Storage Client Library (Microsoft.WindowsAzure.Storage.dll) only.

#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
  }
}

UPDATE

I just tried the code above and it works perfectly fine if the table does not exist in storage. I was able to reproduce the error you're getting if I try to run this script for a table which already exist in storage. I think this is what is happening: When you call New-AzureStorageTable to create a table, if the table already exists then it throws an error and returns a null value for $table variable and then when you try to execute InsertRow function using this null valued $table variable, you get error on Execute function. Ideally New-AzureStorageTable should not error out if the table already exists.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top