-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ADUserLastLogonCache.ps1
More file actions
74 lines (64 loc) · 2.31 KB
/
Copy pathGet-ADUserLastLogonCache.ps1
File metadata and controls
74 lines (64 loc) · 2.31 KB
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
<#
.SYNOPSIS
Baut eine Nachschlagetabelle SamAccountName -> letzte Anmeldung ueber
alle Domain Controller.
.DESCRIPTION
Fragt jeden DC einmal ab und behaelt je Benutzer den juengsten
lastLogon-Wert. Das Attribut wird nicht repliziert, ein einzelner DC
liefert also einen zu alten Wert.
Gedacht als Vorstufe fuer Auswertungen, die den Wert fuer viele Benutzer
brauchen: einmal alle DCs abfragen ist deutlich schneller als je Benutzer
einzeln.
.NOTES
Rueckgabe ist eine Hashtable mit SamAccountName als Schluessel und einem
DateTime als Wert.
#>
function Get-ADUserLastLogonCache {
param(
[string[]]$SamAccountNames
)
$DCs = Get-ADDomainController -Filter * | Select-Object -ExpandProperty HostName
$Cache = @{}
foreach ($DC in $DCs) {
Get-ADUser -Filter * -Server $DC -Properties SamAccountName, LastLogon |
Where-Object { $_.SamAccountName -in $SamAccountNames } |
ForEach-Object {
$Current = $Cache[$_.SamAccountName]
if (-not $Current -or $_.LastLogon -gt $Current) {
$Cache[$_.SamAccountName] = $_.LastLogon
}
}
}
# Rückgabe als Hashtable mit SamAccountName -> DateTime
$Result = @{}
foreach ($Key in $Cache.Keys) {
$Result[$Key] = if ($Cache[$Key] -gt 0) {
[DateTime]::FromFileTime($Cache[$Key])
}
else {
$null
}
}
return $Result
}
$OU = "OU=USRMGMT,OU=ITMGMT,DC=mycorp,DC=local"
$thresholdDate = (Get-Date).AddYears(-3)
# Hauptabfrage
$Users = Get-ADUser -Filter { Enabled -eq $true } -SearchBase $OU -Properties Name, SamAccountName, PasswordLastSet, DistinguishedName |
Where-Object {
$_.PasswordLastSet -lt $thresholdDate -and
$_.DistinguishedName -notmatch "OU=Dienst-Accounts"
}
$SamNames = $Users.SamAccountName
$Logons = Get-ADUserLastLogonCache -SamAccountNames $SamNames
$Users | ForEach-Object {
[PSCustomObject]@{
Name = $_.Name
SamAccountName = $_.SamAccountName
PasswordLastSet = $_.PasswordLastSet
DistinguishedName = $_.DistinguishedName
LastLogonDate = $Logons[$_.SamAccountName]
}
} |
Sort-Object PasswordLastSet |
Out-GridView -Title "Aktive Benutzer in USRMGMT ohne Dienst-Accounts mit Passwort älter als 3 Jahre"