Remove inactive computers

HeathMell
HeathMell Posts: 1
edited May 2023 in General questions

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

Best Answers

Answers

  • JoshP
    JoshP Posts: 894 Senior Moderator

    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: click the triangles to the right of 'Status' to organize your Computers & Contacts by online/offlineclick 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 Community Moderator

    ---

  • 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.

  • roryschmitz
    roryschmitz Posts: 6 ✭✭

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

  • @roryschmitz  Indeed it's in PowerShell.

    Hope you enjoy!

  • roryschmitz
    roryschmitz Posts: 6 ✭✭

    @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?

  • roryschmitz
    roryschmitz Posts: 6 ✭✭

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

  • @roryschmitzMy pleasure, glad I could help!

  • roryschmitz
    roryschmitz Posts: 6 ✭✭

    @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.]

     

  • @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!

  • IanFriis
    IanFriis Posts: 3

    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. :-)

  • roryschmitz
    roryschmitz Posts: 6 ✭✭

    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.

  • @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.

  • IanFriis
    IanFriis Posts: 3

    @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! =)

  • zensbert
    zensbert Posts: 1

    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

  • BambooHR
    BambooHR Posts: 1

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

  • trvadmin
    trvadmin Posts: 1

    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.

     

     

  • Hi @Closed account 

    How can I run this script? sorry i'm not good with technolgy

    For the home studio https://homerecordinghome.com/
  • Fule
    Fule Posts: 1

    Thanks for the script I modified it so it works and all languages (I think)
    Example of running the script Delete-staledevices.ps1 -token "Apikey" -staledays 150

    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True)] [int32]$StaleDays,
    [Parameter(Mandatory=$True)] [String] $Token = ""
    )



    $bearer = "Bearer",$token
    $counter = 0

    $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


    $Days = ((Get-Date).AddDays(-$staleDays))
    $Days = Get-Date $Days -Format s


    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 $Days)
    {

    $counter ++

    Invoke-WebRequest -Uri "Https://webapi.teamviewer.com/api/v1/devices/$ID" -Method Delete -Headers $header -UseBasicParsing
    Write-Host "Deleted device:"$device.alias $counter -ForegroundColor Yellow


    }$Lastseen = $null
    }
    }
    }

     

  • [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True)] [int32]$StaleDays,
    [Parameter(Mandatory=$True)] [String] $Token = ""
    )



    $bearer = "Bearer",$token
    $counter = 0

    $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


    $Days = ((Get-Date).AddDays(-$staleDays))
    $Days = Get-Date $Days -Format s


    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 $Days)
    {

    $counter ++

    Invoke-WebRequest -Uri "Https://webapi.teamviewer.com/api/v1/devices/$ID" -Method Delete -Headers $header -UseBasicParsing
    Write-Host "Deleted device:"$device.alias $counter -ForegroundColor Yellow


    }$Lastseen = $null
    }
    }
    }

     

     

    Nice the this script work perfectly in German !!!

     

     

    thx !!!

  • Thanks for the script. How can I apply this on our teamviewer environment? 

    Many thanks!

  • Save the Above Script in Name Like "TeamViewerDeleteMachines_olderX_Days.ps1"

    simply start CMD run this command with AdminRights

    powershell "PATHtoYOURpowershellScript\TeamViewerDeleteMachines_olderX_Days.ps1" -token YOURTOKENfromTeamVIEWER -staledays 60

    The -staledays 60 stands for the Days you want to delete

     

  • Perfect, this is working! Can I schedule this script on my teamviewer environment?

  • roryschmitz
    roryschmitz Posts: 6 ✭✭

    Yes, you can schedule this.  I run a scheduled task every day to clear out the stale machines.  It keeps things tidy and manageable.

  • PaulM_uk
    PaulM_uk Posts: 2 ✭✭

    I've just tried to run this and I'm finding that the returned object does not contain the last_seen attribute

    Anybody else seen this or have any ideas?

  • PaulM_uk
    PaulM_uk Posts: 2 ✭✭

    Sorted my own issue. If a device is currently online, the last_seen attribute is not returned

    Seems that if you export an array of objects to a csv, it determines the field headers from the first array object.

  • jelrik
    jelrik Posts: 2 ✭✭

    question is it possible to filter out with groups?


    other question we are using vdi envirmont and on all the clients there is a teamvieuwer application running. But when the user end of the day close the vdi new vdi with same id will be created automaticly . And in teamvieuwer then I see 2 the same id only 1 is offline 1 is online. Any solution to this?