|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Removes TeamViewer devices that didn't appear online for a given time. |
| 4 | +
|
| 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. |
| 10 | + The expiration can either be specified by a specific date or by interval. |
| 11 | +
|
| 12 | + .PARAMETER ApiToken |
| 13 | + The TeamViewer API token to use. |
| 14 | + Must be a user access token. |
| 15 | + The token requires the following access permissions: |
| 16 | + - `Computer & Contacts > View entries, add entries, edit entries, remove entries` |
| 17 | +
|
| 18 | + .PARAMETER ExpiryDate |
| 19 | + A specific expiry date. All devices that haven't been online since that |
| 20 | + date are considered being removed. |
| 21 | +
|
| 22 | + .PARAMETER ExpiryInterval |
| 23 | + 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. |
| 26 | +
|
| 27 | + .PARAMETER Days |
| 28 | + Days of the expiration interval. |
| 29 | + Must be used in combination with the `ExpiryInterval` parameter. |
| 30 | +
|
| 31 | + .PARAMETER Hours |
| 32 | + Hours of the expiration interval. |
| 33 | + Must be used in combination with the `ExpiryInterval` parameter. |
| 34 | +
|
| 35 | + .PARAMETER Minutes |
| 36 | + Minutes of the expiration interval. |
| 37 | + Must be used in combination with the `ExpiryInterval` parameter. |
| 38 | +
|
| 39 | + .PARAMETER Seconds |
| 40 | + Seconds of the expiration interval. |
| 41 | + Must be used in combination with the `ExpiryInterval` parameter. |
| 42 | +
|
| 43 | + .PARAMETER Remove |
| 44 | + If set, the script actually removes outdated devices. |
| 45 | + Otherwise, the script only outputs possible removal candidates. |
| 46 | + The default value is `false`. |
| 47 | +
|
| 48 | + .PARAMETER Force |
| 49 | + If set, the script will NOT ask the user for confirmation of the |
| 50 | + removal. This parameter only has effect in combination with the |
| 51 | + `Remove` parameter. |
| 52 | + The default value is `false`, causing the script to ask the user |
| 53 | + one more time before starting to remove devices. |
| 54 | +
|
| 55 | + .EXAMPLE |
| 56 | + .\Remove-TeamViewerOutdatedDevice -ExpiryDate '2018-12-17T17:00:00' |
| 57 | +
|
| 58 | + .EXAMPLE |
| 59 | + .\Remove-TeamViewerOutdatedDevice -ExpiryDate 2018-12-31 -WhatIf |
| 60 | +
|
| 61 | + .EXAMPLE |
| 62 | + .\Remove-TeamViewerOutdatedDevice -ExpiryInterval -Days 10 |
| 63 | +
|
| 64 | + .EXAMPLE |
| 65 | + .\Remove-TeamViewerOutdatedDevice -ExpiryInterval -Hours 12 -Force |
| 66 | +
|
| 67 | + .NOTES |
| 68 | + Copyright (c) 2019 TeamViewer GmbH |
| 69 | + See file LICENSE.txt |
| 70 | + Version 1.0.0 |
| 71 | +#> |
| 72 | + |
| 73 | +[CmdletBinding(DefaultParameterSetName="ExactDate", SupportsShouldProcess=$true)] |
| 74 | +param( |
| 75 | + [Parameter(Mandatory = $true)] |
| 76 | + [string] $ApiToken, |
| 77 | + |
| 78 | + [Parameter(ParameterSetName = "ExactDate", Mandatory = $true)] |
| 79 | + [DateTime] $ExpiryDate, |
| 80 | + |
| 81 | + [Parameter(ParameterSetName = "DateInterval", Mandatory = $true)] |
| 82 | + [switch] $ExpiryInterval, |
| 83 | + |
| 84 | + [Parameter(ParameterSetName = "DateInterval", Mandatory = $false)] |
| 85 | + [int] $Days, |
| 86 | + |
| 87 | + [Parameter(ParameterSetName = "DateInterval", Mandatory = $false)] |
| 88 | + [int] $Hours, |
| 89 | + |
| 90 | + [Parameter(ParameterSetName = "DateInterval", Mandatory = $false)] |
| 91 | + [int] $Minutes, |
| 92 | + |
| 93 | + [Parameter(ParameterSetName = "DateInterval", Mandatory = $false)] |
| 94 | + [int] $Seconds, |
| 95 | + |
| 96 | + [Switch] $Force = $false |
| 97 | +) |
| 98 | + |
| 99 | +if (-Not $MyInvocation.BoundParameters.ContainsKey('ErrorAction')) { $script:ErrorActionPreference = 'Stop' } |
| 100 | +if (-Not $MyInvocation.BoundParameters.ContainsKey('InformationAction')) { $script:InformationPreference = 'Continue' } |
| 101 | + |
| 102 | +$tvApiVersion = 'v1' |
| 103 | +$tvApiBaseUrl = 'https://webapi.teamviewer.com' |
| 104 | + |
| 105 | +function ConvertTo-TeamViewerRestError { |
| 106 | + param([parameter(ValueFromPipeline)]$err) |
| 107 | + try { return ($err | Out-String | ConvertFrom-Json) } |
| 108 | + catch { return $err } |
| 109 | +} |
| 110 | + |
| 111 | +function Invoke-TeamViewerRestMethod { |
| 112 | + # Using `Invoke-WebRequest` instead of `Invoke-RestMethod`: |
| 113 | + # There is a known issue for PUT and DELETE operations to hang on Windows Server 2012. |
| 114 | + try { return ((Invoke-WebRequest -UseBasicParsing @args).Content | ConvertFrom-Json) } |
| 115 | + catch [System.Net.WebException] { |
| 116 | + $stream = $_.Exception.Response.GetResponseStream() |
| 117 | + $reader = New-Object System.IO.StreamReader($stream) |
| 118 | + $reader.BaseStream.Position = 0 |
| 119 | + Throw ($reader.ReadToEnd() | ConvertTo-TeamViewerRestError) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function Get-TeamViewerDevice($accessToken, $onlineState) { |
| 124 | + return Invoke-TeamViewerRestMethod -Uri "$tvApiBaseUrl/api/$tvApiVersion/devices" ` |
| 125 | + -Method Get -Headers @{authorization = "Bearer $accessToken"} ` |
| 126 | + -Body @{ online_state = $onlineState } |
| 127 | +} |
| 128 | + |
| 129 | +function Remove-TeamViewerDevice { |
| 130 | + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'None')] |
| 131 | + param($accessToken, $deviceId) |
| 132 | + if ($PSCmdlet.ShouldProcess($deviceId)) { |
| 133 | + return Invoke-TeamViewerRestMethod -Uri "$tvApiBaseUrl/api/$tvApiVersion/devices/$deviceId" ` |
| 134 | + -Method Delete -Headers @{authorization = "Bearer $accessToken"} |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +function Remove-TeamViewerOutdatedDevice { |
| 139 | + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')] |
| 140 | + param($accessToken, $expiryDate, [bool]$force) |
| 141 | + |
| 142 | + $devices = @((Get-TeamViewerDevice $accessToken 'offline').devices | ` |
| 143 | + Where-Object { $_.last_seen -And [DateTime]($_.last_seen) -le $expiryDate }) |
| 144 | + |
| 145 | + Write-Information "Found $($devices.Count) devices that have been offline since $expiryDate" |
| 146 | + |
| 147 | + if ($devices.Count -gt 0 -And -Not $WhatIfPreference -And -Not $force -And |
| 148 | + -Not $PSCmdlet.ShouldContinue("Do you really want to remove those devices?", $devices)) { |
| 149 | + Write-Information "Aborting..." |
| 150 | + exit |
| 151 | + } |
| 152 | + |
| 153 | + ForEach ($device in $devices) { |
| 154 | + $status = 'Unchanged' |
| 155 | + if ($force -Or $PSCmdlet.ShouldProcess($device.alias)) { |
| 156 | + try { |
| 157 | + Remove-TeamViewerDevice $accessToken $device.device_id |
| 158 | + $status = 'Removed' |
| 159 | + } |
| 160 | + catch { |
| 161 | + Write-Warning "Failed to remove device '$($device.alias)': $_" |
| 162 | + $status = 'Failed' |
| 163 | + } |
| 164 | + } |
| 165 | + Write-Output ([pscustomobject]@{ |
| 166 | + Alias = $device.alias |
| 167 | + DeviceId = $device.device_id |
| 168 | + LastSeen = [DateTime]($device.last_seen) |
| 169 | + Status = $status |
| 170 | + }) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +if ($MyInvocation.InvocationName -ne '.') { |
| 175 | + $now = (Get-Date) |
| 176 | + if ($ExpiryInterval) { |
| 177 | + $ExpiryDate = $now.AddDays(-1 * $Days).AddHours(-1 * $Hours).AddMinutes(-1 * $Minutes).AddSeconds(-1 * $Seconds) |
| 178 | + } |
| 179 | + if ($ExpiryDate -ge $now) { |
| 180 | + Throw "Invalid expiry date specified: $ExpiryDate" |
| 181 | + } |
| 182 | + Remove-TeamViewerOutdatedDevice $ApiToken $ExpiryDate $Force |
| 183 | +} |
0 commit comments