Skip to content

Commit

Permalink
Merge pull request #23 from teamviewer/Remove-TeamViewerOutdatedDevic…
Browse files Browse the repository at this point in the history
…eV2_FormatAndScriptAnalyzer

RemoveTVOutdatedDevicesV2: format & ScriptAnalyzer
  • Loading branch information
ChristianJ-TV committed Aug 10, 2023
2 parents 754896d + e50e632 commit b4ee7f4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 97 deletions.
8 changes: 3 additions & 5 deletions Remove-TeamViewerOutdatedDeviceV2/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# Remove-TeamViewerOutdatedDeviceV2

Removes TeamViewer devices that didn't appear online for a given time.
Removes TeamViewer devices (MDv2) that didn't appear online for a given time.

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 script fetches a list of TeamViewer devices (MDv2) 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.

## Prerequisites
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# Copyright (c) 2019-2021 TeamViewer GmbH
# See file LICENSE.txt
# Copyright (c) 2019-2023 TeamViewer Germany GmbH
# See file LICENSE

BeforeAll {
$testApiToken = [securestring]@{}
. "$PSScriptRoot\Remove-TeamViewerOutdatedDeviceV2.ps1" `
-ApiToken $testApiToken `
-ExpiryDate (Get-Date) `
-InformationAction SilentlyContinue

. "$PSScriptRoot\Remove-TeamViewerOutdatedDeviceV2.ps1" -ApiToken $testApiToken -ExpiryDate (Get-Date) -InformationAction SilentlyContinue

Mock Get-TeamViewerManagedDevice { @(
[pscustomobject]@{ Id = 'device1'; Name = 'device1'; LastSeenAt = [datetime]'2018-12-16' },
[pscustomobject]@{ Id = 'device2'; Name = 'device2'; LastSeenAt = [datetime]'2018-12-17' },
[pscustomobject]@{ Id = 'device3'; Name = 'device3'; LastSeenAt = [datetime]'2018-12-18' },
[pscustomobject]@{ Id = 'device4'; Name = 'device4'; LastSeenAt = [datetime]'2018-12-19' }
) }

Mock Remove-TeamViewerManagedDeviceManagement -RemoveParameterValidation 'Device' {}
}

Expand All @@ -28,6 +27,7 @@ Describe 'Remove-TeamViewerOutdatedDeviceV2' {
$result[1].Status | Should -Be 'Unchanged'
$result[2].DeviceId | Should -Be 'device3'
$result[2].Status | Should -Be 'Unchanged'

Assert-MockCalled Get-TeamViewerManagedDevice -Times 1 -Scope It
Assert-MockCalled Remove-TeamViewerManagedDeviceManagement -Times 0 -Scope It
}
Expand All @@ -41,6 +41,7 @@ Describe 'Remove-TeamViewerOutdatedDeviceV2' {
$result[1].Status | Should -Be 'Removed'
$result[2].DeviceId | Should -Be 'device3'
$result[2].Status | Should -Be 'Removed'

Assert-MockCalled Get-TeamViewerManagedDevice -Times 1 -Scope It
Assert-MockCalled Remove-TeamViewerManagedDeviceManagement -Times 3 -Scope It
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
<#
.SYNOPSIS
Removes TeamViewer devices that didn't appear online for a given time.
Removes TeamViewer devices (MDv2) 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 script fetches a list of TeamViewer devices (MDv2) 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:
- `Device Groups > read operations, modifying operations`
The token requires the following access permissions: `Device Groups > read operations, modifying operations`
.PARAMETER ExpiryDate
A specific expiry date. All devices that haven't been online since that
date are considered being removed.
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.
Should be used in combination with the `Days`, `Hours`, `Minutes` and/or `Seconds` parameter.
.PARAMETER Days
Days of the expiration interval.
Expand All @@ -41,11 +36,9 @@
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.
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'
Expand All @@ -67,94 +60,105 @@
Install-Module TeamViewerPS
```
Copyright (c) 2019-2021 TeamViewer GmbH
See file LICENSE.txt
Version 2.0
Copyright (c) 2019-2023 TeamViewer Germany GmbH
See file LICENSE
Version 2.1
#>

[CmdletBinding(DefaultParameterSetName = "ExactDate", SupportsShouldProcess = $true)]
[CmdletBinding(DefaultParameterSetName = 'ExactDate', SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)]
[securestring] $ApiToken,
[Parameter(Mandatory = $true)]
[securestring] $ApiToken,

[Parameter(ParameterSetName = "ExactDate", Mandatory = $true)]
[DateTime] $ExpiryDate,
[Parameter(ParameterSetName = 'ExactDate', Mandatory = $true)]
[DateTime] $ExpiryDate,

[Parameter(ParameterSetName = "DateInterval", Mandatory = $true)]
[switch] $ExpiryInterval,
[Parameter(ParameterSetName = 'DateInterval', Mandatory = $true)]
[switch] $ExpiryInterval,

[Parameter(ParameterSetName = "DateInterval", Mandatory = $false)]
[int] $Days,
[Parameter(ParameterSetName = 'DateInterval', Mandatory = $false)]
[int] $Days,

[Parameter(ParameterSetName = "DateInterval", Mandatory = $false)]
[int] $Hours,
[Parameter(ParameterSetName = 'DateInterval', Mandatory = $false)]
[int] $Hours,

[Parameter(ParameterSetName = "DateInterval", Mandatory = $false)]
[int] $Minutes,
[Parameter(ParameterSetName = 'DateInterval', Mandatory = $false)]
[int] $Minutes,

[Parameter(ParameterSetName = "DateInterval", Mandatory = $false)]
[int] $Seconds,
[Parameter(ParameterSetName = 'DateInterval', Mandatory = $false)]
[int] $Seconds,

[Switch] $Force = $false
[Switch] $Force = $false
)

if (-Not $MyInvocation.BoundParameters.ContainsKey('ErrorAction')) { $script:ErrorActionPreference = 'Stop' }
if (-Not $MyInvocation.BoundParameters.ContainsKey('InformationAction')) { $script:InformationPreference = 'Continue' }
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 '1.5.0') {
Update-Module TeamViewerPS
}
}

if (!$module) {
Install-Module TeamViewerPS
}
elseif ($module.Version -lt '1.5.0') {
Update-Module TeamViewerPS
}
}

function Remove-TeamViewerOutdatedDeviceV2 {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
param($expiryDate, [bool]$force)

$devices = @((Get-TeamViewerManagedDevice -ApiToken $ApiToken) |
Where-Object { !$_.IsOnline -And $_.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-TeamViewerManagedDeviceManagement -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
})
}
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
param($expiryDate, [bool]$force)

$devices = @((Get-TeamViewerManagedDevice -ApiToken $ApiToken) | Where-Object { !$_.IsOnline -And $_.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-TeamViewerManagedDeviceManagement -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-TeamViewerOutdatedDeviceV2 -expiryDate $ExpiryDate -force $Force
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-TeamViewerOutdatedDeviceV2 -expiryDate $ExpiryDate -force $Force
}

0 comments on commit b4ee7f4

Please sign in to comment.