-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRemove-TeamViewerDuplicateDevicesV2.ps1
More file actions
161 lines (128 loc) · 5.35 KB
/
Remove-TeamViewerDuplicateDevicesV2.ps1
File metadata and controls
161 lines (128 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<#
.SYNOPSIS
Removes TeamViewer duplicate devices (MDv2) based on their alias.
.DESCRIPTION
Removes TeamViewer devices (MDv2) that have a duplicate counterpart in the same company.
The script fetches a list of TeamViewer devices (MDv2) of the TeamViewer company that corresponds to a given API token.
The list will be searched for devices that have the same name (alias). Duplicate devices will be sorted by their last seen timestamp,
and the older ones will be removed.
.PARAMETER ApiToken
The TeamViewer API token to use.
Must be a user access token.
The token requires the following access permissions: company admin, Device Groups: read and write operations
.PARAMETER Force
If set, the script will NOT ask the user for confirmation of the removal.
The default value is `false`, causing the script to ask the user one more time before starting to remove devices.
.EXAMPLE
Remove-TeamViewerDuplicateDevicesV2'
.EXAMPLE
Remove-TeamViewerDuplicateDevicesV2 -WhatIf
.EXAMPLE
Remove-TeamViewerDuplicateDevicesV2 -Force
.NOTES
This script requires the TeamViewerPS module to be installed.
This can be done using the following command:
```
Install-Module TeamViewerPS
```
Copyright (c) 2019-2024 TeamViewer Germany GmbH
See file LICENSE
Version 2.1
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)]
[securestring] $ApiToken,
[switch] $Force = $false
)
if (-Not $MyInvocation.BoundParameters.ContainsKey('ErrorAction')) {
$script:ErrorActionPreference = 'Stop'
}
if (-Not $MyInvocation.BoundParameters.ContainsKey('InformationAction')) {
$script:InformationPreference = 'Continue'
}
function Install-TeamViewerModule {
$module = Get-Module TeamViewerPS
if (!$module) {
Install-Module TeamViewerPS
}
elseif ($module.Version -lt '2.1.0') {
Update-Module TeamViewerPS
}
}
function Remove-TeamViewerDuplicateDevicesV2 {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
param([bool]$force, [bool]$is_verbose)
$devices = @(Get-TeamViewerCompanyManagedDevice -ApiToken $ApiToken)
$name_to_device_map = @{}
ForEach ($device in $devices) {
if ($null -eq $name_to_device_map[$device.Name]) {
$name_to_device_map[$device.Name] = New-Object System.Collections.Generic.List[System.Object]
}
$name_to_device_map[$device.Name].Add($device)
}
$name_to_device_map_sorted = @{}
$name_to_device_map.GetEnumerator() | ForEach-Object {
# Sort duplicate devices by LastSeenAt.
# Older devices should go first.
$offline_duplicate_devices = @(($_.Value | Where-Object { !$_.IsOnline -and $_.LastSeenAt }) | Sort-Object { $_.LastSeenAt })
if ($offline_duplicate_devices.Count -gt 0) {
if ($offline_duplicate_devices.Count -lt $_.Value.Count) {
# There were some online duplicate devices --> remove all of the offline
$name_to_device_map_sorted.Add($_.Key, $offline_duplicate_devices)
}
else {
# No online duplicate devices --> the last one is the "good" device --> skip it
$devices_to_remove = $offline_duplicate_devices | Select-Object -SkipLast 1
if ($null -ne $devices_to_remove) {
$name_to_device_map_sorted.Add($_.Key, $devices_to_remove)
}
}
}
}
if ($is_verbose) {
Write-Information 'All company devices:'
Write-Information ($devices | Format-List | Out-String)
}
if (!$name_to_device_map_sorted.Count) {
Write-Information 'No duplicate devices found. Exiting...'
exit
}
Write-Information 'Found the following devices that have a duplicate alias to other devices in your company, and have been offline for longer:'
Write-Information ($name_to_device_map_sorted | Format-List | Out-String)
if ($name_to_device_map_sorted.Count -gt 0 -And
!$WhatIfPreference -And
!$force -And
!$PSCmdlet.ShouldContinue('Do you really want to remove those devices?', 'Remove managed devices')) {
Write-Information 'Aborting...'
exit
}
$name_to_device_map_sorted.GetEnumerator() | ForEach-Object {
$duplicate_devices_to_be_deleted = $_.Value
ForEach ($device_to_be_deleted in $duplicate_devices_to_be_deleted) {
$status = 'Unchanged'
if ($force -Or $PSCmdlet.ShouldProcess($device_to_be_deleted.TeamViewerId, 'Remove device')) {
try {
Remove-TeamViewerManagedDeviceManagement -ApiToken $ApiToken -Device $device_to_be_deleted
$status = 'Removed'
}
catch {
Write-Warning "Failed to remove device '$($device_to_be_deleted.Name)' with TeamViewerID: '$($device_to_be_deleted.TeamViewerId)'"
$status = 'Failed'
}
}
Write-Output ([pscustomobject]@{
Name = $device_to_be_deleted.Name
ManagementId = $device_to_be_deleted.Id
LastSeen = $device_to_be_deleted.LastSeenAt
TeamViewerID = $device_to_be_deleted.TeamViewerId
Status = $status
})
}
}
}
if ($MyInvocation.InvocationName -ne '.') {
Install-TeamViewerModule
$is_verbose = $PSBoundParameters.ContainsKey('Verbose')
Remove-TeamViewerDuplicateDevicesV2 -force $Force -is_verbose $is_verbose
}