locked
How do I iterate over each Row of a Table Storage using Powershell? RRS feed

  • Question

  • Hi

    This should be easy but I have spent hours on it today. All I want to do is iterate (like in a foreach or something) through each row of an Azure Table Storage in Powershell. I have this much code:

    $resourceGroup = "xxxxxxxxxxx"
    $storageAccount = "scadametrics"
    $tableName = "CompanyID"
    $SaContext = (Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
    $table = (Get-AzStorageTable -Name $tableName -Context $saContext).CloudTable

    Get-AzTableRow -table $table | ft

    This will print out each row, but I need to be able to read a value in each row so that I can assign it to $KeyName or something similar and then move on to the next row. I have googled for hours and can't find how to do this in PowerShell for Azure.  My reason for posting this in the ADF forum, is that I have populated my Table Storage with CompanyID's and ConnectionStrings that I am going to use to populate my Key Vault so that my ADFv2 Pipeline can utilize parameterization of the sinks.

    Thanks!
    Mike Kiser


    Mike Kiser

    Thursday, September 19, 2019 1:12 AM

All replies

  • This is what worked for me . I think you just need to add the foreach in your code and it should do the magic .

    Let us know how it goes 

    #Add-AzAccount

    Set-AzContext -SubscriptionId 'XXXXXsubIDxxxxxx';

    $ctx = New-AzStorageContext -StorageAccountName '---yourstorageaccount---' -StorageAccountKey 'key' 

    $table = (Get-AzStorageTable -Name 'testdoublesource' -Context $ctx).CloudTable

    #Just to check the entries in the Azure table

    Get-AzTableRow -table $table

    foreach($counter in Get-AzTableRow -table $table)

    echo('The values for temp are : ',$counter.temp)

    }


    Get-AzTableRow -table $table will give the below out ( all junk data :) ) 

    temp           : 199
    PartitionKey   : 
    RowKey         : 
    TableTimestamp : 4/8/2019 12:08:14 AM -07:00
    Etag           : W/"datetime'2019-04-08T07%3A08%3A14.7741665Z'"


    temp           : 10.22
    PartitionKey   : 1
    RowKey         : 1
    TableTimestamp : 4/7/2019 11:45:49 PM -07:00
    Etag           : W/"datetime'2019-04-08T06%3A45%3A49.5502459Z'"


    temp           : 199.99
    PartitionKey   : 1
    RowKey         : 2
    TableTimestamp : 4/8/2019 12:08:42 AM -07:00
    Etag           : W/"datetime'2019-04-08T07%3A08%3A42.2799977Z'"


    temp           : 10.22
    PartitionKey   : 1
    RowKey         : f308e079-8761-47be-b6e6-dc187d28961f
    TableTimestamp : 4/7/2019 11:53:51 PM -07:00
    Etag           : W/"datetime'2019-04-08T06%3A53%3A51.1121896Z'"

    foreach($counter in Get-AzTableRow -table $table)
    {
     echo('The values for temp are : ',$counter.temp)

    }

    The values for temp are : 
    199
    The values for temp are : 
    10.22
    The values for temp are : 
    199.99
    The values for temp are : 
    10.22


    Thanks Himanshu

    Thursday, September 26, 2019 1:34 AM