Get-CostCenter.ps1

<#
.Synopsis
   The function returns list of all cost centers
.DESCRIPTION
   The function returns list of all cost centers.
.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 specified cost center and all child cost centers.
.EXAMPLE
   Get-OrgCenter -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -OrgCenterID "3"
   Command retrives specified organization center with authentication via AccessKey
.EXAMPLE
   Get-OrgCenter -URL https://intranet.company.com/webtime12/api -Token $MyToken -OrgCenterID "3" -IncludeChildern
   Command retrives specified organization center and child organization centers with authentication via Token.
#>

function Get-CostCenter {
    [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
    )

    Process {

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

       $url = $url + "/CostCenters"

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

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

       Write-Verbose -Message "Return object Balance."
       Write-Output $CostOrg

    }
}