PowerShell Script to Assign Policy to All Systems
I'm looking to use a PowerShell script, to assign the 'Inherit' from group setting for policies across all our systems. We have 9,500 workstations, spread between 20 groups, so doing it manually takes forever!
I'm using the below code, in a PowerShell script (teamviewer.ps1) and I have generated an API token. However, it seems to run, only takes about 10 seconds, but nothing happens. No changes appear in the management console, and some systems still show as having no policy applied.
$token = Read-Host -Prompt "Paste your account token code here"
$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
$x=1
ForEach($item in $request.devices)
{
$asg = $item.assigned_to
$dev = $item.device_id
If($asg -eq "False")
{
$body = (@{
policy_id = 'inherit'
}) | ConvertTo-Json
Invoke-RestMethod -Uri "https://webapi.teamviewer.com/api/v1/devices/$dev" -Method Put -Headers $header -ContentType application/json -Body $body
$x +=1
}
}
Write-Host "The following devices were not applied the policy change, reason - Not assigned to this account"
$response = Invoke-RestMethod -Uri "https://webapi.teamviewer.com/api/v1/devices" -Method Get -Headers $header
$i=1
Foreach($it in $response.devices)
{
$agn = $it.assigned_to
If(!$agn -eq "False")
{
Write-Host $i ")", $it.alias
$i +=1
}
}
Comments
-
Hi Digon,
Did you ever get this to work?
Regards,
Teja
0 -
Helllo there,
i guess here you've used Powershell false.
If ($asg -eq "False") .. should be If ( $asg -eq $false )
0 -
Hey,
Were you able to get this working? I continually have issues with devices not inheriting policies from the group when installed.
Thanks!
0 -
Hi all,
This can be solved very easily using our TeamViewerPS Powershell module:
# install the module if not already done: Install-Module TeamViewerPS # Store the API token in the current session Connect-TeamViewerApi # Get all devices, filter by "assigned" + "policy is not 'inherit'", change to 'inherit' Get-TeamViewerDevice | ` Where-Object { $_.IsAssignedToCurrentAccount -And $_.PolicyId -ne 'inherit' } | ` Set-TeamViewerDevice -Policy 'inherit' -Verbose
Find more information about the Powershell module here:
Best regards,
Daniel
5 -
Both power script and PowerShell Module works great. However, I run into another issue: it can't apply the policy because "device is not assigned to account". Do you have the powershell commands to accomplish this?
0 -
Hi all,
we are facing the same issue, we need to assign device to out account in several device.
we can do one by one from the online console but we are looking for a script to automate.
Any one?
0 -
I am also facing this same issue
0