Remove-Absence.ps1

<#
.Synopsis
   Remove a absence from attendance database
.DESCRIPTION
   The function delete a absence of the specified date from attendance database.
.PARAMETER URL
    Server API url.
.PARAMETER Token
    Authentication token for accessing API data. If you use the token for authentication, don't enter access key.
.PARAMETER AccessKey
    The AccessKey to get an authentication token for accessing API data. If you use the access key to get authentication token, don't enter token.
.PARAMETER Date
    Date of the absence. If not specified, the current date will be used.
.PARAMETER PersonCode
    Person's personal identification code.
.PARAMETER PersonID
    Person's database identification id.
.EXAMPLE
   Remove-Pass -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -PersonCode 1045
   This command delete the today absence of the specified PersonCode from attendance database.

   Remove-Pass -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -PersonCode 1045 -Date 2020-10-25
   This command delete the absence of the specified Date and PersonCode from attendance database.
#>

function Remove-Absence {
    [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName='AccessKey')]
    param(
        [Parameter(Mandatory = $true)]
        [string]$URL,
        [Parameter(Mandatory = $true,ParameterSetName='Token')]
        [string]$Token,
        [Parameter(Mandatory = $true,ParameterSetName='AccessKey')]
        [string]$AccessKey,
        [Parameter(Mandatory = $false)]
        [string]$Date = $(Get-Date -Format yyyy-MM-ddTHH:mm:ss.msZ),
        [Parameter(Mandatory = $false)]
        [string]$PersonCode,
        [Parameter(Mandatory = $false)]
        [string]$PersonID
)

   Process {

      if ($PSCmdlet.ParameterSetName -eq 'AccessKey') {
         if ($pscmdlet.ShouldProcess("$URL", "Get Token")){
            $SchemeToken = Get-Token -URL $URL -AccessKey $AccessKey
            $Token = $SchemeToken.Scheme + " " + $SchemeToken.Token
         }
      }

      $URL = $URL + "/Absence"

      $Body = @{
         date = $Date;
         personcode = $PersonCode;
         personid = $PersonID;
      }

      $json = $body | ConvertTo-Json

      Write-Verbose -Message "Send request to API endpoint $URL with access key $Token."

      if ($pscmdlet.ShouldProcess("$URL", "Delete Absence")){
         $resp = Invoke-RestMethod -Method Delete -Uri $url -Headers @{ Authorization = $Token } -Body $json -ContentType 'application/json'
         $resp

      Write-Verbose -Message "Returned response object."
      Write-Verbose -Message $resp
      }
   }
}