|
1 | 1 | <# |
2 | 2 | .SYNOPSIS |
3 | | - Removes TeamViewer devices that didn't appear online for a given time. |
| 3 | + Removes TeamViewer devices (MDv2) that didn't appear online for a given time. |
4 | 4 |
|
5 | 5 | .DESCRIPTION |
6 | | - The script fetches a list of TeamViewer devices of the TeamViewer company |
7 | | - that corresponds to a given API token. The list will be filtered by |
8 | | - devices being offline for a certain amount of time. These devices will |
9 | | - be removed. |
| 6 | + The script fetches a list of TeamViewer devices (MDv2) of the TeamViewer company that corresponds to a given API token. |
| 7 | + The list will be filtered by devices being offline for a certain amount of time. These devices will be removed. |
10 | 8 | The expiration can either be specified by a specific date or by interval. |
11 | 9 |
|
12 | 10 | .PARAMETER ApiToken |
13 | 11 | The TeamViewer API token to use. |
14 | 12 | Must be a user access token. |
15 | | - The token requires the following access permissions: |
16 | | - - `Device Groups > read operations, modifying operations` |
| 13 | + The token requires the following access permissions: `Device Groups > read operations, modifying operations` |
17 | 14 |
|
18 | 15 | .PARAMETER ExpiryDate |
19 | | - A specific expiry date. All devices that haven't been online since that |
20 | | - date are considered being removed. |
| 16 | + A specific expiry date. All devices that haven't been online since that date are considered being removed. |
21 | 17 |
|
22 | 18 | .PARAMETER ExpiryInterval |
23 | 19 | Switch that enables interval-based calculation of the expiration date. |
24 | | - Should be used in combination with the `Days`, `Hours`, `Minutes` and/or |
25 | | - `Seconds` parameter. |
| 20 | + Should be used in combination with the `Days`, `Hours`, `Minutes` and/or `Seconds` parameter. |
26 | 21 |
|
27 | 22 | .PARAMETER Days |
28 | 23 | Days of the expiration interval. |
|
41 | 36 | Must be used in combination with the `ExpiryInterval` parameter. |
42 | 37 |
|
43 | 38 | .PARAMETER Force |
44 | | - If set, the script will NOT ask the user for confirmation of the |
45 | | - removal. This parameter only has effect in combination with the |
46 | | - `Remove` parameter. |
47 | | - The default value is `false`, causing the script to ask the user |
48 | | - one more time before starting to remove devices. |
| 39 | + If set, the script will NOT ask the user for confirmation of the removal. |
| 40 | + This parameter only has effect in combination with the `Remove` parameter. |
| 41 | + The default value is `false`, causing the script to ask the user one more time before starting to remove devices. |
49 | 42 |
|
50 | 43 | .EXAMPLE |
51 | 44 | .\Remove-TeamViewerOutdatedDevice -ExpiryDate '2018-12-17T17:00:00' |
|
67 | 60 | Install-Module TeamViewerPS |
68 | 61 | ``` |
69 | 62 |
|
70 | | - Copyright (c) 2019-2021 TeamViewer GmbH |
71 | | - See file LICENSE.txt |
72 | | - Version 2.0 |
| 63 | + Copyright (c) 2019-2023 TeamViewer Germany GmbH |
| 64 | + See file LICENSE |
| 65 | + Version 2.1 |
73 | 66 | #> |
74 | 67 |
|
75 | | -[CmdletBinding(DefaultParameterSetName = "ExactDate", SupportsShouldProcess = $true)] |
| 68 | +[CmdletBinding(DefaultParameterSetName = 'ExactDate', SupportsShouldProcess = $true)] |
76 | 69 | param( |
77 | | - [Parameter(Mandatory = $true)] |
78 | | - [securestring] $ApiToken, |
| 70 | + [Parameter(Mandatory = $true)] |
| 71 | + [securestring] $ApiToken, |
79 | 72 |
|
80 | | - [Parameter(ParameterSetName = "ExactDate", Mandatory = $true)] |
81 | | - [DateTime] $ExpiryDate, |
| 73 | + [Parameter(ParameterSetName = 'ExactDate', Mandatory = $true)] |
| 74 | + [DateTime] $ExpiryDate, |
82 | 75 |
|
83 | | - [Parameter(ParameterSetName = "DateInterval", Mandatory = $true)] |
84 | | - [switch] $ExpiryInterval, |
| 76 | + [Parameter(ParameterSetName = 'DateInterval', Mandatory = $true)] |
| 77 | + [switch] $ExpiryInterval, |
85 | 78 |
|
86 | | - [Parameter(ParameterSetName = "DateInterval", Mandatory = $false)] |
87 | | - [int] $Days, |
| 79 | + [Parameter(ParameterSetName = 'DateInterval', Mandatory = $false)] |
| 80 | + [int] $Days, |
88 | 81 |
|
89 | | - [Parameter(ParameterSetName = "DateInterval", Mandatory = $false)] |
90 | | - [int] $Hours, |
| 82 | + [Parameter(ParameterSetName = 'DateInterval', Mandatory = $false)] |
| 83 | + [int] $Hours, |
91 | 84 |
|
92 | | - [Parameter(ParameterSetName = "DateInterval", Mandatory = $false)] |
93 | | - [int] $Minutes, |
| 85 | + [Parameter(ParameterSetName = 'DateInterval', Mandatory = $false)] |
| 86 | + [int] $Minutes, |
94 | 87 |
|
95 | | - [Parameter(ParameterSetName = "DateInterval", Mandatory = $false)] |
96 | | - [int] $Seconds, |
| 88 | + [Parameter(ParameterSetName = 'DateInterval', Mandatory = $false)] |
| 89 | + [int] $Seconds, |
97 | 90 |
|
98 | | - [Switch] $Force = $false |
| 91 | + [Switch] $Force = $false |
99 | 92 | ) |
100 | 93 |
|
101 | | -if (-Not $MyInvocation.BoundParameters.ContainsKey('ErrorAction')) { $script:ErrorActionPreference = 'Stop' } |
102 | | -if (-Not $MyInvocation.BoundParameters.ContainsKey('InformationAction')) { $script:InformationPreference = 'Continue' } |
| 94 | +if (-Not $MyInvocation.BoundParameters.ContainsKey('ErrorAction')) { |
| 95 | + $script:ErrorActionPreference = 'Stop' |
| 96 | +} |
| 97 | +if (-Not $MyInvocation.BoundParameters.ContainsKey('InformationAction')) { |
| 98 | + $script:InformationPreference = 'Continue' |
| 99 | +} |
103 | 100 |
|
104 | 101 | function Install-TeamViewerModule { |
105 | 102 | $module = Get-Module TeamViewerPS |
106 | | - if (!$module) { |
107 | | - Install-Module TeamViewerPS |
108 | | - } |
109 | | - elseif ($module.Version -lt '1.5.0') { |
110 | | - Update-Module TeamViewerPS |
111 | | - } |
112 | | - } |
| 103 | + |
| 104 | + if (!$module) { |
| 105 | + Install-Module TeamViewerPS |
| 106 | + } |
| 107 | + elseif ($module.Version -lt '1.5.0') { |
| 108 | + Update-Module TeamViewerPS |
| 109 | + } |
| 110 | +} |
113 | 111 |
|
114 | 112 | function Remove-TeamViewerOutdatedDeviceV2 { |
115 | | - [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')] |
116 | | - param($expiryDate, [bool]$force) |
117 | | - |
118 | | - $devices = @((Get-TeamViewerManagedDevice -ApiToken $ApiToken) | |
119 | | - Where-Object { !$_.IsOnline -And $_.LastSeenAt -And $_.LastSeenAt -le $expiryDate}) |
120 | | - |
121 | | - Write-Information "Found $($devices.Count) devices that have been offline since $expiryDate" |
122 | | - |
123 | | - if ($devices.Count -gt 0 -And -Not $WhatIfPreference -And -Not $force -And |
124 | | - -Not $PSCmdlet.ShouldContinue("Do you really want to remove those devices?", $devices)) { |
125 | | - Write-Information "Aborting..." |
126 | | - exit |
127 | | - } |
128 | | - |
129 | | - ForEach ($device in $devices) { |
130 | | - $status = 'Unchanged' |
131 | | - if ($force -Or $PSCmdlet.ShouldProcess($device.Name)) { |
132 | | - try { |
133 | | - Remove-TeamViewerManagedDeviceManagement -ApiToken $ApiToken -Device $device |
134 | | - $status = 'Removed' |
135 | | - } |
136 | | - catch { |
137 | | - Write-Warning "Failed to remove device '$($device.Name)': $_" |
138 | | - $status = 'Failed' |
139 | | - } |
140 | | - } |
141 | | - Write-Output ([pscustomobject]@{ |
142 | | - Name = $device.Name |
143 | | - DeviceId = $device.Id |
144 | | - LastSeen = $device.LastSeenAt |
145 | | - Status = $status |
146 | | - }) |
147 | | - } |
| 113 | + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')] |
| 114 | + param($expiryDate, [bool]$force) |
| 115 | + |
| 116 | + $devices = @((Get-TeamViewerManagedDevice -ApiToken $ApiToken) | Where-Object { !$_.IsOnline -And $_.LastSeenAt -And $_.LastSeenAt -le $expiryDate }) |
| 117 | + |
| 118 | + Write-Information "Found $($devices.Count) devices that have been offline since $expiryDate" |
| 119 | + |
| 120 | + if ($devices.Count -gt 0 -And -Not $WhatIfPreference -And -Not $force -And -Not $PSCmdlet.ShouldContinue('Do you really want to remove those devices?', $devices)) { |
| 121 | + Write-Information 'Aborting...' |
| 122 | + |
| 123 | + exit |
| 124 | + } |
| 125 | + |
| 126 | + ForEach ($device in $devices) { |
| 127 | + $status = 'Unchanged' |
| 128 | + |
| 129 | + if ($force -Or $PSCmdlet.ShouldProcess($device.Name)) { |
| 130 | + try { |
| 131 | + Remove-TeamViewerManagedDeviceManagement -ApiToken $ApiToken -Device $device |
| 132 | + |
| 133 | + $status = 'Removed' |
| 134 | + } |
| 135 | + catch { |
| 136 | + Write-Warning "Failed to remove device '$($device.Name)': $_" |
| 137 | + |
| 138 | + $status = 'Failed' |
| 139 | + } |
| 140 | + } |
| 141 | + Write-Output ([pscustomobject]@{ |
| 142 | + Name = $device.Name |
| 143 | + DeviceId = $device.Id |
| 144 | + LastSeen = $device.LastSeenAt |
| 145 | + Status = $status |
| 146 | + }) |
| 147 | + } |
148 | 148 | } |
149 | 149 |
|
150 | 150 | if ($MyInvocation.InvocationName -ne '.') { |
151 | | - Install-TeamViewerModule |
152 | | - $now = (Get-Date) |
153 | | - if ($ExpiryInterval) { |
154 | | - $ExpiryDate = $now.AddDays(-1 * $Days).AddHours(-1 * $Hours).AddMinutes(-1 * $Minutes).AddSeconds(-1 * $Seconds) |
155 | | - } |
156 | | - if ($ExpiryDate -ge $now) { |
157 | | - Throw "Invalid expiry date specified: $ExpiryDate" |
158 | | - } |
159 | | - Remove-TeamViewerOutdatedDeviceV2 -expiryDate $ExpiryDate -force $Force |
| 151 | + Install-TeamViewerModule |
| 152 | + |
| 153 | + $now = (Get-Date) |
| 154 | + |
| 155 | + if ($ExpiryInterval) { |
| 156 | + $ExpiryDate = $now.AddDays(-1 * $Days).AddHours(-1 * $Hours).AddMinutes(-1 * $Minutes).AddSeconds(-1 * $Seconds) |
| 157 | + } |
| 158 | + |
| 159 | + if ($ExpiryDate -ge $now) { |
| 160 | + Throw "Invalid expiry date specified: $ExpiryDate" |
| 161 | + } |
| 162 | + |
| 163 | + Remove-TeamViewerOutdatedDeviceV2 -expiryDate $ExpiryDate -force $Force |
160 | 164 | } |
0 commit comments