Powershell Scripting Removal of Old Offline Devices Help
Searching the forums I found this, albeit it is old, and it is sadly returning no errors but also not working as i still have devices 4 months old showing. Not sure what the issue is. They are shared but I am the main owner of the share.
$token = "SCRIPTAPITOKENHERE"
$bearer = "Bearer",$token
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("authorization", $bearer)
$devices = (Invoke-RestMethod -Uri "Https://webapi.teamviewer.com/api/v1/devices" -Method Get -Headers $header).devices
$30Days = ((Get-Date).AddDays(-30)).GetDateTimeFormats()[5]
ForEach($device in $devices)
{
if ($device.online_state -eq "Offline")
{
$ID = $device.device_id
$Lastseen = $device.last_seen
if ($Lastseen -ne $null)
{
$LastSeen = ($device.last_seen).Split("T")[0]
[datetime]$DateLastSeen = $LastSeen
if ($DateLastSeen -le $30Days)
{
Invoke-WebRequest -Uri "Https://webapi.teamviewer.com/api/v1/devices/$ID" -Method Delete -Headers $header
Write-Host "Deleted device:"$device.alias -ForegroundColor Yellow
}$Lastseen = $null
}
}
}
Any help would be appreciated at we have thousands of devices i'd otherwise have to scan through, they don't seem to have a report where i can bulk edit and filter by offline or muchless offline for x days. Probably purposeful to make it harder to remove licenses as a business decision...
Answers
-
We are limited to a maximum of 60 devices with the API. Therefore, if you have more than 60 devices, they will not be displayed in your script
0