Public/Add-SpecAzTableRowUsingSAS.ps1
function Add-SpecAzTableRowUsingSAS { <# .SYNOPSIS Adds a new row to an Azure Table Storage using a Shared Access Signature (SAS) token. .DESCRIPTION This function allows you to add a new row to an Azure Table Storage using a SAS token for authentication. You can specify the table name, storage account, SAS token, partition key, row key, a list of properties to add, and the method (default is 'Put'). .PARAMETER TableName The name of the Azure Table to add the row to. .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 new row. Default is '1'. .PARAMETER Key The row key for the new row. .PARAMETER PropertyList An array of properties to add. Each property should be in the format of a key-value pair. .PARAMETER Method (Optional) The method to use for adding the row. Default is 'Put'. .EXAMPLE Add a new row to the table: $PropertyListFilter = @{ Device_Name = 'Enterprise' Last_Logged_On = 'owen.heaume' DeviceType = 'HR' IP_Address_Wifi = '172.18.0.2' StoreName = 'MyStore' StoreNumber = '12345' } $params=@{ tableName = $table StorageAccount = $storageAcc Key = 'mykey' #The unique name that appears in in the 'RowKey' table header PropertyList = $PropertyListFilter SASToken = $sasRW } write-host "Updating value in table row" -ForegroundColor DarkCyan try { Add-SpecAzTableRowUsingSAS @params -ea stop write-host "Succesfully added new row" -ForegroundColor DarkGreen } catch { write-host "An error occurred: $_ " -ForegroundColor DarkYellow } Creates a new row in the table. If the row already exists, only the properties in the property filter will be updated with new values. All other existing property values will become 'null' If you do not want the other properties to be wiped out (i.e set to null), add the parameter "-method merge" or use Update-specAzTableRowUsingSAS .NOTES Author : owen.heaume Version : 1.0 Initial release 2.0 Update to use Write-Error for elegant handling within try \ catch blocks #> [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, [Parameter(Mandatory = $false)] [string]$Method = 'Put' ) Write-Verbose "Adding row to $tableName" $version = '2017-04-17' $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 { $item = Invoke-RestMethod -Method $method -Uri $table_url -Headers $headers -ContentType application/json -Body $body -UseBasicParsing -ea Stop Write-Verbose 'Successfully added' #return 0 # success } 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: $_" } } |