Get-PresenceOrg.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 OrgCenterID
    Database ID code of organization center.
.PARAMETER IncludeChildern
    If parameter is used function returns presence for persons in specified organization center and all child organization centers.
.PARAMETER Simplified
    With parameter function returns a simplified object without nesting in the response.
.EXAMPLE
   Get-PresenceOrg -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -OrgCenterID "3"
   Command retrives presence for a persons from specified organization center with authentication via AccessKey
.EXAMPLE
   Get-PresenceOrg -URL https://intranet.company.com/webtime12/api -Token $MyToken -OrgCenterID "3" -Simplified
   Command retrives presence for a persons from specified organization center with authentication via Token, and then simplifies the acquired object.
#>

function Get-PresenceOrg {
    [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]$OrgCenterId,
        [Parameter(Mandatory = $false)]
        [switch]$IncludeChildren,
        [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 + "/PresenceOrg"

       $Body = @{
            orgcenterid = $OrgCenterId;
            includechildren = $IncludeChildren.IsPresent
       }

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

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

                $pr.Add($prit) | Out-Null
                }
            Write-Verbose -Message "Return simplified object Presence Org."
            Write-Output $spresOrg
            }
        else {
            Write-Verbose -Message "Return object Presence Org."
            Write-Output $presOrg
        }
    }
}