Get-BalanceTerm.ps1

<#
.Synopsis
   The function returns balance on terminal for a person.
.DESCRIPTION
   The function returns balance on terminal for a person. This balance is from database table and isn't calculate.
.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 Simplified
    With parameter function returns a simplified object without nesting in the response.
.EXAMPLE
   Get-BalanceTerm -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -PersonCode "1045"
   Command retrives balance for a person identified by PersonCode with authentication via AccessKey
.EXAMPLE
   Get-BalanceTerm -URL https://intranet.company.com/webtime12/api -Token $MyToken -PersonCode "1045" -Simplified
   Command retrives balance for a person identified by PersonCode with authentication via Token, and then simplifies the acquired object.
#>

function Get-BalanceTerm {
    [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)]
        [switch]$Simplified
    )

    Process {

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

       $url = $url + "/WorkSheet/BalanceTerm"

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

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

       If ($($Simplified.IsPresent)) {
            $blt = @()
            Write-Verbose -Message "Parameter Simplified is present - simplification of the returned object."
            Foreach ($balt in $($balanceterm)) {
                $balant = New-Object PSObject
                Add-Member -InputObject $balant -MemberType NoteProperty -Name Firstname -Value $($balt.Person.Firstname)
                Add-Member -InputObject $balant -MemberType NoteProperty -Name Surname -Value $($balt.Person.Surname)
                Add-Member -InputObject $balant -MemberType NoteProperty -Name DegreeAfter -Value $($balt.Person.DegreeAfter)
                Add-Member -InputObject $balant -MemberType NoteProperty -Name PersonId -Value $($balt.Person.Id)
                Add-Member -InputObject $balant -MemberType NoteProperty -Name PersonCode -Value $($balt.Person.Code)
                Add-Member -InputObject $balant -MemberType NoteProperty -Name Balance -Value $($balt.Balance)
                Add-Member -InputObject $balant -MemberType NoteProperty -Name Error -Value $($balt.Error)

                $blt += $balant
                }
            Write-Verbose -Message "Return simplified object Balance on Terminal."
            $blt
            }
        else {
            Write-Verbose -Message "Return object Balance on Terminal."
            $($balanceterm)
        }
    }
}