Highlighted
Posted by
Trigon

Unshare and Share groups using TeamViewer API + Powershell

Hi there,

I'm trying to work out this powershell module wrapper that's used for TeamViewer API and can't seem to get my head around it?

I get stuck on this section here:

<#
.SYNOPSIS
Unshares a group from certain users.

.DESCRIPTION
Unshares a group from certain users.

.PARAMETER GroupID
Teamviewer Group ID

.PARAMETER UserID
Teamviewer User ID

.PARAMETER token
Is the User Level Token that you can create from the Teamviewer Management Console
Can use Set-TVToken Function will then not be nessessary to use this paramameter

.EXAMPLE
PS C:\> Unshare-TVGroup -GroupID $value1 -UserID $value2

.NOTES
For more Details see Teamviewer API token Documentation
https://www.teamviewer.com/en/for-developers/teamviewer-api/
https://dl.tvcdn.de/integrate/TeamViewer_API_Documentation.pdf
#>
function Unshare-TVGroup
{
[CmdletBinding(ConfirmImpact = 'Medium',
PositionalBinding = $false,
SupportsPaging = $true,
SupportsShouldProcess = $true)]
param
(
[Parameter(Mandatory = $true)]
$GroupID,
[Parameter(Mandatory = $true)]
$UserID,
$Token
)

if ($token)
{ }
elseif ($global:TVToken)
{
$token = $global:TVToken
}
else
{
Write-Output "You need to Set the Token"
break
}
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("authorization", "Bearer $token")
$body = @{
Users = @(
userid = "$UserId"
)
} | ConvertTo-Json
Invoke-RestMethod -Uri "https://webapi.teamviewer.com/api/v1/groups/$groupID/unshare_group" -Method Post -Headers $header -ContentType application/json -Body "$body"
}

This is apart of the Powershell script located here online:
https://www.powershellgallery.com/packages/TeamViewerPSModules/1.3.1.0/Content/TeamViewerPSModules.p...

Error I am getting is:

Invoke-RestMethod : :"Unexpected character encountered while parsing value: {. Path 'Users', line 3, position 19. After parsing a value an unexpected
character was encountered: :. Path 'Users[0]', line 4, position 30. After parsing a value an unexpected character was encountered: :. Path 'Users[0]', line 4, position 30.","error_code":1}
At C:\_RCT\IT TOOLS\Scripts\PowerShell Scripts\TeamViewerPSModules.psm1:701 char:5

Can someone please explain and help me get around this? I am looking to unshare groups with TVUsers and then to Reshare with the same TVUsers, the reason for this is to fix the "Grant Easy Access" which relates back to my replys on this post here:

https://community.teamviewer.com/t5/API-and-Scripting/Easy-Access-Requesting-Password/m-p/67116#M104...

Our IT accounts are broken without easy access and we are having to refer to alternatives, unattended password, windows credentials, TV ID and password, etc. and everytime we move machines to different groups this feature breaks... hence why I am looking for an easy way to reshare the groups through powershell to speed up the process, and yes TV are aware of the bug as I've mentioned countless times along with the black screen still not working on remote screens?

Cheers

4 Replies
Highlighted
Posted by
Digon

Re: Unshare and Share groups using TeamViewer API + Powershell

I'm running into the same thing for the same reason.  Did you ever get a solution?

Highlighted
Posted by
Trigon

Re: Unshare and Share groups using TeamViewer API + Powershell

Unfortunately Easy Access for us is still broken, we've tried to resort to PowerShell fixing this issue for us with unsharing and sharing the groups using TV API and that still does not work.


Yet to get a resolution from TV at the moment......

Highlighted
Posted by
Digon

Re: Unshare and Share groups using TeamViewer API + Powershell

That's unfortunate.  I discovered this a few weeks ago after starting a support case with TeamViewer.  I was told at the time that they were working on a fix and I would be updated when it's done, but I see you started this post in July so I guess I won't hold my breath at this point.

