Get-Balance.ps1
<# .Synopsis The function returns calculated balance for a person. .DESCRIPTION The function calculates and returns balance 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-Balance -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-Balance -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-Balance { [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 + "/WorkSheet/Balance" $Body = @{ date = $date; personcode = $PersonCode; personid = $PersonId } $json = $body | ConvertTo-Json Write-Verbose -Message "Send request to API endpoint $URL with access key $Token." $balance = Invoke-RestMethod -Method Post -Uri $url -Headers @{ Authorization = $Token } -Body $json -ContentType 'application/json' If ($($Simplified.IsPresent)) { $bl = @() Write-Verbose -Message "Parameter Simplified is present - simplification of the returned object." Foreach ($bal in $($balance)) { $balan = New-Object PSObject Add-Member -InputObject $balan -MemberType NoteProperty -Name Firstname -Value $($bal.Person.Firstname) Add-Member -InputObject $balan -MemberType NoteProperty -Name Surname -Value $($bal.Person.Surname) Add-Member -InputObject $balan -MemberType NoteProperty -Name DegreeAfter -Value $($bal.Person.DegreeAfter) Add-Member -InputObject $balan -MemberType NoteProperty -Name PersonId -Value $($bal.Person.Id) Add-Member -InputObject $balan -MemberType NoteProperty -Name PersonCode -Value $($bal.Person.Code) Add-Member -InputObject $balan -MemberType NoteProperty -Name BalanceAccount -Value $($bal.BalanceAccount) Add-Member -InputObject $balan -MemberType NoteProperty -Name Balance -Value $($bal.Balance) Add-Member -InputObject $balan -MemberType NoteProperty -Name IsConfirmed -Value $($bal.IsConfirmed) $bl += $balan } Write-Verbose -Message "Return simplified object Balance." $bl } else { Write-Verbose -Message "Return object Balance." $($balance) } } } |