Posted by HeathMell
Henagon

Remove inactive computers

I'm using TeamViewer 13 Corporate, and I've been using it for several years now. 

Many of the computers in our organization have been retired or changed out, but the naming convention for TeamViewer computer is the same for all of our users regarless if they've had one, two, three or even a hundred computers assigned to them.  Is it possible to add a feature that would allow you to search for computers that have been offline for a cetain time frame, like two or three moths for example?  Then from that list, be able to do a bulk delete?  I'm aware that the bulk delete has been asked for in many other posts as well, and it makes no sense to me that this feature would not be available by design.

Also, there are many computers listed in "Unnamed devices" folder, for one time connects I've used in the past, is there no way I can delete these from my system as well?  I'm just trying to make my console clean and relevant, but you guys seem to be tying our hands with these basic functions by only permitting us to delete computers one at a time.

I would apreciate any response from TeamViewer staff on this matter.

Thank you

2 Accepted Solutions

Accepted Solutions
Posted by KyleHoneycutt
Digon
Solution

Re: Remove inactive computers

I was running into this same problem but wasn't satisfied with the workaround provided. I spend countless hours trying to automate any and everything so this was right up my alley.

It will delete any device that hasn't been seen in the last 30 days. Just insert your API Token in the $token variable and you'll be good to go.

Hope this helps all the fellow admins.

$token = "INSERTYOURAPITOKENHERE"
$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
            }
    }
}
Posted by KyleHoneycutt
Digon
Solution

Re: Remove inactive computers

@roryschmitz Design and Deploy won't be the correct API token. I actually just use the same API Token I use for my Full Version installer but if you don't already have that just follow these steps to create one:

1. When you are logged in click your Profile in the top right then click Edit Profile.

2. Click the Apps tab on the left and then create a new script token.

3. Change Group Management to "View, create, delete, edit and share groups"

4. Change Computers & Contacts to "View, add, edit and delete entries"

5. Click Save then copy your API Token into the script.

teamviewerapitoken.png

 

18 Replies
Posted by Senior Moderator Senior Moderator
Senior Moderator

Re: Remove inactive computers

Hello @HeathMell,

Thank you for yourfeedback.

Currently, you can only organize devices based on their online and offline statuses in the Management Console, to make location of the offline devices you wish to delete easier: organize offline.pngclick the triangles to the right of 'Status' to organize your Computers & Contacts by online/offline

In regards to unnamed devices, the unnamed devices list is the list that for the devices that you have connected before but are not in your computers and contacts list. As a licensed user, all connections you make are reported in the Connection Reports; including devices that are not in your list.

As these devices are not in your Computers & Contacts list, in order to remove these you will want to delete the connection reports for the devices themselves:

  1. Click on the Unnamed Devices group in the Management Console,  and then on the Connection Report tab
  2. Clear all filters, set date range as desired, select User reports
  3. Check the box at the top of the list to mark all connections
  4. Click on Tools | Delete Selected to remove all unnamed connections. Repeat as necessary

As a licensed user, should any further issues arise regarding the unnamed devices, I would recommend reaching out to our support team directly.

I will forward your request for bulk-delete and search based on offline status to our Product Development team as well.

 

Josh P.
Senior Moderator

If my reply answered your question, help out other users and click the Accept as a Solution  button below.

You can also say thanks by clicking on the Thumbs Up button!

Thanks for being an active member of our Community!

Japanese Community (日本語コミュニティ) |  Chinese Community (中文社区) |  German Community (Deutschsprachig) | French Community (Communauté française) | English Community | Spanish Community (Comunidad española)

Posted by IanFriis
Digon

Re: Remove inactive computers

We would also very much like the feature to bulk remove retired computers. We recently reinstalled 4000 units with Windows 10, thus reinstalling teamviewer. Now, they alle are doubled in the console.

Posted by KyleHoneycutt
Digon
Solution

Re: Remove inactive computers

I was running into this same problem but wasn't satisfied with the workaround provided. I spend countless hours trying to automate any and everything so this was right up my alley.

It will delete any device that hasn't been seen in the last 30 days. Just insert your API Token in the $token variable and you'll be good to go.

Hope this helps all the fellow admins.

$token = "INSERTYOURAPITOKENHERE"
$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
            }
    }
}
Posted by roryschmitz
Henagon

Re: Remove inactive computers

@KyleHoneycutt Brilliant!  Is this just a powershell cmdlet or do we run this from somewhere else?

Posted by KyleHoneycutt
Digon

Re: Remove inactive computers

@roryschmitz  Indeed it's in PowerShell.

Hope you enjoy!

Posted by roryschmitz
Henagon

Re: Remove inactive computers

@KyleHoneycutt Thanks, I'm getting the following errors when it's running through the script.  It appears to find all of the stale comptures out there, but is this error pops up for each machines:

