-
Notifications
You must be signed in to change notification settings - Fork 6
/
Delete-StaleDevices.ps1
58 lines (54 loc) · 2.25 KB
/
Delete-StaleDevices.ps1
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
<#
.SYNOPSIS
Deletes device records for enterprise wipe pending or unenrolled devices that have not checked in with AirWatch since a configurable number of days ago.
.DESCRIPTION
This script displays a list of all Organization groups in an environment, allowing the user to select an organization group.
The user then enters a number of days(X) since the devices have been last seen.
All of the devices in that organization group (and child org groups) that have not been seen since X days and are pending
enterprise wipe or unenrolled are deleted are exported to a CSV file named with that date.
.INPUTS
AirWatchConfig.json
.OUTPUTS
Outputs a CSV file with Devices that have not been seen in X number of days that have been deleted.
.NOTES
Version: 1.8
Author: Joshua Clark @MrTechGadget
Creation Date: 09/15/2017
Last Updated: 10/13/2022
Site: https://github.com/MrTechGadget/aw-bulkdevices-script
.EXAMPLE
Delete-StaleDevices.ps1 -pageSize 1000 -maxLastSeen 250
#>
[CmdletBinding()]
Param(
[Parameter(HelpMessage="Number of devices returned, default is 500")]
[string]$pageSize = "500",
[Parameter(HelpMessage="Maximum number of days since devices were last seen, default is 200")]
[string]$maxLastSeen = "200"
)
Import-Module .\PSairwatch.psm1
Write-Log -logstring "$($MyInvocation.Line)"
$OrgGroups = Get-OrgGroups
$GroupID = Select-Tag $OrgGroups
$DaysPrior = Set-DaysPrior $maxLastSeen
$LastSeenDate = Set-LastSeenDate $DaysPrior
Write-Host("------------------------------")
Write-Host("")
Write-Host "Devices last seen on or before " + $LastSeenDate
$Devices = Get-Device $LastSeenDate $GroupID $pageSize
$DeviceList = Set-UnenrolledDeviceIdList $Devices
$DeviceJSON = Set-AddTagJSON $DeviceList
$DeviceDetails = Get-DeviceDetails $DeviceJSON
if ($DeviceList.Count -ne 0) {
Write-Host $DeviceList.Count "Pending Enterprise Wipe Devices exported"
$DeviceDetails | Export-Csv -Path "EntWipeDevicesLastSeen${LastSeenDate}.csv"
}
Write-Host("------------------------------")
Write-Host("")
$DeletedDevices = Remove-DeviceBulk $DeviceJSON
Write-Host("------------------------------")
if ($DeletedDevices -eq 404) {
Write-Host "No devices found pending enterprise wipe in that time frame."
} else {
$DeletedDevices
}