Get-Absence.ps1

<#
.Synopsis
   Returns list of person's absences.
.DESCRIPTION
   The function returns list of person's absences.
.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 PersonCode
    Person's personal identification code.
.PARAMETER PersonID
    Person's database identification id.
.PARAMETER DateFrom
    Date from which data is returned. If not specified, the first day of the month will be used.
.PARAMETER DateTo
    Date to which data is returned. If not specified, the current date will be used.
.PARAMETER Simplified
    With parameter function returns a simplified object without nesting in the response.
.EXAMPLE
   Get-Absence -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -PersonCode "1045"
   Command retrives absences for a person identified by PersonCode with authentication via AccessKey
.EXAMPLE
   Get-Absence -URL https://intranet.company.com/webtime12/api -Token $MyToken -PersonCode "1045" -Simplified
   Command retrives absences for a person identified by PersonCode with authentication via Token, and then simplifies the acquired object.
#>

function Get-Absence {
    [CmdletBinding(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]$DateFrom = $(Get-Date -Format yyyy-MM-01T00:00:00.00Z),
        [Parameter(Mandatory = $false)]
        [string]$DateTo = $(Get-Date -Format yyyy-MM-ddT00:00:00.00Z),
        [Parameter(Mandatory = $false)]
        [switch]$Simplified
    )

    Process {

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

       $url = $url + "/Absences"

       $Body = @{
            datefrom = $DateFrom;
            dateto = $DateTo;
            personcode = $PersonCode;
            personid = $PersonId
       }

       $json = $body | ConvertTo-Json
       Write-Verbose -Message "Send request to API endpoint $URL with access key $Token."
       $Absence = Invoke-RestMethod -Method Post -Uri $url -Headers @{ Authorization = $Token } -Body $json -ContentType 'application/json'

       If ($($Simplified.IsPresent)) {
            $sabsence = New-Object -TypeName System.Collections.ArrayList
            Write-Verbose -Message "Parameter Simplified is present - simplification of the returned object."
            Foreach ($abs in $($Absence.Absences)) {
                $simabs = New-Object PSObject
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name Firstname -Value $($Absence.Person.Firstname)
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name Surname -Value $($Absence.Person.Surname)
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name DegreeBefore -Value $($Absence.Person.DegreeBefore)
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name DegreeAfter -Value $($Absence.Person.DegreeAfter)
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name PersonId -Value $($Absence.Person.Id)
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name PersonCode -Value $($Absence.Person.Code)
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name PersonOrdinal -Value $($Absence.Person.Ordinal)
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name Date -Value $($abs.Date)
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name AbsId -Value $($abs.Id)
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name Shotcut -Value $($abs.Shortcut)
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name TimeFrom -Value $($abs.TimeFrom)
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name TimeTo -Value $($abs.TimeTo)
                Add-Member -InputObject $simabs -MemberType NoteProperty -Name Comment -Value $($abs.Comment)

                $sabsence.Add($simabs) | Out-Null
                }
            Write-Verbose -Message "Return simplified object Absences."
            Write-Output $sabsence
            }
        else {
            Write-Verbose -Message "Return object Absences."
            Write-Output $Absence
        }

    }
}