Skip to content

Instantly share code, notes, and snippets.

@joshooaj
Created January 30, 2021 00:55
Show Gist options
  • Select an option

  • Save joshooaj/35946ffd468c2a52d97f56a1e4c5ea20 to your computer and use it in GitHub Desktop.

Select an option

Save joshooaj/35946ffd468c2a52d97f56a1e4c5ea20 to your computer and use it in GitHub Desktop.
Wrote up a function this afternoon to make it quicker and easier to modify motion detection settings for a camera. Might just refactor it a little and include it in MilestonePSTools later.
function Set-MotionDetection {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact='High')]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[VideoOS.Platform.ConfigurationItems.Camera]
$Camera,
[Parameter()]
[switch]
$Enabled,
[Parameter()]
[switch]
$ManualSensitivityEnabled,
# Specifies the sensitivity on a scale of 0-300. Note the UI shows the scale as 0-100
[Parameter()]
[ValidateRange(0,300)]
[int]
$ManualSensitivity,
[Parameter()]
[ValidateRange(0,10000)]
[int]
$Threshold,
[Parameter()]
[switch]
$KeyframesOnly,
# Specifies the maximum time in milliseconds to process an image for motion detection
[Parameter()]
[ValidateSet(100,250,750,1000)]
[int]
$ProcessTime,
[Parameter()]
[ValidateSet('Normal', 'Optimized', 'Fast', IgnoreCase = $false)]
[string]
$DetectionMethod,
[Parameter()]
[switch]
$GenerateMotionMetadata,
[Parameter()]
[ValidateSet('Off', 'Automatic', IgnoreCase = $false)]
[string]
$HardwareAccelerationMode,
[Parameter()]
[switch]
$UseExcludeRegions,
[Parameter()]
[ValidateSet('Grid8X8', 'Grid16X16', 'Grid32X32', 'Grid64X64', IgnoreCase = $false)]
[string]
$GridSize,
# Specifies the exclusion regions as a string of zeros and ones with a length of 64-4096 characters depending on the chosen GridSize.
[Parameter()]
[string]
$ExcludeRegions
)
process {
$motion = $Camera.MotionDetectionFolder.MotionDetections[0]
$propertyNames = $motion | Get-Member -MemberType Properties | Select -ExpandProperty Name
$dirty = $false
foreach ($key in $PSCmdlet.MyInvocation.BoundParameters.Keys) {
$Camera | Get-Member -
if ($key -notin $propertyNames) {
continue
}
$value = $PSCmdlet.MyInvocation.BoundParameters.$key
if ($motion.$key -ne $value) {
Write-Verbose "Setting $key to $value on $($Camera.Name) ($($Camera.Id))"
$motion.$key = $PSCmdlet.MyInvocation.BoundParameters.$key
$dirty = $true
}
}
if ($PSCmdlet.ShouldProcess("Motion Detection Settings for $($Camera.Name) ($($Camera.Id))", "Update")) {
if ($dirty) {
Write-Verbose "Saving motion detection settings on $($Camera.Name) ($($Camera.Id))"
$motion.Save()
}
}
elseif ($WhatIfPreference) {
$result = $motion.ValidateItem()
if ($result.ValidatedOk -ne $true) {
foreach ($validationError in $result.ErrorResults) {
Write-Error "Validation failed for property '$($validationError.ErrorProperty)': $($validationError.ErrorText)"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment