Skip to content

Instantly share code, notes, and snippets.

@joshooaj
Created July 12, 2024 17:34
Show Gist options
  • Save joshooaj/975013ed06f7bc16bec5b0d5d8393454 to your computer and use it in GitHub Desktop.
Save joshooaj/975013ed06f7bc16bec5b0d5d8393454 to your computer and use it in GitHub Desktop.
Bulk rename XProtect hardware and child devices
function Rename-VmsHardwareAndDevices {
<#
.SYNOPSIS
Renames hardware and all child devices using the default pattern.
.DESCRIPTION
This function renames the provided hardware(s) and all child devices. If a
value is provided for BaseName, the hardware will be renamed. The BaseName
can contain case-insensitive placeholders for any property available on a
Hardware object.
For example, the default naming pattern applied when adding hardware in
XProtect can be represented using "<model> (<ipaddress>)". Note that the
Hardware object does not have an "IpAddress" property, but one will be
added based on the existing "Address" property containing an http(s) URI.
If no value for BaseName is provided, the hardware will not be renamed and
the existing hardware names will be used for BaseName.
Child devices of all types (camera, microphone, speaker, metadata, input,
and output), will be renamed following the pattern
"<BaseName> - <DeviceType> <Channel>".
.PARAMETER Hardware
Specifies one or more Hardware. Use the Get-VmsHardware command to get
hardware.
.PARAMETER BaseName
Specifies an optional new hardware name. If no value is provided, the
existing hardware name will be used for BaseName. Child devices will be
renamed based on the pattern "<BaseName> - <DeviceType> <Channel>".
.EXAMPLE
Get-VmsHardware | Rename-VmsHardwareAndDevices -BaseName '<model> (<ipaddress>)'
Rename ALL hardware and child devices based on the default XProtect naming
conventions.
.EXAMPLE
$recorders = Get-VmsRecordingServer | Out-GridView -OutputMode Multiple
$recorders | Get-VmsHardware | Rename-VmsHardwareAndDevices -BaseName '<model> (<ipaddress>)'
Rename all hardware on the selected recording server(s) based on the
default XProtect naming conventions.
.EXAMPLE
$recorders = Get-VmsRecordingServer | Out-GridView -OutputMode Multiple
$recorders | ForEach-Object {
$rec = $_
$rec | Get-VmsHardware | Rename-VmsHardwareAndDevices -BaseName "$($rec.Name) <model> (<ipaddress>)"
}
Rename all hardware on the selected recording server(s) based on the
default XProtect naming conventions, but prefix all names with the display
name of the parent recording server.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'Hardware')]
[VideoOS.Platform.ConfigurationItems.Hardware[]]
$Hardware,
[Parameter()]
[string]
$BaseName # = '<model> (<ipaddress>)'
)
process {
foreach ($hw in $Hardware) {
$newName = $hw.Name
if (-not [string]::IsNullOrEmpty($BaseName)) {
$hw | Add-Member -MemberType NoteProperty -Name IpAddress -Value ([uri]$hw.Address).Host
$regex = [regex]::new('<(?<property>\w+)>')
$newName = $BaseName
foreach ($m in $regex.Matches($BaseName)) {
$propertyName = $m.Groups['property'].Value
if ($null -eq $hw.$propertyName) {
Write-Warning "Ignoring unrecognized placeholder '$($m.Groups[0].Value)'"
continue
}
$newName = $newName.Replace($m.Groups[0].Value, $hw.$propertyName)
}
}
if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('BaseName')) {
$hw | Set-VmsHardware -Name $newName
}
$passthruProperties = @(
@{n='Hardware';e={$hw.Name}},
'Name'
)
foreach ($device in $hw | Get-VmsCamera -EnableFilter All) {
$deviceType = ($device.Path -split '\[')[0]
$device | Set-VmsCamera -Name "$newName - $deviceType $($device.Channel + 1)" -PassThru | Select-Object $passthruProperties
}
foreach ($device in $hw | Get-VmsMicrophone -EnableFilter All) {
$deviceType = ($device.Path -split '\[')[0]
$device | Set-VmsMicrophone -Name "$newName - $deviceType $($device.Channel + 1)" -PassThru | Select-Object $passthruProperties
}
foreach ($device in $hw | Get-VmsSpeaker -EnableFilter All) {
$deviceType = ($device.Path -split '\[')[0]
$device | Set-VmsSpeaker -Name "$newName - $deviceType $($device.Channel + 1)" -PassThru | Select-Object $passthruProperties
}
foreach ($device in $hw | Get-VmsMetadata -EnableFilter All) {
$deviceType = ($device.Path -split '\[')[0]
$device | Set-VmsMetadata -Name "$newName - $deviceType $($device.Channel + 1)" -PassThru | Select-Object $passthruProperties
}
foreach ($device in $hw | Get-VmsInput -EnableFilter All) {
$deviceType = ($device.Path -split '\[')[0]
$device | Set-VmsInput -Name "$newName - $deviceType $($device.Channel + 1)" -PassThru | Select-Object $passthruProperties
}
foreach ($device in $hw | Get-VmsOutput -EnableFilter All) {
$deviceType = ($device.Path -split '\[')[0]
$device | Set-VmsOutput -Name "$newName - $deviceType $($device.Channel + 1)" -PassThru | Select-Object $passthruProperties
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment