Retrieving Last Modified Dates for All Tables in Azure Storage Account

Niket Kumar Singh 455 Reputation points
2025-03-10T14:16:07.7733333+00:00

obtaining the last modified dates for all tables within our Azure Storage account. The objective is to monitor and manage data effectively without making any modifications to the existing data.

e have attempted to use Azure PowerShell cmdlets to achieve this. Our approach included:​

  1. Setting the Azure Subscription Context:

Set-AzContext -SubscriptionId "<Your Subscription ID>"
  1. Retrieving the Storage Account Key:

$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName "<Your Resource Group>" -Name "<Your Storage Account>").Value[0]
  1. Creating a Storage Context:

$storageContext = New-AzStorageContext -StorageAccountName "<Your Storage Account>" -StorageAccountKey $storageAccountKey
  1. Iterating Over Each Table to Fetch Entities and Determine the Last Modified Date:

$tables = Get-AzStorageTable -Context $storageContext
foreach ($table in $tables) {
    $entities = Get-AzTableRow -TableName $table.Name -Context $storageContext | Sort-Object Timestamp -Descending
    $latestTimestamp = $entities[0].Timestamp.DateTime.ToString("yyyy-MM-dd HH:mm:ss")
    Write-Output "Table: $($table.Name) | Last Modified: $latestTimestamp"
}

Challenges Encountered:

  • The Get-AzTable cmdlet is not recognized, indicating that the AzTable module may not be installed or imported correctly.​
  • Errors related to parameter sets not being recognized, leading to execution failures.​

Objective:

We seek guidance on:

Module Installation and Import: How can we ensure that the AzTable module is installed and imported correctly in our PowerShell environment?​

Retrieving Last Modified Dates: What is the recommended approach to retrieve the last modified dates for all tables in an Azure Storage account using PowerShell?​

Ensuring Read-Only Operations: How can we confirm that the script performs only read-only operations to prevent any unintended data modifications?​

Error Handling: What are the best practices for handling errors and exceptions in this context to ensure the script runs smoothly?​

Additional Information:

  • We have already defined the storage account name, resource group name, and subscription ID.​
  • We are operating within the Azure Cloud Shell environment, which should have the necessary modules pre-installed.​

Any insights, code snippets, or references to relevant documentation would be greatly appreciated.

Thank you for your assistance.e have attempted to use Azure PowerShell cmdlets to achieve this.

Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,414 questions
{count} votes

Accepted answer
  1. Venkatesan S 585 Reputation points Microsoft External Staff
    2025-03-12T14:55:12.34+00:00

    Hi @Niket Kumar Singh

    Retrieving Last Modified Dates for All Tables in Azure Storage Account

    You can use the below PowerShell code that retrieves the Azure table storage entities with last modified table.

    Make sure you have assigned the Storage Table Data Contributor role to your user for the storage account.

    Script:

    Connect-Azaccount -Subscription "xxx"
    
    $resourceGroup = "xxxx"
    $storageAccount = "xxxxx"
    
    try {
        # Get the storage account key
        $storageKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $storageAccount -ErrorAction Stop).Value[0]
        
        # Create a storage context
        $storageContext = New-AzStorageContext -StorageAccountName $storageAccount -StorageAccountKey $storageKey -ErrorAction Stop
        
        # Get all table names in the storage account
        $tables = Get-AzStorageTable -Context $storageContext
    
        # Iterate through each table and fetch rows
        foreach ($table in $tables) {
            $tableName = $table.CloudTable.Name
            $CloudTable = $table.CloudTable
    
            Write-Host "Fetching rows from table: $tableName"
    
            # Get all rows from the table and sort by Timestamp in descending order
            $entities = Get-AzTableRow -Table $CloudTable | 
                        Select-Object @{Name="TableName"; Expression={$tableName}}, PartitionKey, RowKey, TableTimestamp, Property1, Property2, ETag |
                        Sort-Object TableTimestamp -Descending
    
            # Output results
            if ($entities) {
                $entities | Format-Table -AutoSize
            } else {
                Write-Host "No data found in table: $tableName"
            }
        }
    }
    catch {
        Write-Error "An error occurred: $_"
    }
    

    Output:

    
    Fetching rows from table: demo
    
    TableName PartitionKey RowKey TableTimestamp             Property1 Property2 Etag                                          
    --------- ------------ ------ --------------             --------- --------- ----                                          
    demo      test         venkat 12-03-2025 19:45:04 +05:30                     W/"datetime'2025-03-12T14%3A15%3A04.5351661Z'"
    
    
    Fetching rows from table: table1
    
    TableName PartitionKey RowKey     TableTimestamp             Property1 Property2 Etag                                          
    --------- ------------ ------     --------------             --------- --------- ----                                          
    table1    name         venkat890  12-03-2025 19:18:51 +05:30                     W/"datetime'2025-03-12T13%3A48%3A51.2879744Z'"
    table1    name         venkat     17-10-2024 12:03:37 +05:30 test      sample    W/"datetime'2024-10-17T06%3A33%3A37.2273905Z'"
    table1    name         venkatesan 19-08-2024 11:07:21 +05:30 test      key       W/"datetime'2024-08-19T05%3A37%3A21.1385156Z'"
    

    enter image description here

    The above script:

    • Fetches all tables in the storage account.
    • Iterates through each table.
    • Retrieves all rows from each table and sorts them by TableTimestamp in descending order.
    • Displays the results in a table format.

    REST API (Shared Key Authentication):

    You have used account key directly in authorization that could cause the error here.

    You can use the below script which could be useful to fetch the table entities using REST API.

    Script:

    $storageAccountName = "xxxx"
    $storageKey = "xxxxx"
    
    # Table name
    $tableName = "xxxx"
    
    # Construct the request URL
    $url = "https://$storageAccountName.table.core.windows.net/$tableName()"
    
    # Headers
    $version = "2021-02-12"
    $date = (Get-Date).ToUniversalTime().ToString("R")  # RFC1123 format
    
    # Canonicalized resources and headers
    $canonicalizedResources = "/$storageAccountName/$tableName()"
    $canonicalizedHeaders = "x-ms-date:$date"
    
    # String to sign
    $stringToSign = "$date`n$canonicalizedResources"
    
    # Compute the HMAC-SHA256 signature
    $keyBytes = [Convert]::FromBase64String($storageKey)
    $stringToSignBytes = [Text.Encoding]::UTF8.GetBytes($stringToSign)
    $hmacsha256 = New-Object System.Security.Cryptography.HMACSHA256
    $hmacsha256.Key = $keyBytes
    $signatureBytes = $hmacsha256.ComputeHash($stringToSignBytes)
    $signature = [Convert]::ToBase64String($signatureBytes)
    
    # Authorization header
    $authHeader = "SharedKeyLite $storageAccountName`:$signature"
    
    # Make the request
    $headers = @{
        "x-ms-date" = $date
        "x-ms-version" = $version
        "Authorization" = $authHeader
        "Accept" = "application/json;odata=nometadata"
    }
    
    $response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers
    $response | ConvertTo-Json -Depth 10  # Pretty-print the response
    

    Output:

    {
        "value":  [
                      {
                          "PartitionKey":  "name",
                          "RowKey":  "venkat",
                          "Timestamp":  "2024-10-17T06:33:37.2273905Z",
                          "Property1":  "test",
                          "Property2":  "sample"
                      },
                      {
                          "PartitionKey":  "name",
                          "RowKey":  "venkat890",
                          "Timestamp":  "2025-03-12T13:48:51.2879744Z"
                      },
                      {
                          "PartitionKey":  "name",
                          "RowKey":  "venkatesan",
                          "Timestamp":  "2024-08-19T05:37:21.1385156Z",
                          "Property1":  "test",
                          "Property2":  "key"
                      }
                  ]
    }
    

    enter image description here

    Reference:

    Hope the above answer helps! Please let us know do you have any further queries.

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.           

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.