Get-Pass.ps1
<# .Synopsis The function returns list of person's passes. .DESCRIPTION The function returns passes per person and specified period. .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 ValidOnly Function returns date for valid persons only .PARAMETER DateFrom Date from which data is returned. If not specified, the first day of the month will be used. .PARAMETER DateTo 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-Pass -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -PersonCode "1045" -ValidOnly -DateFrom "2019-01-01" Command retrives passes for a valid person identified by PersonCode for period from Date to present with authentication via AccessKey .EXAMPLE Get-Pass -URL https://intranet.company.com/webtime12/api -Token $MyToken -PersonCode "1045" -DateFrom "2019-01-01" -DateTo "2019-31-01" -Simplified Command passes for a person identified by PersonCode for period from DateFrom to DateTo with authentication via Token, and then simplifies the acquired object. #> function Get-Pass { [CmdletBinding(DefaultParameterSetName='AccessKey')] #[OutputType("System.Collections.ArrayList")] 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]$ValidOnly, [Parameter(Mandatory = $false)] [string]$DateFrom = $(Get-Date -Format yyyy-MM-01T00:00:00.00Z), [Parameter(Mandatory = $false)] [string]$DateTo = $(Get-Date -Format yyyy-MM-ddT23:59:59.00Z), [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 + "/Passes" $Body = @{ validonly = $ValidOnly.IsPresent; personcode = $PersonCode; personid = $PersonId; datefrom = $DateFrom; dateto = $DateTo; } $json = $body | ConvertTo-Json Write-Verbose -Message "Send request to API endpoint $URL with access key $Token." $Pass = Invoke-RestMethod -Method Post -Uri $url -Headers @{ Authorization = $Token } -Body $json -ContentType 'application/json' If ($($Simplified.IsPresent)) { $spass = New-Object -TypeName System.Collections.ArrayList Write-Verbose -Message "Parameter Simplified is present - simplification of the returned object." Foreach ($p in $($Pass.Passes)) { $passobj = New-Object PSObject Add-Member -InputObject $passobj -MemberType NoteProperty -Name Firstname -Value $($pass.Person.Firstname) Add-Member -InputObject $passobj -MemberType NoteProperty -Name Surname -Value $($pass.Person.Surname) Add-Member -InputObject $passobj -MemberType NoteProperty -Name DegreeBefore -Value $($pass.Person.DegreeBefore) Add-Member -InputObject $passobj -MemberType NoteProperty -Name DegreeAfter -Value $($pass.Person.DegreeAfter) Add-Member -InputObject $passobj -MemberType NoteProperty -Name PersonId -Value $($pass.Person.Id) Add-Member -InputObject $passobj -MemberType NoteProperty -Name PersonCode -Value $($pass.Person.Code) Add-Member -InputObject $passobj -MemberType NoteProperty -Name PersonOrdinal -Value $($p.Person.Ordinal) Add-Member -InputObject $passobj -MemberType NoteProperty -Name Ordinal -Value $($p.Ordinal) Add-Member -InputObject $passobj -MemberType NoteProperty -Name Time -Value $($p.Time) Add-Member -InputObject $passobj -MemberType NoteProperty -Name PresenceTypeId -Value $($p.PresenceTypeId) Add-Member -InputObject $passobj -MemberType NoteProperty -Name TerminalName -Value $($p.TerminalName) Add-Member -InputObject $passobj -MemberType NoteProperty -Name Validity -Value $($p.Validity) $spass.add($passobj) | Out-Null } Write-Verbose -Message "Return simplified object Monthly Account." Write-Output $spass } else { Write-Verbose -Message "Return object Pass." Write-Output $($Pass) } } } |