Public/Add-SpecAzTableRow.ps1

function Add-SpecAzTableRow {
    <#
    .SYNOPSIS
    Adds or updates a row to an Azure Storage Table.
 
    .DESCRIPTION
    This function is used to add or update a row to an Azure Storage Table. It requires the Azure PowerShell module 'Az.Storage' to be installed. The function accepts various parameters such as table name, resource group, storage account, partition key, row key, property hashtable, and an optional switch to update existing data. It connects to the specified storage account, locates the table, and adds or updates the row with the provided properties.
 
    .PARAMETER tableName
    The name of the Azure Storage Table where the row will be added. This parameter is mandatory.
 
    .PARAMETER tableResourceGroup
    The resource group of the Azure Storage Account. If not specified, the function will use the current resource group context.
 
    .PARAMETER tableStorageAccount
    The name of the Azure Storage Account where the table is located. If not specified, the function will use the current storage account context.
 
    .PARAMETER PartitionKey
    The partition key for the new row. Default value is 1.
 
    .PARAMETER RowKeyValue
    The value for the row key of the new row. This parameter is mandatory.
 
    .PARAMETER Property
    A hashtable representing the properties and their values for the new row. This parameter is mandatory.
 
    .PARAMETER UpdateExistingData
    A switch parameter indicating whether to update existing data if a row with the same partition key and row key already exists in the table. If the row key does not exist, it creates a new row entry in the table.
    Important note: If you send empty or null values, then the table row will be updated to reflect they are null or empty! To avoid this, use Update-SpecAzTableRow and view the example given.
 
    .EXAMPLE
    Add-SpecAzTableRow -tableName "MyTable" -tableResourceGroup "MyResourceGroup" -tableStorageAccount "MyStorageAccount" -PartitionKey "1" -RowKeyValue "ABC" -Property @{ "Name" = "John"; "Age" = 30 }
 
    Adds a new row to the Azure Storage Table "MyTable" in the resource group "MyResourceGroup" and storage account "MyStorageAccount". The partition key is set to "1" and the row key is set to "ABC". The row will have properties "Name" and "Age" with values "John" and 30, respectively.
 
    .OUTPUTS
    It will return various error numbers depending on the error:
        501 - Storage account not found
        502 - Table Not found
        503 - Unable to add properties to the table
 
    .NOTES
    Author: owen.heaume
    Version: - 1.0 Initial release
 
    #>


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

        [Parameter(Mandatory = $false)]
        [string]$tableResourceGroup,

        [Parameter(Mandatory = $false)]
        [string]$tableStorageAccount,

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

        [Parameter(Mandatory = $True)]
        [string]$RowKeyValue,

        [Parameter(Mandatory = $true)]
        [hashtable]$Property,

        [Switch]$UpdateExistingData
    )
    Begin {
        # clean up leading and trailing whitespace in vars
        $TableName = $TableName.Trim()
        $tableResourceGroup = $tableResourceGroup.Trim()
        $tableStorageAccount = $tableStorageAccount.Trim()
        $PartitionKey = $PartitionKey.Trim()
        $RowKeyValue = $RowKeyValue.Trim()

        # Get the table to work with
        $cloudTable = get-specCloudtable -TableName $TableName -tableResourceGroup $tableResourceGroup -tableStorageAccount $tableStorageAccount

        # Check for errors
        switch ($cloudTable) {
            501 { return 501 }
            502 { return 502 }
        }
    }

    Process {
        Try {
            if ($UpdateExistingData) {
                Write-Verbose "Updating existing row data within table $tableName"
                $tableresult = Add-AzTableRow -table $cloudTable -PartitionKey $PartitionKey -RowKey $RowKeyValue -property $Property -ErrorAction Stop -UpdateExisting
            } else {
                Write-Verbose "Adding new row entry to table: $tableName"
                $tableresult = Add-AzTableRow -table $cloudTable -PartitionKey $PartitionKey -RowKey $RowKeyValue -property $Property -ErrorAction Stop
            }
        } Catch {
            Write-error "Error adding properties to table $tableName"
            return 503
        }

        Return $tableresult
    }
}