Skip to content

Commit b4ee7f4

Browse files
Merge pull request #23 from teamviewer/Remove-TeamViewerOutdatedDeviceV2_FormatAndScriptAnalyzer
RemoveTVOutdatedDevicesV2: format & ScriptAnalyzer
2 parents 754896d + e50e632 commit b4ee7f4

File tree

3 files changed

+100
-97
lines changed

3 files changed

+100
-97
lines changed

Remove-TeamViewerOutdatedDeviceV2/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# Remove-TeamViewerOutdatedDeviceV2
22

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.
44

5-
The script fetches a list of TeamViewer devices of the TeamViewer company
6-
that corresponds to a given API token. The list will be filtered by
7-
devices being offline for a certain amount of time. These devices will
8-
be removed.
5+
The script fetches a list of TeamViewer devices (MDv2) of the TeamViewer company that corresponds to a given API token.
6+
The list will be filtered by devices being offline for a certain amount of time. These devices will be removed.
97
The expiration can either be specified by a specific date or by interval.
108

119
## Prerequisites

Remove-TeamViewerOutdatedDeviceV2/Remove-TeamViewerOutdatedDeviceV2.Tests.ps1

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
# Copyright (c) 2019-2021 TeamViewer GmbH
2-
# See file LICENSE.txt
1+
# Copyright (c) 2019-2023 TeamViewer Germany GmbH
2+
# See file LICENSE
33

44
BeforeAll {
55
$testApiToken = [securestring]@{}
6-
. "$PSScriptRoot\Remove-TeamViewerOutdatedDeviceV2.ps1" `
7-
-ApiToken $testApiToken `
8-
-ExpiryDate (Get-Date) `
9-
-InformationAction SilentlyContinue
6+
7+
. "$PSScriptRoot\Remove-TeamViewerOutdatedDeviceV2.ps1" -ApiToken $testApiToken -ExpiryDate (Get-Date) -InformationAction SilentlyContinue
108

119
Mock Get-TeamViewerManagedDevice { @(
1210
[pscustomobject]@{ Id = 'device1'; Name = 'device1'; LastSeenAt = [datetime]'2018-12-16' },
1311
[pscustomobject]@{ Id = 'device2'; Name = 'device2'; LastSeenAt = [datetime]'2018-12-17' },
1412
[pscustomobject]@{ Id = 'device3'; Name = 'device3'; LastSeenAt = [datetime]'2018-12-18' },
1513
[pscustomobject]@{ Id = 'device4'; Name = 'device4'; LastSeenAt = [datetime]'2018-12-19' }
1614
) }
15+
1716
Mock Remove-TeamViewerManagedDeviceManagement -RemoveParameterValidation 'Device' {}
1817
}
1918

@@ -28,6 +27,7 @@ Describe 'Remove-TeamViewerOutdatedDeviceV2' {
2827
$result[1].Status | Should -Be 'Unchanged'
2928
$result[2].DeviceId | Should -Be 'device3'
3029
$result[2].Status | Should -Be 'Unchanged'
30+
3131
Assert-MockCalled Get-TeamViewerManagedDevice -Times 1 -Scope It
3232
Assert-MockCalled Remove-TeamViewerManagedDeviceManagement -Times 0 -Scope It
3333
}
@@ -41,6 +41,7 @@ Describe 'Remove-TeamViewerOutdatedDeviceV2' {
4141
$result[1].Status | Should -Be 'Removed'
4242
$result[2].DeviceId | Should -Be 'device3'
4343
$result[2].Status | Should -Be 'Removed'
44+
4445
Assert-MockCalled Get-TeamViewerManagedDevice -Times 1 -Scope It
4546
Assert-MockCalled Remove-TeamViewerManagedDeviceManagement -Times 3 -Scope It
4647
}
Lines changed: 90 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
<#
22
.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.
44
55
.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.
108
The expiration can either be specified by a specific date or by interval.
119
1210
.PARAMETER ApiToken
1311
The TeamViewer API token to use.
1412
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`
1714
1815
.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.
2117
2218
.PARAMETER ExpiryInterval
2319
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.
2621
2722
.PARAMETER Days
2823
Days of the expiration interval.
@@ -41,11 +36,9 @@
4136
Must be used in combination with the `ExpiryInterval` parameter.
4237
4338
.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.
4942
5043
.EXAMPLE
5144
.\Remove-TeamViewerOutdatedDevice -ExpiryDate '2018-12-17T17:00:00'
@@ -67,94 +60,105 @@
6760
Install-Module TeamViewerPS
6861
```
6962
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
7366
#>
7467

75-
[CmdletBinding(DefaultParameterSetName = "ExactDate", SupportsShouldProcess = $true)]
68+
[CmdletBinding(DefaultParameterSetName = 'ExactDate', SupportsShouldProcess = $true)]
7669
param(
77-
[Parameter(Mandatory = $true)]
78-
[securestring] $ApiToken,
70+
[Parameter(Mandatory = $true)]
71+
[securestring] $ApiToken,
7972

80-
[Parameter(ParameterSetName = "ExactDate", Mandatory = $true)]
81-
[DateTime] $ExpiryDate,
73+
[Parameter(ParameterSetName = 'ExactDate', Mandatory = $true)]
74+
[DateTime] $ExpiryDate,
8275

83-
[Parameter(ParameterSetName = "DateInterval", Mandatory = $true)]
84-
[switch] $ExpiryInterval,
76+
[Parameter(ParameterSetName = 'DateInterval', Mandatory = $true)]
77+
[switch] $ExpiryInterval,
8578

86-
[Parameter(ParameterSetName = "DateInterval", Mandatory = $false)]
87-
[int] $Days,
79+
[Parameter(ParameterSetName = 'DateInterval', Mandatory = $false)]
80+
[int] $Days,
8881

89-
[Parameter(ParameterSetName = "DateInterval", Mandatory = $false)]
90-
[int] $Hours,
82+
[Parameter(ParameterSetName = 'DateInterval', Mandatory = $false)]
83+
[int] $Hours,
9184

92-
[Parameter(ParameterSetName = "DateInterval", Mandatory = $false)]
93-
[int] $Minutes,
85+
[Parameter(ParameterSetName = 'DateInterval', Mandatory = $false)]
86+
[int] $Minutes,
9487

95-
[Parameter(ParameterSetName = "DateInterval", Mandatory = $false)]
96-
[int] $Seconds,
88+
[Parameter(ParameterSetName = 'DateInterval', Mandatory = $false)]
89+
[int] $Seconds,
9790

98-
[Switch] $Force = $false
91+
[Switch] $Force = $false
9992
)
10093

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+
}
103100

104101
function Install-TeamViewerModule {
105102
$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+
}
113111

114112
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+
}
148148
}
149149

150150
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
160164
}

0 commit comments

Comments
 (0)