Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sample code for using AzureTableEntity PowerShell module
<#
================================================================================
AUTHOR: Tao Yang
DATE: 30/11/2016
Version: 1.0
Comment: Sample code for using AzureTableEntity PowerShell module
Project Repo: https://github.com/tyconsulting/AzureTableEntity-PowerShell-Module
================================================================================
#>
$StorageAccountName = '<Enter your storage account name>'
$TableName = '<Enter your Azure table name>'
$StorageAccountAccessKey = '<Enter your storage account access key>'
## Insert entities ##
#Bulk insert
$Entities = @()
$pro1 = @{
PartitionKey = 'test1'
RowKey = '1'
CustomerName = 'Eric Cartman'
dtDate = [datetime]::UtcNow
Age = 9
Alive = $true
}
$entity1 = New-Object psobject -Property $pro1
$pro2 = @{
PartitionKey = 'test1'
RowKey = '2'
CustomerName = 'Bart Simpson'
dtDate = [datetime]::UtcNow
Age = 10
Alive = $true
}
$entity2 = New-Object psobject -Property $pro2
$Entities += $entity1
$Entities += $entity2
$BulkInsert = New-AzureTableEntity -StorageAccountName $StorageAccountName -StorageAccountAccessKey $StorageAccountAccessKey -TableName $TableName -Entities $Entities -Verbose
#single insert
$pro3 = @{
PartitionKey = 'test2'
RowKey = '1'
CustomerName = 'Bart'
HourlyRate = 9.99
today = [datetime]::UtcNow
}
$entity3 = New-Object psobject -Property $pro3
$SingleInsert = New-AzureTableEntity -StorageAccountName $StorageAccountName -StorageAccountAccessKey $StorageAccountAccessKey -TableName $TableName -Entities $Entity3 -Verbose
## search entity ##
#return up to 1000 entities
$StartUTCDate = Get-Date -year 2016 -Month 11 -day 27 -Hour 11 -Minute 0 -Second 0 -Millisecond 0
$strStartUTCDate = $StartUTCDate.ToString('yyyy-MM-ddTHH:mm:ss.fffZ')
$QueryString = "(today ge datetime'$strStartUTCDate') and (PartitionKey eq 'test2')"
$SearchResult = Get-AzureTableEntity -StorageAccountName $StorageAccountName -StorageAccountAccessKey $StorageAccountAccessKey -TableName $TableName -QueryString $QueryString -GetAll $false -Verbose
#return all entities, and convert datetime fields
$StartUTCDate = Get-Date -year 2016 -Month 11 -day 27 -Hour 11 -Minute 0 -Second 0 -Millisecond 0
$strStartUTCDate = $StartUTCDate.ToString('yyyy-MM-ddTHH:mm:ss.fffZ')
$QueryString = "(dtDate ge datetime'$strStartUTCDate') and (PartitionKey eq 'test2')"
$SearchResult = Get-AzureTableEntity -StorageAccountName $StorageAccountName -StorageAccountAccessKey $StorageAccountAccessKey -TableName $TableName -QueryString $QueryString -ConvertDateTimeFields $true -GetAll $true -Verbose
## update entities ##
#updating single entity
$pro3 = @{
PartitionKey = 'test2'
RowKey = '1'
CustomerName = 'Bart Simpson'
today = [datetime]::UtcNow
}
$entity3 = New-Object psobject -Property $pro3
$SingleInsert = Update-AzureTableEntity -StorageAccountName $StorageAccountName -StorageAccountAccessKey $StorageAccountAccessKey -TableName $TableName -Entities $Entity3 -Verbose
#updating multiple entities
$Entities = @()
$pro1 = @{
PartitionKey = 'test1'
RowKey = '1'
CustomerName = 'Hello Kitty'
dtDate = [datetime]::UtcNow
}
$entity1 = New-Object psobject -Property $pro1
$pro2 = @{
PartitionKey = 'test1'
RowKey = '2'
CustomerName = 'Homer Simpson'
dtDate = [datetime]::UtcNow
}
$entity2 = New-Object psobject -Property $pro2
$Entities += $entity1
$Entities += $entity2
$BulkUpdate = Update-AzureTableEntity -StorageAccountName $StorageAccountName -StorageAccountAccessKey $StorageAccountAccessKey -TableName $TableName -Entities $Entities -Verbose
## Remove entities ##
#removing single entity
$pro3 = @{
PartitionKey = 'test2'
RowKey = '1'
}
$entity3 = New-Object psobject -Property $pro3
$SingleRemoval = Remove-AzureTableEntity -StorageAccountName $StorageAccountName -StorageAccountAccessKey $StorageAccountAccessKey -TableName $TableName -Entities $Entity3 -Verbose
#Removing multiple entities
$Entities = @()
$pro1 = @{
PartitionKey = 'test1'
RowKey = '1'
}
$entity1 = New-Object psobject -Property $pro1
$pro2 = @{
PartitionKey = 'test1'
RowKey = '2'
}
$entity2 = New-Object psobject -Property $pro2
$Entities += $entity1
$Entities += $entity2
$BulkRemoval = Remove-AzureTableEntity -StorageAccountName $StorageAccountName -StorageAccountAccessKey $StorageAccountAccessKey -TableName $TableName -Entities $Entities -Verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment