Public/Update-pseAzTableRowUsingSAS.ps1

function Update-pseAzTableRowUsingSAS {
    <#
    .SYNOPSIS
    Updates a row in an Azure Table Storage using a Shared Access Signature (SAS) token.
 
    .DESCRIPTION
    This function allows you to update a row in an Azure Table Storage using a SAS token for authentication. It can also create a new row if it does not already exist.').
 
    .PARAMETER TableName
    The name of the Azure Table to update the row in.
 
    .PARAMETER StorageAccount
    The name of the Azure Storage Account containing the table.
 
    .PARAMETER SASToken
    The Shared Access Signature (SAS) token for authenticating with the Azure Table Storage.
 
    .PARAMETER PartitionKey
    (Optional) The partition key for the row to update. Default is '1'.
 
    .PARAMETER Key
    The row key for the row to update.
 
    .PARAMETER PropertyList
    An array of properties to update. Each property should be in the format of a key-value pair.
 
    .EXAMPLE
    $PropertyListFilter = @{
        DeviceType = "Dispense3"
        Last_Logged_On = "owen.heaume"
    }
 
    Update-pseAzTableRowUsingSAS -TableName $tablename -storageAccount $storageAccount -sastoken $sasWriteToken -key 'HV4DZY2' -PropertyList $PropertyListFilter
 
    Updates only the properties and values specified in the $propertyListFilterArray for the table row containing the key of 'HV4DZY2'. If the table contains other table headers with existing values it leaves them untouched.
 
    .EXAMPLE
    This function can also ADD a new row if it does not exist:
 
    $PropertyListFilter = @{
        Device_Name = $env:computername
        Last_Logged_On = 'owen.heaume'
        DeviceType = 'Class'
        IP_Address_Wifi = '172.18.0.1'
    }
 
    # Function params
    $tableRowParams = @{
        tableName = $tableName
        StorageAccount = $StorageAccount
        SasToken = $sasWriteToken
        key = 'R2D2' # This is the unique value identifying the row to modify,under the RowKey header
        PropertyList = $PropertyListFilter # An array containing ONLY properties\values to modify
    }
 
    write-host "Adding new row to table [$tablename]" -ForegroundColor DarkCyan
    Update-pseAzTableRowUsingSAS @tableRowParams
 
    .NOTES
    Author : owen.heaume
    Version : 1.0.0 - Initial release
    #>


    [cmdletbinding()]

    param
    (
        [Parameter(Mandatory = $true)]
        [string]$TableName,

        [Parameter(Mandatory = $true)]
        [string]$storageAccount,

        [Parameter(Mandatory = $true)]
        [string]$SasToken,

        [parameter(Mandatory = $false)]
        [string]$PartitionKey = '1',

        [Parameter(Mandatory = $true)]
        [string]$Key,

        [Parameter(Mandatory = $true)]
        [array]$PropertyList
    )

    Write-Verbose "Updating $tableName"

    $Method = 'Merge'
    $resource = "$tableName(PartitionKey='$partitionkey',RowKey='$key')"
    $table_url = "https://$storageAccount.table.core.windows.net/$resource$SasToken"
    $GMTTime = (Get-Date).ToUniversalTime().toString('R')
    $headers = @{
        'x-ms-date' = $GMTTime;
        Accept      = 'application/json;odata=nometadata'
    }

    $body = $PropertyList | ConvertTo-Json

    Try {
        Invoke-RestMethod -Method $method -Uri $table_url -Headers $headers -ContentType application/json -Body $body -UseBasicParsing | Out-Null
        Write-Verbose "OK"
    } catch [System.Net.WebException] {
        if ($_.Exception.Message -contains "The remote server returned an error: (404) Not Found.") {
            Write-Error "Table [$TableName] not found. Please check that the table exists and that you have not made a typo. $_"
        } elseif ($_.Exception.Message -match "The remote name could not be resolved") {
            Write-Error "Storage account [$StorageAccount] not found. Please check that the storage account exists and that you have not made a typo. $_"
        } else {
            # Handle other WebExceptions
            Write-Error "A WebException occurred: $_.Exception.Message)"
        }
    } catch {
        Write-Error "An error occurred: $_"
    }
}