New-Pass.ps1

<#
.Synopsis
   Insert a pass into attendance database
.DESCRIPTION
   The function insert a new pass of the specified type for the specified person into attendance 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 PersonCode
    Person's personal identification code.
.PARAMETER PersonID
    Person's database identification id.
.PARAMETER Time
    Date and time of the new pass. If not specified, the current date will be used.
.PARAMETER TerminalID
    Terminal ID of the new pass. If not specified, the ID for manual entry (-1) will be used.
.PARAMETER PresenceTypeID
    Presence Type ID of the new pass. If not specified, the current date will be used.
.PARAMETER Comment
    Comment of the new pass.
.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.

#>

function New-Pass {
    [CmdletBinding(SupportsShouldProcess, 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]$Time = $(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 + "/Pass"

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

      $json = $body | ConvertTo-Json

      Write-Verbose -Message "Send request to API endpoint $URL with access key $Token."

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

         Write-Verbose -Message "Returned response object."
         Write-Verbose -Message $resp
         }
   }
}