Highlighted
Posted by RCT_IT
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