New-Absence.ps1

<#
.Synopsis
   Insert a absence into attendance database
.DESCRIPTION
   The function insert a new absence of the specified type for the specified person into 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 AccountID
    Account ID of the new absence.
.PARAMETER PersonCode
    Person's personal identification code.
.PARAMETER PersonID
    Person's database identification id.
.PARAMETER Date
    Date and time of the new absence. If not specified, the current date will be used.
.PARAMETER TimeFrom
    Eventual start time of part-time absence. Let it null if absence is supposed to be all-day long.
.PARAMETER TimeTo
    Eventual end time of part-time absence. Let it null if absence is supposed to be all-day long.
.PARAMETER Comment
    Comment of the new absence.
.EXAMPLE
   New-Absence -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -PersonCode 1045 -AccountID 4 -Comment "API Absence"
   This command insert new absence of the specified type for the specified person code into attendance database with comment.

#>

function New-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]$PersonCode,
        [Parameter(Mandatory = $false)]
        [string]$PersonID,
        [Parameter(Mandatory = $false)]
        [string]$Date = $(Get-Date -Format yyyy-MM-ddTHH:mm:ss.msZ),
        [Parameter(Mandatory = $false)]
        [string]$TimeFrom,
        [Parameter(Mandatory = $false)]
        [string]$TimeTo,
        [Parameter(Mandatory = $true)]
        [string]$AccountID,
        [Parameter(Mandatory = $false)]
        [string]$Comment
)

   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;
         accountid = $AccountID;
         timefrom = $TimeFrom;
         timeto = $TimeTo;
         comment = $Comment
         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", "New Absence")){
         $resp = Invoke-RestMethod -Method Put -Uri $url -Headers @{ Authorization = $Token } -Body $json -ContentType 'application/json'
         $resp

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