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.