Get-Vacation.ps1

<#
.Synopsis
   The function returns vaction for a person.
.DESCRIPTION
   The function returns vacation for a person and specified date.
.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 Date
    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-Vacation -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -PersonCode "1045"
   Command retrives presence for a person identified by PersonCode with authentication via AccessKey
.EXAMPLE
   Get-Vacation -URL https://intranet.company.com/webtime12/api -Token $MyToken -PersonCode "1045" -Simplified
   Command retrives presence for a person identified by PersonCode with authentication via Token, and then simplifies the acquired object.
#>

function Get-Vacation {
    [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]$Date = $(Get-Date -Format yyyy-MM-ddTHH:mm:ss.msZ),
        [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 + "/Vacation"

       $Body = @{
            date = $date;
            personcode = $PersonCode;
            personid = $PersonId
       }

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

       If ($($Simplified.IsPresent)) {
            $svacation = New-Object -TypeName System.Collections.ArrayList
            Write-Verbose -Message "Parameter Simplified is present - simplification of the returned object."
            Foreach ($vac in $($vacation)) {
                $vacat = New-Object PSObject
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name Firstname -Value $($vac.Person.Firstname)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name Surname -Value $($vac.Person.Surname)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name DegreeBefore -Value $($vac.Person.DegreeBefore)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name DegreeAfter -Value $($vac.Person.DegreeAfter)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name PersonId -Value $($vac.Person.Id)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name PersonCode -Value $($vac.Person.Code)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name Ordinal -Value $($vac.Person.Ordinal)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name Used -Value $($vac.Used)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name UsedMinutes -Value $($vac.Used_Minutes)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name Planned -Value $($vac.Planned)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name PlannedMinutes -Value $($vac.Planned_Minutes)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name Balance -Value $($vac.Balance)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name BalanceMinutes -Value $($vac.Balance_Minutes)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name Claim -Value $($vac.Claim)
                Add-Member -InputObject $vacat -MemberType NoteProperty -Name ClaimMinutes -Value $($vac.Claim_Minutes)

                $vc.Add($vacat) | Out-Null
                }
                Write-Verbose -Message "Return simplified object Vacation."
            Write-Output $svacation
            }
        else {
            Write-Verbose -Message "Return object Vacation."
            Write-Output $vacation
        }
    }
}