Public/Remove-SpecAzTableRowUsingSAS.ps1

function Remove-specAzTableRowUsingSAS {
    <#
    .SYNOPSIS
        Deletes a specific row from an Azure Table Storage using a SAS token, PartitionKey, and RowKey.
 
    .DESCRIPTION
        This function sends a DELETE request to Azure Table Storage to remove a row identified by its PartitionKey and RowKey.
        The request uses a SAS token for authentication and must include the PartitionKey and RowKey in the URL.
        The function returns a success message if the row is deleted or an error message if the deletion fails.
 
    .PARAMETER SasToken
        The SAS token granting access to the Azure Table Storage resource. This token must have 'Delete' permission.
 
    .PARAMETER StorageAccount
        The name of the Azure Storage account that contains the table.
 
    .PARAMETER TableName
        The name of the Azure Table from which the row will be deleted.
 
    .PARAMETER PartitionKey
        The PartitionKey of the entity to delete.
 
    .PARAMETER RowKey
        The RowKey of the entity to delete.
 
    .EXAMPLE
        Remove-SpecAzTableRowUsingSAS -SasToken "<your SAS token>" -StorageAccount "<your storage account>" -TableName "<your table name>" -PartitionKey "<your partition key>" -RowKey "<your row key>"
 
        This example deletes a row from the specified Azure Table Storage by providing the SAS token, table name,
        PartitionKey, and RowKey.
 
    .EXAMPLE
        $params = @{
            SASToken = '<your SAS token>'
            StorageAccount = '<your storage account>'
            TableName = "<your table name>"
            PartitionKey = '1'
            RowKey = '<RowKey of the entity to delete>'
        }
 
        try {
            Remove-SpecAzTableRowUsingSAS @params -ea stop
        } catch {
            write-host $_ -ForegroundColor red
        }
 
        This example deletes a row from the specified Azure Table Storage demonstrating 'real-world' usage.
 
    .NOTES
        Author : owen.heaume
        Version : 1.0 - Initial release
    #>



    [cmdletbinding()]
    param(
        [parameter (mandatory = $true)]
        [string]$SasToken,

        [parameter (mandatory = $true)]
        [string]$StorageAccount,

        [parameter (mandatory = $true)]
        [string]$TableName,

        [parameter (mandatory = $true)]
        [string]$PartitionKey,

        [parameter (mandatory = $true)]
        [string]$RowKey
    )

    begin {
        $headers = @{
            Accept     = 'application/json;odata=nometadata'
            'If-Match' = '*'  # Ensures that the entity is deleted if it exists
        }
    }

    process {
        # Construct the URI for the DELETE request, using PartitionKey and RowKey in the path
        $tableUri = "https://$StorageAccount.table.core.windows.net/$TableName(PartitionKey='$PartitionKey',RowKey='$RowKey')$SasToken"

        try {
            Write-Host "Attempting to delete the row with PartitionKey: $PartitionKey and RowKey: $RowKey" -ForegroundColor DarkGray

            # Send the DELETE request to remove the specified row
            $response = Invoke-WebRequest -Method Delete -Uri $tableUri -Headers $headers -ContentType 'application/json' -ErrorAction Stop -UseBasicParsing

            if ($response.StatusCode -eq 204) {
                Write-Host "Row with PartitionKey: $PartitionKey and RowKey: $RowKey has been successfully deleted." -ForegroundColor Green
            } else {
                Write-Error "Failed to delete the row. Status Code: $($response.StatusCode)"
            }
        } catch {
            Write-Error "An error occurred while deleting the row: $_"
        }
    }
}