3

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
}

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I did have to add the following to make it work: Add-Type -Path "C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\v2.2\ref\Microsoft.WindowsAzure.Storage.dll"
And is there a way to make it work using the New-AzureStorageTable?
Regarding 1st comment: I launched the PowerShell Console using Azure PowerShell Cmdlets shortcut which loaded everything for me so I didn't need to do add this library manually but otherwise yes you would need to do that. Regarding 2nd comment: For some reason I could not find the metadata of the object returned by New-AzureStorageTable. I can certainly give that a try once I know the kind of methods and properties the object exposes.
Updated my answer. HTH.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.