-
Notifications
You must be signed in to change notification settings - Fork 4
/
disable-duplicateAzureAdDevices.ps1
126 lines (113 loc) · 4.52 KB
/
disable-duplicateAzureAdDevices.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#Requires -Modules AzureAD
<#
.DESCRIPTION
Cleans up any duplicate devices in Azure AD that have the same hardware ID, by only leaving the most recently active one enabled
.PARAMETER WhatIf
If specified, will not remove/disable anything (report-only mode).
-Force overrides -WhatIf
.PARAMETER Force
If specified, devices will be REMOVED instead of disabled
NOTE: -Force overrides -WhatIf!
.NOTES
author: Jos Lieben
blog: www.lieben.nu
created: 17/12/2019
editied: 07/30/2020
Modified by: Powershellcrack
.CHANGES
- Added it as a function
- changed cmdlets to AzureAD
#>
function Invoke-AzureADCleanup {
Param(
[Switch]$WhatIf,
[Switch]$Force
)
Begin {
Write-Verbose "Checking Azure Connection"
try{
$null = Connect-AzureAD
}catch{
Throw "Not authenticated. Please use the `"Connect-AzureAD`" command to authenticate."
}
}
Process {
#get all enabled AzureAD devices
$devices = Get-AzureADDevice -All:$true | Where{$_.AccountEnabled}
$hwIds = @{}
$duplicates=@{}
#create hashtable with all devices that have a Hardware ID
foreach($device in $devices){
$physId = $Null
foreach($deviceId in $device.DevicePhysicalIds){
if($deviceId.StartsWith("[HWID]")){
$physId = $deviceId.Split(":")[-1]
}
}
if($physId){
if(!$hwIds.$physId){
$hwIds.$physId = @{}
$hwIds.$physId.Devices = @()
$hwIds.$physId.DeviceCount = 0
}
$hwIds.$physId.DeviceCount++
$hwIds.$physId.Devices += $device
}
}
#select HW ID's that have multiple device entries
$hwIds.Keys | % {
if($hwIds.$_.DeviceCount -gt 1){
$duplicates.$_ = $hwIds.$_.Devices
}
}
#loop over the duplicate HW Id's
$cleanedUp = 0
$totalDevices = 0
foreach($key in $duplicates.Keys){
$mostRecent = (Get-Date).AddYears(-100)
foreach($device in $duplicates.$key){
$totalDevices++
#detect which device is the most recently active device
if([DateTime]$device.ApproximateLastLogonTimestamp -gt $mostRecent){
$mostRecent = [DateTime]$device.ApproximateLastLogonTimestamp
}
}
foreach($device in $duplicates.$key){
if([DateTime]$device.ApproximateLastLogonTimestamp -lt $mostRecent){
try{
if($Force){
Remove-AzureADDevice -ObjectId $device.objectId -verbose -ErrorAction Stop
Write-Output "Removed Stale device $($device.DisplayName) with last active date: $($device.ApproximateLastLogonTimestamp)"
}elseif($WhatIf){
Write-Output "Should disable Stale device $($device.DisplayName) with last active date: $($device.ApproximateLastLogonTimestamp)"
}else{
Disable-AzureADDevice -ObjectId $device.objectId -verbose -ErrorAction Stop
Write-Output "Disabled Stale device $($device.DisplayName) with last active date: $($device.ApproximateLastLogonTimestamp)"
}
$cleanedUp++
}catch{
Write-Output "Failed to disable Stale device $($device.DisplayName) with last active date: $($device.ApproximateLastLogonTimestamp)"
Write-Output $_.Exception
}
}
}
}
Write-Output "Total unique hardware ID's with >1 device registration: $($duplicates.Keys.Count)"
Write-Output "Total devices registered to these $($duplicates.Keys.Count) hardware ID's: $totalDevices"
Write-Output "Devices cleaned up: $cleanedUp"
<# fun snippet to get distribution:
$distribution = @{}
foreach($key in $duplicates.Keys){
if($distribution.$($duplicates.$key.Count)){
[Int]$distribution.$($duplicates.$key.Count)++ | out-null
}else{
[Int]$distribution.$($duplicates.$key.Count) = 1
}
}
Write-Output $distribution
#>
}
End {
}
}
Invoke-AzureADCleanup -WhatIf