Skip to content

Commit 9072d7d

Browse files
committed
Adds Get-TeamViewerPolicy utility script
1 parent 74f00f8 commit 9072d7d

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright (c) 2019 TeamViewer GmbH
2+
# See file LICENSE.txt
3+
4+
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
5+
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
6+
. "$here\$sut" -ApiToken "test" -InformationAction SilentlyContinue
7+
8+
Describe 'Get-TeamViewerPolicy' {
9+
Mock Invoke-WebRequest { @{Content = '{"policies": [
10+
{"policy_id": "foo", "name": "bar", "settings": {}},
11+
{"policy_id": "hello", "name": "world", "settings": {}}
12+
]}'} }
13+
14+
It 'Should call the "teamviewerpolicies" Web API endpoint' {
15+
Get-TeamViewerPolicy 'TestToken'
16+
Assert-MockCalled Invoke-WebRequest -Times 1 -Scope It -ParameterFilter {
17+
$Uri -And [System.Uri]$Uri.PathAndQuery -eq '/api/v1/teamviewerpolicies' -And
18+
$Method -And $Method -eq 'Get' -And -Not $Body
19+
}
20+
}
21+
22+
It 'Should set the authorization header' {
23+
Get-TeamViewerPolicy 'TestToken'
24+
Assert-MockCalled Invoke-WebRequest -Times 1 -Scope It -ParameterFilter {
25+
$Headers -And $Headers.ContainsKey('authorization') -And `
26+
$Headers.authorization -eq 'Bearer TestToken'
27+
}
28+
}
29+
30+
It 'Should filter-out policy ID and name' {
31+
$result = (Get-TeamViewerPolicy 'TestToken')
32+
$result | Should -HaveCount 2
33+
$result[0].PSObject.Properties | Should -HaveCount 2
34+
$result[0].policy_id | Should -Be "foo"
35+
$result[0].name | Should -Be "bar"
36+
$result[1].PSObject.Properties | Should -HaveCount 2
37+
$result[1].policy_id | Should -Be "hello"
38+
$result[1].name | Should -Be "world"
39+
}
40+
}
41+
42+
Describe 'ConvertTo-TeamViewerRestError' {
43+
It 'Should convert from JSON' {
44+
$result = ('{"foo": "bar"}' | ConvertTo-TeamViewerRestError)
45+
$result.foo | Should -Be 'bar'
46+
}
47+
48+
It 'Should return input object for invalid JSON' {
49+
$result = ('garbage' | ConvertTo-TeamViewerRestError)
50+
$result | Should -Be 'garbage'
51+
}
52+
}
53+
54+
Describe 'Invoke-TeamViewerRestMethod' {
55+
Mock Invoke-WebRequest { @{Content = '{"foo": "bar"}'} }
56+
57+
It 'Should call Invoke-WebRequest and convert the result from JSON' {
58+
$result = Invoke-TeamViewerRestMethod -Uri 'http://example.test'
59+
$result.foo | Should -Be 'bar'
60+
Assert-MockCalled Invoke-WebRequest `
61+
-ParameterFilter { $Uri -eq 'http://example.test' }-Times 1 -Scope It
62+
}
63+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<#
2+
.SYNOPSIS
3+
Get a list of TeamViewer policies.
4+
5+
.DESCRIPTION
6+
The script fetches TeamViewer policies for the account that corresponds
7+
to the given API token.
8+
It outputs the policy ID and name.
9+
Use this script in conjunction with the `Set-TeamViewerDevicesPolicy`
10+
example script to set the policy of certain devices.
11+
12+
.PARAMETER ApiToken
13+
The TeamViewer API token to use.
14+
The token requires the following access permissions:
15+
- `View TeamViewer policies`
16+
17+
.EXAMPLE
18+
.\Get-TeamViewerPolicy.ps1
19+
20+
.NOTES
21+
Copyright (c) 2019 TeamViewer GmbH
22+
See file LICENSE.txt
23+
Version 1.0.0
24+
#>
25+
26+
param(
27+
[Parameter(Mandatory = $true)]
28+
[string] $ApiToken
29+
)
30+
31+
if (-Not $MyInvocation.BoundParameters.ContainsKey('ErrorAction')) { $script:ErrorActionPreference = 'Stop' }
32+
if (-Not $MyInvocation.BoundParameters.ContainsKey('InformationAction')) { $script:InformationPreference = 'Continue' }
33+
34+
$tvApiVersion = 'v1'
35+
$tvApiBaseUrl = 'https://webapi.teamviewer.com'
36+
37+
function ConvertTo-TeamViewerRestError {
38+
param([parameter(ValueFromPipeline)]$err)
39+
try { return ($err | Out-String | ConvertFrom-Json) }
40+
catch { return $err }
41+
}
42+
43+
function Invoke-TeamViewerRestMethod {
44+
# Using `Invoke-WebRequest` instead of `Invoke-RestMethod`:
45+
# There is a known issue for PUT and DELETE operations to hang on Windows Server 2012.
46+
try { return ((Invoke-WebRequest -UseBasicParsing @args).Content | ConvertFrom-Json) }
47+
catch [System.Net.WebException] {
48+
$stream = $_.Exception.Response.GetResponseStream()
49+
$reader = New-Object System.IO.StreamReader($stream)
50+
$reader.BaseStream.Position = 0
51+
Throw ($reader.ReadToEnd() | ConvertTo-TeamViewerRestError)
52+
}
53+
}
54+
55+
function Get-TeamViewerPolicy($accessToken) {
56+
return Invoke-TeamViewerRestMethod -Uri "$tvApiBaseUrl/api/$tvApiVersion/teamviewerpolicies" `
57+
-Method Get -Headers @{authorization = "Bearer $accessToken"} | `
58+
Select-Object -ExpandProperty 'policies' |
59+
Select-Object -Property 'name','policy_id'
60+
}
61+
62+
if ($MyInvocation.InvocationName -ne '.') {
63+
Get-TeamViewerPolicy $ApiToken
64+
}

0 commit comments

Comments
 (0)