Remove-AZTableEntity--v3-0.psm1

$FunctionScriptName = "Remove-AZTableEntity"
Write-Verbose "Import-Start| [$($FunctionScriptName)]"

#* Dependencies
# Import-Function -Scope "Support" -Function "Get-SAStokenValidity" -Version "1.X"

function Remove-AZTableEntity {
        <#
        .SYNOPSIS
            Removes AZ Table entitys
         
        .DESCRIPTION
            Removes AZ Table entitys
 
            Only deletes 1 matching entry! - No bulk allowed
             
        .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 Delete
            Only uses RowKey & PartitionKey
       
        .PARAMETER PartitionKey
            PartitionKey to use
            Overrides value from Entity param
         
        .PARAMETER RowKey
            RowKey to use
            Overrides value from Entity param
         
        .PARAMETER skipSAScheck
            ignore SAStoken check
            recommended if a lot of tables with the same token shall be used
         
        .EXAMPLE
            Remove-AZTableEntity -Config $Config -Entity $Entity
            $Entity | Remove-AZTableEntity -Config $Config -sastoken $sastoken
         
        .NOTES
            AUTHOR: Ken Dobrunz // Ken.Dobrunz@Direkt-Gruppe.de | Direkt Gruppe
            WEBSITE: http://kensmagic.site
 
            LASTEDIT: 31.05.2020 - Version: 3.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(ValueFromPipeline)]$Entity,
        [Parameter()][Alias('PKey')]$PartitionKey, # overrides if in entity
        [Parameter()][Alias('RKey')]$RowKey, # overrides if in entity

        [Parameter()][switch]$skipSAScheck
    )
    Begin {
        $SelfIdentifier = "RemoveTable"

        $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 = "DELETE"

        $TableAPIHeader = @{
            "x-ms-version" = "2019-07-07"
            Accept         = "application/json;odata=minimalmetadata"
            "If-Match"     = "*"
        }

        $functionverbosecount = 0
    }
    Process { 
        if (!$Entity -and !$PartitionKey -and !$RowKey) { throw "[$($SelfIdentifier)] No 'Entity' value" }
        elseif (!$Entity -and $PartitionKey -and $RowKey) { $Entity = @{PartitionKey = $PartitionKey;RowKey = $RowKey}}
        
        $Entity | ForEach-Object {
            $RowKey = if ($RowKey) { $RowKey } elseif ($pipe.RowKey) { $pipe.RowKey } else { Write-Error "[$($SelfIdentifier)] No RowKey provided" }
            $PartitionKey = if ($PartitionKey) { $PartitionKey } elseif ($pipe.PartitionKey) { $pipe.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-Verbose "[$($SelfIdentifier)] [$($Method)] @ [$($table_uri)] - [$($filter)] "

            Invoke-RestMethod -Method $Method -uri ($table_uri + $filter + $sastoken) -Headers $TableAPIHeader -ContentType application/json
            $functionverbosecount++
        }
    }
    End {
        if ($functionverbosecount -gt 1) { Write-Verbose "[$($SelfIdentifier)] processed $($functionverbosecount) entries" }
    }
} #v3.0

Export-ModuleMember -Function *
Write-Verbose "Import-END| [$($FunctionScriptName)]"