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.  

TeamViewer-Script1.JPG

 TeamViewer-Script2.JPG

$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

  • Teja
    Teja Posts: 1

    Hi Digon,

    Did you ever get this to work?

    Regards,

    Teja

  • Helllo there, 

    i guess here you've used Powershell false.

    If ($asg -eq "False") .. should be If ( $asg -eq $false )



     

  • techaanderson
    techaanderson Posts: 1

    Hey,

    Were you able to get this working? I continually have issues with devices not inheriting policies from the group when installed.

    Thanks!

  • KLTVUser
    KLTVUser Posts: 1

    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?

  • dbassetto
    dbassetto Posts: 1

    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?