-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRemove-TeamViewerOutdatedDevice.ps1
More file actions
152 lines (120 loc) · 5.1 KB
/
Remove-TeamViewerOutdatedDevice.ps1
File metadata and controls
152 lines (120 loc) · 5.1 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
<#
.SYNOPSIS
Removes TeamViewer devices that didn't appear online for a given time.
.DESCRIPTION
The script fetches a list of TeamViewer devices of the TeamViewer company
that corresponds to a given API token. The list will be filtered by
devices being offline for a certain amount of time. These devices will
be removed.
The expiration can either be specified by a specific date or by interval.
.PARAMETER ApiToken
The TeamViewer API token to use.
Must be a user access token.
The token requires the following access permissions:
- `Computer & Contacts > View entries, add entries, edit entries, remove entries`
.PARAMETER ExpiryDate
A specific expiry date. All devices that haven't been online since that
date are considered being removed.
.PARAMETER ExpiryInterval
Switch that enables interval-based calculation of the expiration date.
Should be used in combination with the `Days`, `Hours`, `Minutes` and/or
`Seconds` parameter.
.PARAMETER Days
Days of the expiration interval.
Must be used in combination with the `ExpiryInterval` parameter.
.PARAMETER Hours
Hours of the expiration interval.
Must be used in combination with the `ExpiryInterval` parameter.
.PARAMETER Minutes
Minutes of the expiration interval.
Must be used in combination with the `ExpiryInterval` parameter.
.PARAMETER Seconds
Seconds of the expiration interval.
Must be used in combination with the `ExpiryInterval` parameter.
.PARAMETER Force
If set, the script will NOT ask the user for confirmation of the
removal. This parameter only has effect in combination with the
`Remove` parameter.
The default value is `false`, causing the script to ask the user
one more time before starting to remove devices.
.EXAMPLE
.\Remove-TeamViewerOutdatedDevice -ExpiryDate '2018-12-17T17:00:00'
.EXAMPLE
.\Remove-TeamViewerOutdatedDevice -ExpiryDate 2018-12-31 -WhatIf
.EXAMPLE
.\Remove-TeamViewerOutdatedDevice -ExpiryInterval -Days 10
.EXAMPLE
.\Remove-TeamViewerOutdatedDevice -ExpiryInterval -Hours 12 -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-2021 TeamViewer GmbH
See file LICENSE.txt
Version 2.0
#>
[CmdletBinding(DefaultParameterSetName = "ExactDate", SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)]
[securestring] $ApiToken,
[Parameter(ParameterSetName = "ExactDate", Mandatory = $true)]
[DateTime] $ExpiryDate,
[Parameter(ParameterSetName = "DateInterval", Mandatory = $true)]
[switch] $ExpiryInterval,
[Parameter(ParameterSetName = "DateInterval", Mandatory = $false)]
[int] $Days,
[Parameter(ParameterSetName = "DateInterval", Mandatory = $false)]
[int] $Hours,
[Parameter(ParameterSetName = "DateInterval", Mandatory = $false)]
[int] $Minutes,
[Parameter(ParameterSetName = "DateInterval", Mandatory = $false)]
[int] $Seconds,
[Switch] $Force = $false
)
if (-Not $MyInvocation.BoundParameters.ContainsKey('ErrorAction')) { $script:ErrorActionPreference = 'Stop' }
if (-Not $MyInvocation.BoundParameters.ContainsKey('InformationAction')) { $script:InformationPreference = 'Continue' }
function Install-TeamViewerModule { if (!(Get-Module TeamViewerPS)) { Install-Module TeamViewerPS } }
function Remove-TeamViewerOutdatedDevice {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
param($expiryDate, [bool]$force)
$devices = @((Get-TeamViewerDevice -ApiToken $ApiToken -FilterOnlineState 'Offline') | `
Where-Object { $_.LastSeenAt -And $_.LastSeenAt -le $expiryDate })
Write-Information "Found $($devices.Count) devices that have been offline since $expiryDate"
if ($devices.Count -gt 0 -And -Not $WhatIfPreference -And -Not $force -And
-Not $PSCmdlet.ShouldContinue("Do you really want to remove those devices?", $devices)) {
Write-Information "Aborting..."
exit
}
ForEach ($device in $devices) {
$status = 'Unchanged'
if ($force -Or $PSCmdlet.ShouldProcess($device.Name)) {
try {
Remove-TeamViewerDevice -ApiToken $ApiToken -Device $device
$status = 'Removed'
}
catch {
Write-Warning "Failed to remove device '$($device.Name)': $_"
$status = 'Failed'
}
}
Write-Output ([pscustomobject]@{
Name = $device.Name
DeviceId = $device.Id
LastSeen = $device.LastSeenAt
Status = $status
})
}
}
if ($MyInvocation.InvocationName -ne '.') {
Install-TeamViewerModule
$now = (Get-Date)
if ($ExpiryInterval) {
$ExpiryDate = $now.AddDays(-1 * $Days).AddHours(-1 * $Hours).AddMinutes(-1 * $Minutes).AddSeconds(-1 * $Seconds)
}
if ($ExpiryDate -ge $now) {
Throw "Invalid expiry date specified: $ExpiryDate"
}
Remove-TeamViewerOutdatedDevice -expiryDate $ExpiryDate -force $Force
}