Update-AZTableEntity--v1-0.psm1
$FunctionScriptName = "Update-AZTableEntity" Write-Verbose "Import-Start| [$($FunctionScriptName)]" #* Dependencies # Import-Function -Scope "Support" -Function "Get-SAStokenValidity" -Version "1.X" function Update-AZTableEntity { <# .SYNOPSIS Updates AZ Table entitys or creates .DESCRIPTION Updates AZ Table entitys or creates Default: Create if not exist. Merge if exist. set "RemoveExistingData" switch to override existing data Partitionkey & RowKey are static values and can´t be changed. .PARAMETER Config Config param Expects following values: StorageAccount / TableName / Sastoken All 3 can be forced by direct param .PARAMETER StorageAccount Storageaccount to use Overrides value from config param .PARAMETER TableName TableName to use Overrides value from config param .PARAMETER sastoken sastoken to use Overrides value from config param .PARAMETER Entity Entity to update .PARAMETER pipe Same as "Entity" Multiple are possible .PARAMETER PartitionKey PartitionKey to use DOES NOT Overrides value from Entity param .PARAMETER RowKey RowKey to use DOES NOT Overrides value from Entity param .PARAMETER RemoveExistingData [switch] true => Deletes values for entity that aren´t listed in entity param .PARAMETER skipSAScheck ignore SAStoken check recommended if a lot of tables with the same token shall be used .EXAMPLE Update-AZTableEntity -Config $Config -Entity $Entity $Entity | Update-AZTableEntity -Config $Config -sastoken $sastoken .NOTES AUTHOR: Ken Dobrunz // Ken.Dobrunz@Direkt-Gruppe.de | Direkt Gruppe WEBSITE: http://kensmagic.site LASTEDIT: 29.05.2020 - Version: 1.0 #> [cmdletbinding()] Param( #* Active data # Tableconfig - Config or other 3 needed [Parameter()]$Config, [Parameter()][Alias('Storage')]$StorageAccount, [Parameter()][Alias('Table')]$TableName, [Parameter()][Alias('sas')]$sastoken, # Entity [parameter()]$Entity, [parameter(ValueFromPipeline = $True)]$pipe, [Parameter()][Alias('PKey')]$PartitionKey, # overrides if in entity [Parameter()][Alias('RKey')]$RowKey, # overrides if in entity [Parameter()][Alias('Put')][switch]$RemoveExistingData, [Parameter()][switch]$skipSAScheck ) Begin { $SelfIdentifier = "UpdateTable" $TableName = if ($TableName) { $TableName }elseif ($Config.Table) { $Config.Table }else { Write-Error "[$($SelfIdentifier)] No TableName provided" } $StorageAccount = if ($StorageAccount) { $StorageAccount }elseif ($Config.StorageAccount) { $Config.StorageAccount }else { Write-Error "[$($SelfIdentifier)] No StorageAccount provided" } $sastoken = if ($sastoken) { $sastoken }elseif ($Config.sastoken) { $Config.sastoken }else { Write-Error "[$($SelfIdentifier)] No sastoken provided" } if (!$skipSAScheck) { if (!(Get-SAStokenValidity -ReadOnlyObject -Table -sastoken $sastoken)) { throw "[$($SelfIdentifier)] Sastoken not valid" } } else { Write-Verbose "[$($SelfIdentifier)] Skipped sas check due to skip flag" } $Method = if ($RemoveExistingData) { "PUT" }else { "MERGE" } $TableAPIHeader = @{ "x-ms-version" = "2019-07-07" Accept = "application/json;odata=minimalmetadata" } $functionverbosecount = 0 } Process { if ($Entity) { $pipe = $Entity} $RowKey = if ($pipe.RowKey) { $pipe.RowKey }elseif ($RowKey) { $RowKey }else { Write-Error "[$($SelfIdentifier)] No RowKey provided" } $PartitionKey = if ($pipe.PartitionKey) { $pipe.PartitionKey }elseif ($PartitionKey) { $PartitionKey }else { Write-Error "[$($SelfIdentifier)] No PartitionKey provided" } if (!$PartitionKey -or !$RowKey) { Write-Error "[$($SelfIdentifier)] No P/Rkey!" } $table_uri = "https://$($StorageAccount).table.core.windows.net/$($TableName)" $filter = "(PartitionKey='$($PartitionKey)',RowKey='$($Rowkey)')" Write-Debug "[$($SelfIdentifier)] [$($Method)] @ [$($table_uri)] - [$($filter)] " $body = ($pipe | ConvertTo-Json) Invoke-RestMethod -Method $Method -uri ($table_uri + $filter + $sastoken) -Headers $TableAPIHeader -Body $body -ContentType application/json $functionverbosecount++ } End { if ($functionverbosecount -gt 1) { Write-Verbose "[$($SelfIdentifier)] Updated $($functionverbosecount) entries" } } } #v1.0b Export-ModuleMember -Function * Write-Verbose "Import-END| [$($FunctionScriptName)]" |