We just completed a TV12 to TV14 upgrade.  TV12 was never really accepted by the users, and our hope was that we could do better with TV14 and remove all the other remote control software in our environment, but now I'm not so sure that will happen.  Maybe it's just time to move on from TeamViewer.

Highlighted
Posted by
Digon

Re: Unshare and Share groups using TeamViewer API + Powershell

I got the following from TeamViewer support to unshare/reshare all groups.  The script does what it's supposed to do, but only time will tell if this is a sufficient work around to the real problem.  I only just implemented this last night so I'll try to remember to report back in a week or two with results.

@RCT_IT If you haven't discovered the solution to your original post, try this.  Maybe this will help others searching on the same problem like I was too.

#============================
#Unshare/Reshare Group Scipt
#============================
# Platform: Windows, PowerShell 5.1
# Author: Christian Cay
# Description: Interactive script that will perform the following user stories:
#              - As an admin I want to re-share a group automatically so we don't have to do it manually.
#              - As an admin I want to resheare several groups at a time so I can save some time.
# Note: It will perform the action on groups that are shared with more than 2 users.
#
#============================
# Configuration settings
#============================
$user_token = ""

#============================
#Begin code
#============================
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header["authorization"] = "Bearer $user_token"
$request = Invoke-RestMethod -Uri "https://webapi.teamviewer.com/api/v1/groups?shared=false" -Method Get -Headers $header
$shared_groups = $request.groups | Where-Object shared_with -ne $null

Write-Host "Available Groups and their IDs to reshare." 
foreach ($item in $shared_groups) {
    Write-Host $item.name - $item.id
}

$selected_group_id = Read-Host -Prompt "What Group ID would you like to share? Hint: press enter for all groups"

if ($selected_group_id -ne $null) {
    $shared_groups = $shared_groups | Where-Object id -eq $selected_group_id    
}
if ($shared_groups -eq $null) {
    Write-Host "Invalid Group-ID aborting script."
    exit
}

#this loop will unshare on all groups shared.
foreach ($item in $shared_groups) {
    $permissions  = $item | Select-Object -Property shared_with
    $group_id = $item.id
    $group_name = $item.name
    Write-Host "Group $group_name"
    $item.shared_with | Select-Object -Property name, permissions | Format-Table
    foreach ($p in $permissions) {
        if ($item.shared_with.Count -lt 2) {
            Write-Host "Skipping '$group_name'. The script only proceess groups with two or more users."
            break;
        }
        #save this permissions 
        $sharedwith_permissions = $p.shared_with | Select-Object -Property userid, permissions
        $sharewith_users = $sharedwith_permissions | Select-Object -ExpandProperty userid
        $sharedbody_permissions = @{ users=$sharedwith_permissions}
        $sharedbody_users = @{ users=$sharewith_users}
        #users to reshare
        $JSON_share = $sharedbody_permissions | ConvertTo-Json
        #users to unshare
        $JSON_unshare = $sharedbody_users | ConvertTo-Json
        #share function
        Write-Host "Unsharing group '$group_name'"
        $request = Invoke-RestMethod -Uri "https://webapi.teamviewer.com/api/v1/groups/$group_id/unshare_group" -Method Post -Headers $header -ContentType application/json -Body $JSON_unshare
        Write-Host "Sharing groups again, please wait..."
        Start-Sleep -Seconds 5
        #$p.shared_with | Select-Object -Property name, permissions
        $request = Invoke-RestMethod -Uri "https://webapi.teamviewer.com/api/v1/groups/$group_id/share_group" -Method Post -Headers $header -ContentType application/json -Body $JSON_share
    }
    
}
Write-Host "Process completed."

This is a manual script by design.  In order to reshare all groups automaticlly so you can run this on a schedule, replace

$selected_group_id = Read-Host -Prompt "What Group ID would you like to share? Hint: press enter for all groups"

with

$selected_group_id = $null