Hi,
Since we are including the Teamviewer agent in our OS-deployment we get duplicate alias when a computer is reinstalled.
I just wrote this script that removes duplicate objects with the same alias and only keeps the most recent object.
I am not the best at writing scripts so please test it and get back here if you think something should be changed.
It only deletes the outer loop object to keep the logic a little bit more simple.
Update with your token
If you want to test it without actually deleting objects you can comment out the following row to see what the end result would be:
Invoke-WebRequest -Uri "Https://webapi.teamviewer.com/api/v1/devices/$ID" -Method Delete -Headers $header
#script to identify duplicates alias in Teamviewer and deleting the objects with the oldest last seen timestamp
#define token
$token = "ENTERYOURTOKENHERE"
$bearer = "Bearer",$token
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("authorization", $bearer)
#Getting all the devices from API
$devices = (Invoke-RestMethod -Uri "Https://webapi.teamviewer.com/api/v1/devices" -Method Get -Headers $header).devices
#Total number of devices
write-host "Total Number of devices" $devices.Count
#Looping through all devices
ForEach($device in $devices)
{
#Settings variable for device ID
$ID = $device.device_id
#Setting device last seen variable
$deviceLastseen = $device.last_seen
#resetting device deleted
$devicedeleted = $false
#Looping through all devices again to be able to compare each object with all other objects
ForEach($deviceloop in $devices)
{
#Only do something if the device is not already delted
if($devicedeleted -ne $true)
{
#Setting inner loop variable for device last seen
$deviceLastseenloop = $deviceloop.last_seen
#Only do something if last seen values are populated
if ($deviceLastseen -ne $null -and $deviceLastseenloop -ne $null)
{
#Compare other loop device to inner loop device by alias, if they are the same but device ID is not, enter if statement.
if($device.alias -eq $deviceloop.alias -and $device.device_id -ne $deviceloop.device_id )
{
#cleanup date formatting
$deviceLastseen = ($device.last_seen).Split("T")[0]
$deviceLastseenloop = ($deviceloop.last_seen).Split("T")[0]
Write-Host "Comparing "$device.alias $deviceLastseen $device.teamviewer_id "to" $deviceloop.alias "|" $deviceLastseenloop "|" $deviceloop.teamviewer_id -ForegroundColor Yellow
#If duplicate found and outer loop device is older delete object
if($deviceLastseen -lt $deviceLastseenloop)
{
#Deleting outer loop device $ID = $device.device_id set above
Invoke-WebRequest -Uri "Https://webapi.teamviewer.com/api/v1/devices/$ID" -Method Delete -Headers $header
Write-Host "Deleted device:"$device.alias "|" $deviceLastseen "|" $device.teamviewer_id -ForegroundColor Green
$count++
$devicedeleted = $true
}
}
}
#reset last seen value
$deviceLastseenloop = $null
}
}
#reset last seen value
$deviceLastseen = $null
}
#Total number of delted objects
write-host "Total number of deleted objects" $count
#Reset count
$count = $null
#read devices again
$devices = (Invoke-RestMethod -Uri "Https://webapi.teamviewer.com/api/v1/devices" -Method Get -Headers $header).devices
#Total number of devices
write-host "Total Number of devices" $devices.Count
I take no responsibility for you using this script. Its on your own risk :)
Best regards
Dan