Patrick Lamber
Patrick Lamber IT Geek focusing on Microsoft collaboration software.

Azure Table Storage CRUD operations with PowerShell

In the past months I worked on several projects that automated the data collection of local IT assets. During this time, we decided to use the Azure Table Storage service for storing data.

During this time, there was no easy way for me to perform CRUD operations with PowerShell. Meanwhile, you will be able to use the official Microsoft project Az.Storage. Nevertheless, I wanted to share the module that we are currently successfully using in our projects. I conveniently stored the project for you under this Github repository.

Usage

Find here a summary how you can use this Module.

  • Get the AZTableModule.psm1 and store it to your desired location
  • Get your Azure Storage name and key. You will need it in the script

Initializing the module

$storage = "addYourStorageNameHere"
$key = "addYourStorageSecretKeyHere"

Import-Module AZTableModule.psm1 -ArgumentList $storage, $key

Creating a new table

New-AzTable "sampletable"

Add a new entry to your table

  • Dates must be older or equal than “1901-01-01”
  • Replaces the entry if the unique key and partitionkey matches
    $birthDate = (Get-date -date "1983-01-02")
    $patrick = @{
      PartitionKey = 'yourPartitionName'
      RowKey = 'yourUniqueRowKeyPatrick'
      "birthDate@odata.type"="Edm.DateTime"
      birthDate = $birthDate.toString("yyyy-MM-ddT00:00:00.000Z")
      name = "Patrick"
      lastname = "Lamber"
    }
    Add-AzTableEntry -table "sampletable" -partitionKey $patrick.PartitionKey -rowKey $patrick.RowKey -entity $patrick
    

    Create a new entry or merge it with an existing one

    $birthDate = (Get-date -date "1986-10-19")
    $rene = @{<br />
      PartitionKey = 'yourPartitionName'
      RowKey = 'yourUniqueRowKeyRene'
      "birthDate@odata.type"="Edm.DateTime"
      birthDate = $birthDate.toString("yyyy-MM-ddT00:00:00.000Z")
      name = "Rene'"
      lastname = "Lamber
    }
    Merge-AzTableEntry -table "sampletable" -partitionKey $rene.PartitionKey -rowKey $rene.RowKey -entity $rene
    

    Return a single entry

    $patrickFromTheCloud = Get-AzTableEntry -table "sampletable" -partitionKey $patrick.PartitionKey -rowKey $patrick.RowKey
    

    Update an individual field of an existing entry

    $patrickFromTheCloud = Get-AzTableEntry -table "sampletable" -partitionKey $patrick.PartitionKey -rowKey $patrick.RowKey
    $patrickFromTheCloud.name = "Patrick has been updated"
    Merge-AzTableEntry -table "sampletable" -partitionKey $patrickFromTheCloud.PartitionKey -rowKey $patrickFromTheCloud.RowKey -entity $patrickFromTheCloud
    

    Get all entries

    $entries = Get-AzTableEntries -table "sampletable"
    

Select individual fields from the table

$entriesWithSomeProperties = Get-AzTableEntries -table "sampletable" -select "RowKey,PartitionKey,name"

Filter entries

$filteredEntries = Get-AzTableEntries -table "sampletable" -filter "name eq 'Patrick'"

Delete an entry

Remove-AzTableEntry -table "sampletable" -partitionKey $rene.PartitionKey -rowKey $rene.RowKey

Delete a table

Remove-AzTable -table "sampletable"

comments powered by Disqus