Pulling Teeth (Reports) – the TeamViewer way

Almost everyone has heard of TeamViewer, and believe it or not there are people that actually use it in business (so they actually pay for licenses and not just use the free edition).

Paying for a license enables you to have management in terms of how the clients (or HOSTS) are configured and reports on which technician is connecting to which machine and for how long. However getting a report from the system actually detailing which machines the software is deployed to, what policy is applied to them, if the online or offline and remote ID…well lets just say that it is not as easy as hitting an export button.

So my one clients had an issue.  They had just had a new TeamViewer (TV from now onwards) version rolled out (that is a a whole other post for another day) into their environment and things were not matching up to their expectations.  They had two different deployment groups on TV and one policy defined for both. Looking on the web portal it seemed that some machines were in the one group and not the other, some were only in the new group and some did not have the policy applied to them.  

Then for good measure, throw in a SCCM report or two, a CMDB report and another deployment tool report and then things start getting REALLY wild in terms of reporting.

Logically it should be no problem pulling a report like that – lets go into TV Console, lets go to the All Devices Group and lets see if there is an export of some sorts?  We needed a report that shows Computer Name, Policy Applied or Not; and this for ALL the deployment Groups in TV.  After clicking ALL over and not finding anything that remotely came to an export function it was time to knock on Google’s door.

One of the posts I stumbled across stated that the information could only be  pulled using a Web API query and a Powershell script – say what?!?!  Looking around the TV site it seems that by doing it this way you could build your own tools and have it run queries against your tenant – great if you are a programmer, not so much if you don’t have coding skills. Here is a link to their documentation regarding this.

The process in the original post above states that you create a token for your site/tenant, and then create a Powershell script that you run and enter this token into it and then magically a file or two should appear on the desktop with your export.   That is in theory.  Here is the code that I copied and pasted into Powershell ISE and SAVED as a .ps1 file (TVDeviceExport2.ps1)  (slightly edited so I could get all the values for Devices and not just a filter of it).  Then ran it in Powershell

$token = Read-Host -Prompt "Enter your API account token now"
$bearer = "Bearer",$token

$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("authorization", $bearer)

$request = Invoke-RestMethod -Uri "https://webapi.teamviewer.com/api/v1/devices" -Method Get -Headers $header
$request.devices | Export-Csv "$env:userprofile\Desktop\TVDevicesListExport2.csv"

$grequest = Invoke-RestMethod -Uri "https://webapi.teamviewer.com/api/v1/groups" -Method Get -Headers $header
$grequest.groups | Select-Object -Property "name","id" | Export-Csv “$env:userprofile\Desktop\TVGroupsListExport2.csv"

What I got was this error (tip RED is never good):

Invoke-RestMethod : {“error”:”invalid_token”,”error_description”:”Access token is of wrong type. You need a UserToken to call this function.”,”error_code”:2}

Seems that I had created a SITE token instead of a USER token – Doh!

So off to the User Management section of the web portal, found my username then I clicked on the drop down by my name in the top RIGHT and then Edit Profile.  Then click Apps on the left menu, and then Create Script Token button at the bottom of the window.  Here I then selected Read Only / View rights (as I wanted to make sure I did not change anything) and saved it as “User Read Only”.  A unique token was then generated and displayed on the screen – I have obviously blacked this out in the screenshot below.

Using this new USER TOKEN I ran the Powershell script and magically two CSV files appeared on my desktop – TVDevicesListExport2.csv   and    TVGroupsListExport2.csv

TVDevicesListExport2.csv – List of Devices
TVGroupsistExport2.csv – Group Names and ID number

As you can see, the device list has characters and number values for the GroupID and PolicyID.  We have the GroupID so we can use Find and Replace function in Excel and change the GroupID to a legible value, but seems we don’t have the PolicyID values.   Time to refer to the TV API documentation…

After 3 attempts found I could run the following code to pull the Policy information into it’s own file   (TVPolicies2.ps1):

$token = Read-Host -Prompt "Enter your API account token now"
$bearer = "Bearer",$token

$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("authorization", $bearer)

$request = Invoke-RestMethod -Uri "https://webapi.teamviewer.com/api/v1/teamviewerpolicies" -Method Get -Headers $header
$request.policies | Export-Csv "$env:userprofile\Desktop\TVPoliciesExport2.csv"

The output was exactly what I needed:

TVPoliciesExport2.csv – used to find the PolicyID value

Time to complete the Find and Replace in Excel and then provide the report to the client.

MadMike posted at 2020-11-18 Category: Coding and Scripts

Leave a Reply

(Ctrl + Enter)