public/Get-AxisRecording.ps1

<#
.SYNOPSIS
Retrieves a list of recordings from an Axis device within a specified time range.
 
.DESCRIPTION
The Get-AxisRecording function retrieves recordings from an Axis device based on the specified parameters.
 
.PARAMETER Device
The Hostname or IP address of the Axis device.
 
.PARAMETER Lens
The source lens for the recordings.
If not specified, all lenses are included.
 
.PARAMETER MaxResults
The maximum number of recordings to retrieve. Optional.
 
.PARAMETER StartTime
The start time of the recording range.
 
.PARAMETER StopTime
The stop time of the recording range.
 
.PARAMETER ThrowErrorOnTimeDifference
Specifies whether to throw an error if the time difference between the device and the current time is greater than 60 seconds. Optional.
 
.EXAMPLE
Get-AxisRecording -Device 192.168.1.100 -StartTime (Get-Date).AddDays(-1) -StopTime (Get-Date)
 
This example retrieves all recordings within the last 24 hours.
 
.NOTES
This function checks the time difference between the device and the current time and will warning if the difference is greater than 60 seconds.
#>

function Get-AxisRecording {
    [cmdletbinding()]
    Param(
        [Parameter(Mandatory)]
        [String]$Device,

        [Parameter()]
        [String]$Lens,

        [Parameter()]
        [int]$MaxResults,

        [Parameter(Mandatory)]
        [DateTime]$StartTime,

        [Parameter(Mandatory)]
        [DateTime]$StopTime,

        [Parameter(DontShow)]
        [Switch]$ThrowErrorOnTimeDifference
    )

    $DeviceTime = Get-AxisDate -Device $Device
    if([Math]::Abs($DeviceTime.OffsetFromCurrent) -gt 60) {
        if($ThrowErrorOnTimeDifference) {
            throw "The time difference between the device and the current time is greater than 60 seconds."
        }
        Write-Warning "The time difference between the device and the current time is greater than 60 seconds."
    }

    $DateTimeFormat = "yyyy-MM-ddThh:mm:ss"

    $begin = $StartTime.ToString($DateTimeFormat)
    $end = $StopTime.ToString($DateTimeFormat)
    if($StartTime.Kind -ne 'Utc') {
        $begin = $StartTime.ToUniversalTime().ToString($DateTimeFormat)
    }

    if($StopTime.Kind -ne 'Utc') {
        $end = $StopTime.ToUniversalTime().ToString($DateTimeFormat)
    }

    $Param = @{
        Device = $Device
        Path = "/axis-cgi/record/list.cgi?starttime=$begin&stoptime=$end"
    }
    if($Lens) {
        $Param.Path += "&source=$Lens"
    }
    if($MaxResults) {
        $Param.Path += "&maxnumberofresults=$MaxResults"
    }
    $result = (Invoke-axiswebApi @Param).root.recordings
    
    if($result.numberOfRecordings -eq 0) {
        Write-Warning "No recordings found for the specified time range."
        return
    }

    return $result.recording
}