Get-Journal.ps1

<#
.Synopsis
   Returns list of database changes.
.DESCRIPTION
   The function returns changes made to the database.
.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 DbOparation
    Array of database operations to be filtered, ["UPDATE","INSERT","DELETE"].
.PARAMETER TableName
     List of tables for which changes are returned.
.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-Journal -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -DbOperation "INSERT"
   Command retrives list of database change with database operation INSERT
.EXAMPLE
   Get-Journal -URL https://intranet.company.com/webtime12/api -Token $MyToken -PersonCode ("INSERT","DELETE") -Simplified
   Command retrives list of database change with database operation INSERT with authentication via Token, and then simplifies the acquired object.
#>

function Get-Journal {
    [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[]]$DbOperation,
        [Parameter(Mandatory = $false)]
        [string[]]$TableName,
        [Parameter(Mandatory = $false)]
        [switch]$ChangeOnly,
        [Parameter(Mandatory = $false)]
        [string]$DateFrom = $(Get-Date -Format yyyy-MM-01T00:00:00.00Z),
        [Parameter(Mandatory = $false)]
        [string]$DateTo = $(Get-Date -Format yyyy-MM-ddT00:00:00.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 + "/Journal"

       $Body = @{
            datefrom = $DateFrom;
            dateto = $DateTo;
            dbops = $DbOperation;
            tablenames = $TableName;
            changeonly = $ChangeOnly
            }

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

       If ($($Simplified.IsPresent)) {
            $ji = @()
            Write-Verbose -Message "Parameter Simplified is present - simplification of the returned object."
            Foreach ($journalitem in $($Journal)) {
                $juritem = New-Object PSObject
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name Firstname -Value $($journalitem.Person.Firstname)
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name Surname -Value $($journalitem.Person.Surname)
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name DegreeBefore -Value $($journalitem.Person.DegreeBefore)
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name DegreeAfter -Value $($journalitem.Person.DegreeAfter)
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name PersonCode -Value $($journalitem.Person.Code)
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name PersonId -Value $($journalitem.Person.Id)
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name Application -Value $($journalitem.Application)
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name DbOp -Value $($journalitem.DbOp)
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name TableName -Value $($journalitem.TableName)
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name ChangeTime -Value $($journalitem.ChangeTime)
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name CompUser -Value $($journalitem.CompUser)
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name CompName -Value $($journalitem.CompName)
                Add-Member -InputObject $juritem -MemberType NoteProperty -Name Items -Value $($journalitem.Items)

                $ji += $juritem
                }
            Write-Verbose -Message "Return simplified object Journal."
            $ji
            }
        else {
            Write-Verbose -Message "Return object Journal."
            $($Journal)
        }
    }
}