Get-PresenceCost.ps1

<#
.Synopsis
   The function returns information about presence of persons within a cost center.
.DESCRIPTION
   The function returns information about presence of persons within a cost center.
.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 CostCenterID
    Database ID code of cost center.
.PARAMETER IncludeChildern
    If parameter is used function returns presence for persons in specified cost center and all child cost 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-PresenceCost {
    [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]$CostCenterId,
        [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 + "/PresenceCost"

       $Body = @{
            costcenterid = $CostCenterId;
            includechildren = $IncludeChildren.IsPresent
       }

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

       If ($($Simplified.IsPresent)) {
            $spresCost = New-Object -TypeName System.Collections.ArrayList
            Write-Verbose -Message "Parameter Simplified is present - simplification of the returned object."
            Foreach ($pres in $($presCost)) {
                $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 Ordinal -Value $($pres.Person.Ordinal)
                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 Cost."
            Write-Output $spresCost
            }
        else {
            Write-Verbose -Message "Return object Presence Cost."
            Write-Output $presCost
        }
    }
}