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
1 -
Painfully obvious they don't give **bleep** about the technical details of their products and instead focus on all the wrong things. DeviceGroups not in API so now we have to rename every single device manually 😊 Smart moves TeamViewer.
The FAQ and guides are complete utter garbage. Among the worst I've ever had the displeasure of reading.
Anywho for mass deletion you can… use api to rename devices older than x days and mass remove them from the online console.
Cant share the whole thing cause it's part of a much bigger script but we did something like this
function Set-TeamViewerStatus {
param (
[Parameter()][string]$TeamViewerAlias,
[Parameter()][string]$DeviceID,
[ValidateSet('Disabled_','Inactive.')][string]$ActionSw,
[Parameter()][string]$LastSeen
)
try {
$RequestBody = @{
'Alias'= '{0}{1}{2}' -f $ActionSw, $LastSeen, $TeamViewerAlias
}
[hashtable] $RequestParam = @{
Uri = ""
Headers =
@{
"Accept" = "application/json"
"Authorization" = "Bearer <token>"
}
Body = $RequestBody | ConvertTo-Json
Method = 'PUT'
ContentType = 'application/json; charset=utf-8'
}
Invoke-RestMethod @RequestParam
}
catch {
Write-Host " --Set-TeamViewerStatus failed:`n$($PSItem.Exception.Message)" -ForegroundColor Red
}
} $CurrentTime = [datetime]::now
foreach ($tvDevice in $TeamViewerHosts.Devices) {
# Reset before each iteration
$Inactive = $null
# Skip if last_seen is empty (If the computer is online, or has never been connected, there is no last_seen..?)
if (![string]::IsNullOrEmpty($tvDevice.last_seen)) {
# Get days inactive
$Inactive = (New-TimeSpan -End $CurrentTime -Start $tvDevice.last_seen).Days
# Check if current device is found in any description
if ($Inactive -ge 90) {
# Update days inactive for already updated devices
if ($tvDevice.Alias -match '.*_') {
Write-Host " --Updated inactivity " -ForegroundColor Yellow -NoNewline
Write-Host " $Inactive/days" -BackgroundColor DarkGray -NoNewline
Write-Host " $($tvDevice.alias)" -ForegroundColor DarkGray
Set-TeamViewerStatus -TeamViewerAlias $tvDevice.Alias -DeviceID $tvDevice.Device_id -ActionSw Inactive. -LastSeen "$($Inactive)(dagar)_"
}
# Write new description or if not already set
if (!$tvDevice.Alias.StartsWith('Inactive') -and ($tvDevice.Alias -notmatch "resurs")) {
Write-Host " --Inactive " -ForegroundColor Yellow -NoNewline
Write-Host " $Inactive/days" -BackgroundColor DarkGray -NoNewline
Write-Host " $($tvDevice.alias)" -ForegroundColor DarkGray
Set-TeamViewerStatus -TeamViewerAlias $tvDevice.Alias -DeviceID $tvDevice.Device_id -ActionSw Inactive. -LastSeen "$($Inactive)(dagar)_"
}
else {
Write-Host " --Inactive " -ForegroundColor Green -NoNewline
Write-Host " $Inactive/days" -BackgroundColor DarkGray -NoNewline
Write-Host " $($tvDevice.alias)" -ForegroundColor DarkGray
}
}
}
}0