Azure Storage Table Manipulation via PowerShell

Here’s a quick one. Azure Storage tables. I have a function that is called Add-Entity that will add rows to the table and the powershell that does the lifting.

# Function Add-Entity:  Adds an employee entity to a table

function Add-Entity() {
	$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $partitionKey $rowKey
	$entity.Properties.Add("Name", $name)
	$entity.Properties.Add("ID", $id)
	#Entity is new data row inserted into table
	$result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))

Select-AzureRMSubscription -SubscriptionName Pay-As-You-Go

#Define the storage account and context.
$StorageAccountName = "storage001"
$StorageAccountKey = Get-AzureRMStorageAccountKey -StorageAccountName $StorageAccountName -ResourceGroupName "Storage"
$Ctx = New-AxureStorageContext $StorageAccountName -StorageAccountKey $StorageAccountKey[0].value
$TableName = "Employees"

#Retrieve the table if it already exists
$table = Get-AzureStorageTable -Name $TableName -Context $Ctx -ErrorAction Ignore

#Create a new table if it does not exist
if ($table -eq $null)
	$table = New-AzureStorageTable -Name $TableName -Context $Ctx

#Add multiple entities to a table
Add-Entity -Table $table -PartitionKey Partition1 -RowKey Row1 -Name Chris -Id 1
Add-Entity -Table $table -PartitionKey Partition1 -RowKey Row2 -Name Jessie -Id 2
Add-Entity -Table $table -PartitionKey Partition2 -RowKey Row1 -Name Christine -Id 3
Add-Entity -Table $table -PartitionKey Partition2 -RowKey Row2 -Name Steven -Id 4

## Azure Storage Explorer to explore table

Leave a Reply

Your email address will not be published. Required fields are marked *