New-Pass.ps1

<#
.Synopsis
   Insert a pass into attendance database
.DESCRIPTION
   The function insert new pass of the specified type for the specified person into attendance database.
.EXAMPLE
   New-Pass -URL https://intranet.company.com/webtime12/api -AccessKey 56879065 -PersonCode 1045 -TerminalID "-1" -PresenceTypeID 1 -Comment "API Pass"
   This command insert new pass of the specified type for the specified person code into attendance database with comment and terminal.
.NOTES
   General notes
#>

function New-Pass {
    [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName='AccessKey')]
    param(
        # Server API url
        [Parameter(Mandatory = $true)]
        [string]$URL,
        # Authentication token for accessing API data
        [Parameter(Mandatory = $true,ParameterSetName='Token')]
        [string]$Token,
        # The AccessKey to get an authentication token for accessing API data
        [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)]
        [string]$TerminalID = -1,
        [Parameter(Mandatory = $true)]
        [string]$PresenceTypeID,
        [Parameter(Mandatory = $false)]
        [string]$Comment
)

   Process {

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

      $URL = $URL + "/Passes"

      $Body = @{
         terminalid = $TerminalID;
         presencetypeid = $PresenceTypeID;
         time = $Date;
         comment = $Comment
         personcode = $PersonCode;
         personid = $PersonID;
      }

      $json = $body | ConvertTo-Json

      if ($pscmdlet.ShouldProcess("$URL", "New Pass")){
         $resp = Invoke-RestMethod -Method Put -Uri $url -Headers @{ Authorization = $Token } -Body $json -ContentType 'application/json'
         $resp
      }
   }
}