Skip to main content

One post tagged with "Duplicate"

View All Tags

· One min read

I'm the author of the NinjaOne PowerShell module so when I was looking for a quick way to identify duplicate devices I turned to that module and the NinjaOne API to find a solution.

About this script

This script is a quick way to identify duplicate devices in your NinjaOne instance. It can return a full listing of all devices grouped by serial number, it can also just return the ID, last contact date and serial number for the device with the oldest last contact date.

The script

Get-NinjaOneDuplicateDevices.ps1
#Requires -Module NinjaOne
#Requires -Version 7
[CmdletBinding()]
param (
# Returns all devices with duplicates.
[Parameter(ParameterSetName = 'All', Mandatory)]
[Switch]$All,
# Returns the duplicate devices only.
[Parameter(ParameterSetName = 'Duplicates', Mandatory)]
[Switch]$Duplicates
)

try {
$DuplicateDevices = Get-NinjaOneDevices -detailed | Group-Object { $_.system.serialNumber } | Where-Object { $_.count -gt 1 -and ($_.name -ne '$(DEFAULT_STRING)' -and $_.name -ne 'Default string') }
if ($All) {
$Output = $DuplicateDevices | ForEach-Object { $_ | Select-Object -ExpandProperty group | Sort-Object $_.lastContact }
} elseif ($Duplicates) {
$Output = $DuplicateDevices | ForEach-Object { $_ | Select-Object -ExpandProperty group | Sort-Object $_.lastContact | Select-Object -First 1 -Property id, lastContact, @{ name = 'serialNumber'; expression = { $_.system.serialNumber } } }
}
if ($Output) {
return $Output
} else {
Write-Warning 'No duplicate devices found or an unanticipated error occurred.'
}
} catch {
Write-Error 'Please ensure the NinjaOne PowerShell module is correctly installed and you have run the "Connect-NinjaOne" command and connected to NinjaOne.'
exit 1
}

At this time it's not possible to remove devices via the NinjaOne API so this script will only return the duplicate devices - you'll have to arrange to remove these devices manually.

If you want all devices with duplicates:

Get-NinjaOneDuplicateDevices -All

or just the older duplicated devices:

Get-NinjaOneDuplicateDevices -Duplicates