Get-Presence.ps1

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

function Get-Presence {
    [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 + "/Presence"

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

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

       If ($($Simplified.IsPresent)) {
            $spresence = New-Object -TypeName System.Collections.ArrayList
            Write-Verbose -Message "Parameter Simplified is present - simplification of the returned object."
            Foreach ($pres in $($presence)) {
                $pre = New-Object PSObject
                Add-Member -InputObject $pre -MemberType NoteProperty -Name Firstname -Value $($pres.Person.Firstname)
                Add-Member -InputObject $pre -MemberType NoteProperty -Name Surname -Value $($pres.Person.Surname)
                Add-Member -InputObject $pre -MemberType NoteProperty -Name DegreeBefore -Value $($pres.Person.DegreeBefore)
                Add-Member -InputObject $pre -MemberType NoteProperty -Name DegreeAfter -Value $($pres.Person.DegreeAfter)
                Add-Member -InputObject $pre -MemberType NoteProperty -Name PersonId -Value $($pres.Person.PersonId)
                Add-Member -InputObject $pre -MemberType NoteProperty -Name PersonCode -Value $($pres.Person.PersonCode)
                Add-Member -InputObject $pre -MemberType NoteProperty -Name Ordinal -Value $($pres.Person.Ordinal)
                Add-Member -InputObject $pre -MemberType NoteProperty -Name Present -Value $($pres.Present)
                Add-Member -InputObject $pre -MemberType NoteProperty -Name PassTime -Value $($pres.PassTime)
                Add-Member -InputObject $pre -MemberType NoteProperty -Name PassText -Value $($pres.PassText)
                Add-Member -InputObject $pre -MemberType NoteProperty -Name PresenceText -Value $($pres.PresenceText)

                $spresence.Add($pre) | Out-Null
                }
            Write-Verbose -Message "Return simplified object Presence."
            Write-Output $spresence
            }
        else {
           Write-Verbose -Message "Return object Presence."
           Write-Output $presence
        }
    }
}