Invoke-WebRequest : {"error":"invalid_token","error_description":"Access token does not have the required permissions
for this function.","error_code":2}
At C:\Downloads\TV_Cleanup.ps1:30 char:21
+ ...             Invoke-WebRequest -Uri "Https://webapi.teamviewer.com/api ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Deleted device: COMPUTERNAME

Unsure why the access token wouldn't have the required permissions?  We only have the one token that I set up.  Maybe I'm not using the correct API token, but I just copied it from the Design & Deploy area in the web admin.  It's the one under custom TV Host package I created.  Is that the correct token?

Posted by KyleHoneycutt
Digon
Solution

Re: Remove inactive computers

@roryschmitz Design and Deploy won't be the correct API token. I actually just use the same API Token I use for my Full Version installer but if you don't already have that just follow these steps to create one:

1. When you are logged in click your Profile in the top right then click Edit Profile.

2. Click the Apps tab on the left and then create a new script token.

3. Change Group Management to "View, create, delete, edit and share groups"

4. Change Computers & Contacts to "View, add, edit and delete entries"

5. Click Save then copy your API Token into the script.

teamviewerapitoken.png

 

Posted by roryschmitz
Henagon

Re: Remove inactive computers

@KyleHoneycutt That worked!  You're a life saver and I really appreciate it.

Posted by KyleHoneycutt
Digon

Re: Remove inactive computers

@roryschmitzMy pleasure, glad I could help!

Posted by roryschmitz
Henagon

Re: Remove inactive computers

@KyleHoneycutt I ended up posting your great solution over at the Spiceworks forums as well and linked back to here.  Hope that is OK?  If there's a way I can buy you a beer or two, please let me know. 

Here's the discussion link:  [The URL link has been removed by a moderator.]

 

Posted by KyleHoneycutt
Digon

Re: Remove inactive computers

@roryschmitz Perfectly fine with me, spread it around as much as you wish! If you're ever in the Dallas area hit me up and we can definitely have a few beers. Cheers!

Posted by IanFriis
Digon

Re: Remove inactive computers

Hi there!

Thanks a lot for providing a solution here.. :-)

I am having another issue though, regarding the comparison of the dates. I'm trying to clean out devices not seen in a year, so i modified the script to use '360' instead of '30', but i don't think that is the issue - the problem is the time/date formats. I get a lot of this error (in danish):

Could not compare "03/21/2018 00:00:00" to "søndag den 11. marts 2018". Error: "Cannot convert value "søndag den 11. marts 2018" to type "System.DateTime". Error: "Strengen blev ikke genkendt som en gyldig DateTime. Der er et ukendt ord, der starter ved indekset 0.""
At C:\Users\adm_ianfje\Documents\TeamViewer_Cleanup_Inactive_Computers_Script.ps1:31 char:25
+ if ($DateLastSeen -le $360Days)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : ComparisonFailure

I am totally new at powershell scripting, so i got no clue how to fix this. Please help. :-)

Posted by roryschmitz
Henagon

Re: Remove inactive computers

I'm not a PS expert either, but as a quick test, I modified this line:

$30Days = ((Get-Date).AddDays(-30)).GetDateTimeFormats()[5]

to this:

$30Days = ((Get-Date).AddDays(-365)).GetDateTimeFormats()[5]

and I did not receive any errors.  That could also be due to mine not finding any machines older than 365 days as I just removed everything older than 30 days.   It's a little messy, but it might be a quick test.  There shouldn't be any reason you couldn't replace '30' with '365' throughout the script.

Posted by KyleHoneycutt
Digon

Re: Remove inactive computers

@IanFriis Hey Ian,

I'm not sure if PowerShell can convert the Danish language into a System.DateTime Object which is why you're getting that error.

The way the script works is that it gets the date string from TeamViewer and then converts that string into a DateTime object so that it can be used as a comparison operator. Without converting it, it can't compare correctly.

I think the easiest thing for you to do would be to change your local system language to English or run the Script on a server that has English as the OS language.

If that workaround isn't feasible for you just let me know and we can try to come up with another solution for you.

Posted by IanFriis
Digon

Re: Remove inactive computers

@KyleHoneycutt @roryschmitz thank you for replying :)

I solved the problem by setting my system language to english on the computer running the script. After a reboot the script ran without errors!

Great to finally see those outdated computers removed from our list. 

Thanks again - have a great day! =)

Highlighted
Posted by zensbert
Electron

Re: Remove inactive computers

Hi Kyle, we are getting the same error as Ian. Our system runs in german language and we would like to keep it that way. The error we receive is:

Could not compare "03/13/2019 00:00:00" to "19.9.2018". Error: "Cannot convert value "19.9.2018" to type "System.DateTime".

i tried a little bit with converting the format but i didn´t manage to get this script running.

Thanks in advance,

Thomas

1 Reply
Posted by BambooHR
Henagon

Re: Remove inactive computers

This was a great help. I decided to recreate this in Python 3. If anyone is interested, let me know!

Posted by trvadmin
Photon

Re: Remove inactive computers

Hi,

Not sure if it still relevant but i had the same issue with the German format on my Win10 device, and after changing to the English (United states) formats the script was working correctly.