teamviewer.com link icon
or
Ask The Community

Devices API endpoint question

AndrewTaylor
AndrewTaylor Posts: 2 ✭✭
edited October 2022 in API and scripting

Hi,

I want to use the Teamviewer API to automatically remove our machines from Teamviewer, i can see there is the devices endpoint /api/v1/devices with the parameter option for "name" that will return the device ID

However when I use that endpoint with a device name (https://webapi.teamviewer.com/api/v1/devices?parameters.name=DV00310) it just returns a full list of every machine in our organisation. I can see on the returned json that "name" isn't actually an option it returns, am I using this wrong?

"devices": [

{

"remotecontrol_id": "string",

"device_id": "string",

"userid": "string",

"alias": "string",

"groupid": "string",

"description": "string",

"online_state": 0,

"policy_id": "string",

"assigned_to": true,

"supported_features": "string",

"last_seen": "2022-10-18T10:07:33.519Z",

"teamviewer_id": "string"

}

]

}


Can someone point me in the right direction? My aim is to create a script that allows us to delete a device based on its hostname, I know you need the device ID to call the endpoint which is why I'm wanting to filter out the info based on the device name


Thank you,

Andrew

Comments

  • SuperD0S
    SuperD0S Posts: 9 ✭✭

    did just this a couple of days ago, since there's no way to fetch a single device you need to get all devices and the filter out the right computer via Alias. After that use the DELETE /api/v1/devices/{id} 

  • AndrewTaylor
    AndrewTaylor Posts: 2 ✭✭

    @SuperD0S Thats what i was hoping to avoid, had a similar attempt go wrong a few years ago, do you have a sample script I could reference by any chance? I'll be writing this in Powershell but any language would be good to see

  • SuperD0S
    SuperD0S Posts: 9 ✭✭

    Forgot to say you can query a single device but you need to use the Teamviewer ID but often you want to query via the alias field which often is the computer name.

    Here's a script that utilize a script token that you can create in TeamViewer Mangement Console (https://login.teamviewer.com/nav/profile/apps):

    #Remove Teamviewer Device

    #Vars

    $tvApiVersion = 'v1'

    $tvApiBaseUrl = 'https://webapi.teamviewer.com'

    $accessToken = 'ENTER YOUR SCRIPT TOKEN'

    $DeviceApi = "$tvApiBaseUrl/api/$tvApiVersion/devices"


    function Get-TeamViewerDevices {

      param (

        [Parameter(Mandatory = $true)][string]$accessToken,

        [Parameter(Mandatory = $false)][string]$deviceID,

        [Parameter(Mandatory = $false)][string]$alias

      )


      try {

        if ($deviceid -and !$alias) {

          $(Invoke-RestMethod -Uri "$DeviceApi/$deviceid" -Method Get -Headers @{authorization = "Bearer $accessToken" } -ErrorAction Stop).devices 

        }

        elseif ($alias -and !$deviceid) {

          $(Invoke-RestMethod -Uri $DeviceApi -Method Get -Headers @{authorization = "Bearer $accessToken" } -ErrorAction Stop).devices | Where-Object -FilterScript { $_.alias -match $alias }

        }

        elseif (!$alias -and $deviceid) {

          $(Invoke-RestMethod -Uri $DeviceApi -Method Get -Headers @{authorization = "Bearer $accessToken" } -ErrorAction Stop).devices

        }

        else {

          "Please Use only deviceID OR alias!"

        }

      }

      catch {

        Write-Error $PSItem.Exception.Message

      }  

      

    }


    function Remove-TeamViewerDevice {

      [CmdletBinding(SupportsShouldProcess)]

      param (

        [Parameter(Mandatory = $true)][string]$accessToken,

        [Parameter(Mandatory = $true)][string]$deviceid

      )


      try {

        if ($PSCmdlet.ShouldProcess($deviceid)) {

          Write-Host "Deleting TeamViwer device: $deviceid" -ForegroundColor Green

          $(Invoke-RestMethod -Uri "$DeviceApi/$deviceid" -Method Delete -Headers @{authorization = "Bearer $accessToken" } -ErrorAction stop )

        }

        else {

          Write-Host "WhatIf: The TeamViewer device with ID $deviceid will be deleted!" -ForegroundColor Yellow

        }     

         

      }

      catch {

        Write-Error $PSItem.Exception.Message

      }


    }


    #Example: Remove Computer with Alias "TestComputer"

    $device=(get-teamViewerDevices -accessToken $accessToken -alias "TestComputer").device_id

    Remove-TeamViewerDevice -accessToken $accessToken -deviceid $device

  • SuperD0S
    SuperD0S Posts: 9 ✭✭

    @AndrewTaylor did you get it to work?