Skip to content

Commit b8c7be2

Browse files
cleaned up the example scripts for the TeamViewer Web API
1 parent cb12c5f commit b8c7be2

3 files changed

Lines changed: 29 additions & 29 deletions

File tree

Invoke-TeamViewerGroupPerUserSync/Invoke-TeamViewerGroupPerUserSync.ps1

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function ConvertTo-GroupName($user) {
106106

107107
function Install-TeamViewerModule { if (!(Get-Module TeamViewerPS)) { Install-Module TeamViewerPS } }
108108

109-
function Write-Log {
109+
function Write-SyncLog {
110110
param(
111111
[Parameter(ValueFromPipeline)] $message,
112112
[Parameter(Mandatory = $false)][ValidateSet('Info', 'Error', 'Warning')][string] $Severity = "Info",
@@ -153,56 +153,56 @@ function Invoke-TeamViewerGroupPerUserSync {
153153
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
154154
param($sourceGroupName, [bool]$ignoreSourceGroup, $mappingData)
155155

156-
Write-Log "Script started"
157-
Write-Log "Environment: OS $([environment]::OSVersion.VersionString), PS $($PSVersionTable.PSVersion)"
158-
Write-Log "Read $($mappingData.Count) mapping entries from CSV."
156+
Write-SyncLog "Script started"
157+
Write-SyncLog "Environment: OS $([environment]::OSVersion.VersionString), PS $($PSVersionTable.PSVersion)"
158+
Write-SyncLog "Read $($mappingData.Count) mapping entries from CSV."
159159

160-
Write-Log "Checking connection to TeamViewer API."
160+
Write-SyncLog "Checking connection to TeamViewer API."
161161
if (!(Invoke-TeamViewerPing -ApiToken $ApiToken)) {
162-
Write-Log -Fatal "Failed to contact TeamViewer API. Invalid token or connection problem. Aborting."
162+
Write-SyncLog -Fatal "Failed to contact TeamViewer API. Invalid token or connection problem. Aborting."
163163
}
164164

165165
$statistics = @{ Updated = 0; Failed = 0; Unchanged = 0; }
166166
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
167167

168168
# Fetch current users/devices/groups:
169169

170-
Write-Log "Fetching TeamViewer company users."
170+
Write-SyncLog "Fetching TeamViewer company users."
171171
$users = @(Get-TeamViewerUser -ApiToken $ApiToken)
172172
$usersByEmail = @{ }
173173
($users | ForEach-Object { $usersByEmail[$_.Email] = $_ } | Out-Null)
174-
Write-Log "Retrieved $($users.Count) TeamViewer company users."
174+
Write-SyncLog "Retrieved $($users.Count) TeamViewer company users."
175175

176-
Write-Log "Fetching TeamViewer groups of administrative user."
176+
Write-SyncLog "Fetching TeamViewer groups of administrative user."
177177
$groups = @(Get-TeamViewerGroup -ApiToken $ApiToken)
178178
$groupsByName = @{ }
179179
($groups | ForEach-Object { $groupsByName[$_.Name] = $_ } | Out-Null)
180-
Write-Log "Retrieved $($groups.Count) TeamViewer groups."
180+
Write-SyncLog "Retrieved $($groups.Count) TeamViewer groups."
181181

182-
Write-Log "Fetching TeamViewer devices list of administrative user."
182+
Write-SyncLog "Fetching TeamViewer devices list of administrative user."
183183
$devices = @(Get-TeamViewerDevice -ApiToken $ApiToken)
184184
$devicesByAlias = @{ }
185185
$devicesByRemoteControlId = @{}
186186
($devices | Where-Object { $_.Name } | ForEach-Object { $devicesByAlias[$_.Name] = $_ } | Out-Null)
187187
($devices | Where-Object { $_.TeamViewerId } | ForEach-Object { $devicesByRemoteControlId[$_.TeamViewerId] = $_ } | Out-Null)
188-
Write-Log "Retrieved $($devices.Count) TeamViewer devices."
188+
Write-SyncLog "Retrieved $($devices.Count) TeamViewer devices."
189189

190190
# Check for source group
191191
$sourceGroup = $groupsByName[$sourceGroupName]
192192
if (!$ignoreSourceGroup -And !$sourceGroup) {
193-
Write-Log -Fatal "Source group with name '$($sourceGroupName)' not found. Aborting."
193+
Write-SyncLog -Fatal "Source group with name '$($sourceGroupName)' not found. Aborting."
194194
}
195195

196-
Write-Log "Starting processing of $($mappingData.Count) given mapping entries."
196+
Write-SyncLog "Starting processing of $($mappingData.Count) given mapping entries."
197197
$count = 0
198198
$totalCount = $mappingData.Count
199199
foreach ($entry in $mappingData) {
200200
$count++
201-
Write-Log "Processing entry [$count/$totalCount] - email: '$($entry.email)', device: '$($entry.device)', teamviewerid: '$($entry.teamviewerid)'."
201+
Write-SyncLog "Processing entry [$count/$totalCount] - email: '$($entry.email)', device: '$($entry.device)', teamviewerid: '$($entry.teamviewerid)'."
202202

203203
$user = $usersByEmail[$entry.email]
204204
if (!$user) {
205-
Write-Log -Severity Error "Failed to find user with email '$($entry.email)'. Skipping."
205+
Write-SyncLog -Severity Error "Failed to find user with email '$($entry.email)'. Skipping."
206206
$statistics.Failed++
207207
continue
208208
}
@@ -213,7 +213,7 @@ function Invoke-TeamViewerGroupPerUserSync {
213213
$groupName = (ConvertTo-GroupName $user)
214214
$group = $groupsByName[$groupName]
215215
if (!$group) {
216-
Write-Log "Creating group '$groupName'."
216+
Write-SyncLog "Creating group '$groupName'."
217217
try {
218218
if ($PSCmdlet.ShouldProcess("Create group '$groupName'.")) {
219219
$group = (New-TeamViewerGroup -ApiToken $ApiToken -Name $groupName)
@@ -223,7 +223,7 @@ function Invoke-TeamViewerGroupPerUserSync {
223223
$didUpdate = $true
224224
}
225225
catch {
226-
Write-Log -Severity Error "Failed to create group '$groupName'. Error: $_"
226+
Write-SyncLog -Severity Error "Failed to create group '$groupName'. Error: $_"
227227
$statistics.Failed++
228228
continue
229229
}
@@ -235,21 +235,21 @@ function Invoke-TeamViewerGroupPerUserSync {
235235
# Second, try to resolve device by alias
236236
$device = $devicesByAlias[$entry.device]
237237
if (!$device) {
238-
Write-Log -Severity Error "Device '$(if ($entry.device) { $entry.device } else { $entry.teamviewerid })' not found. Skipping"
238+
Write-SyncLog -Severity Error "Device '$(if ($entry.device) { $entry.device } else { $entry.teamviewerid })' not found. Skipping"
239239
$statistics.Failed++
240240
continue
241241
}
242242
}
243243
if (!$ignoreSourceGroup -And $device.GroupId -ne $sourceGroup.id) {
244-
Write-Log -Severity Warning "Device '$($device.Name)' not in source group. Skipping."
244+
Write-SyncLog -Severity Warning "Device '$($device.Name)' not in source group. Skipping."
245245
$statistics.Unchanged++
246246
continue
247247
}
248248

249249
# Move device to target group, if not yet done.
250250
if (!$device.GroupId -or $device.GroupId -ne $group.Id) {
251251
try {
252-
Write-Log "Moving device '$($device.Name)' to group '$groupName'."
252+
Write-SyncLog "Moving device '$($device.Name)' to group '$groupName'."
253253
if ($PSCmdlet.ShouldProcess("Move device '$($device.Name)' to group '$groupName'.")) {
254254
Set-TeamViewerDevice `
255255
-ApiToken $ApiToken `
@@ -259,20 +259,20 @@ function Invoke-TeamViewerGroupPerUserSync {
259259
$didUpdate = $true
260260
}
261261
catch {
262-
Write-Log -Severity Error "Failed to move device '$($device.Name)' to group '$groupName'. Error: $_"
262+
Write-SyncLog -Severity Error "Failed to move device '$($device.Name)' to group '$groupName'. Error: $_"
263263
$statistics.Failed++
264264
continue
265265
}
266266
}
267267
else {
268-
Write-Log "Device '$($device.Name)' is already in group '$groupName'. Ignoring."
268+
Write-SyncLog "Device '$($device.Name)' is already in group '$groupName'. Ignoring."
269269
}
270270

271271
# Share target group with user, if not yet done.
272272
$sharedUserIds = (@($group.SharedWith) | Select-Object -ExpandProperty UserId)
273273
if ($user.id -notin $sharedUserIds) {
274274
try {
275-
Write-Log "Sharing group '$groupName' with user '$($user.email)'."
275+
Write-SyncLog "Sharing group '$groupName' with user '$($user.email)'."
276276
if ($PSCmdlet.ShouldProcess("Sharing group '$groupName' with user '$($user.email)'.")) {
277277
Publish-TeamViewerGroup `
278278
-ApiToken $ApiToken `
@@ -283,20 +283,20 @@ function Invoke-TeamViewerGroupPerUserSync {
283283
$didUpdate = $true
284284
}
285285
catch {
286-
Write-Log -Severity Error "Failed to share group '$groupName' with user '$($user.email)'. Error: $_"
286+
Write-SyncLog -Severity Error "Failed to share group '$groupName' with user '$($user.email)'. Error: $_"
287287
$statistics.Failed++
288288
continue
289289
}
290290
}
291291
else {
292-
Write-Log "Group '$groupName' is already shared with user '$($user.email)'. Ignoring."
292+
Write-SyncLog "Group '$groupName' is already shared with user '$($user.email)'. Ignoring."
293293
}
294294

295295
if ($didUpdate) { $statistics.Updated++ }
296296
else { $statistics.Unchanged++ }
297297
}
298298

299-
Write-Log "Script finished"
299+
Write-SyncLog "Script finished"
300300
$stopwatch.Stop()
301301

302302
Write-Output @{

Remove-TeamViewerOutdatedDeviceV2/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The expiration can either be specified by a specific date or by interval.
88

99
## Prerequisites
1010

11-
This script requires the `TeamViewerPS` powershell module to be installed in at least Version 1.5.0.
11+
This script requires the `TeamViewerPS` powershell module to be installed in at least Version 2.4.0.
1212

1313
```powershell
1414
Install-Module TeamViewerPS

Remove-TeamViewerOutdatedDeviceV2/Remove-TeamViewerOutdatedDeviceV2.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function Install-TeamViewerModule {
104104
if (!$module) {
105105
Install-Module TeamViewerPS
106106
}
107-
elseif ($module.Version -lt '1.5.0') {
107+
elseif ($module.Version -lt '2.4.0') {
108108
Update-Module TeamViewerPS
109109
}
110110
}

0 commit comments

Comments
 (0)