-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ADComputerLastLogon.ps1
More file actions
67 lines (57 loc) · 2.65 KB
/
Copy pathGet-ADComputerLastLogon.ps1
File metadata and controls
67 lines (57 loc) · 2.65 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
<#
.SYNOPSIS
Ermittelt je AD-Computer die letzte Anmeldung ueber alle Domain
Controller hinweg.
.DESCRIPTION
Das Attribut lastLogon wird nicht repliziert: jeder Domain Controller
fuehrt seinen eigenen Wert. Wer nur einen DC fragt, bekommt ein zu altes
Datum. Das Skript fragt deshalb alle DCs ab und behaelt je Computer den
juengsten Wert.
Mit ausgegeben werden Betriebssystem, Enabled-Status, Beschreibung,
pwdLastSet und - sofern vorhanden - das LAPS-Passwort aus
ms-Mcs-AdmPwd.
.NOTES
Das LAPS-Attribut ist nur fuer Konten sichtbar, die dafuer berechtigt
sind; sonst bleibt die Spalte leer.
#>
function Get-ADComputerLastLogon {
[CmdletBinding()]
param(
[string]$Filter = '*'
)
# Alle DCs ermitteln
$DCs = Get-ADDomainController -Filter *
$allResults = foreach ($DC in $DCs) {
Get-ADComputer -Filter $Filter -Server $DC.HostName -Properties lastLogon, pwdLastSet, OperatingSystem, Enabled, Description, DistinguishedName, ms-Mcs-AdmPwd |
Select-Object Name,
ms-Mcs-AdmPwd,
DistinguishedName,
OperatingSystem,
Enabled,
@{Name = 'DC'; Expression = { $DC.HostName } },
@{Name = 'LastLogon'; Expression = { [datetime]::FromFileTime($_.lastLogon) } },
@{Name = 'PwdLastSet'; Expression = { [datetime]::FromFileTime($_.pwdLastSet) } },
Description
}
# pro Computer den neuesten Logonwert wählen
$allResults | Group-Object DistinguishedName | ForEach-Object {
$latest = $_.Group | Sort-Object LastLogon -Descending | Select-Object -First 1
[PSCustomObject]@{
Name = $latest.Name
OperatingSystem = $latest.OperatingSystem
Enabled = $latest.Enabled
LastLogon = $latest.LastLogon
PwdLastSet = $latest.PwdLastSet
LapsPwd = $latest.'ms-Mcs-AdmPwd'
Description = $latest.Description
DistinguishedName = $latest.DistinguishedName
}
}
}
# Beispiel: nur aktive Windows 10 Computer
Get-ADComputerLastLogon -Filter 'Enabled -eq $True -and OperatingSystem -like "*Windows*10*"' | Out-GridView
$erg = Get-ADComputerLastLogon -Filter 'Enabled -eq $True -and OperatingSystem -like "*Windows*"'
$thresholdDate = (Get-Date).AddMonths((-1))
$erg | Where-Object { $_.LastLogon -lt $thresholdDate } | Out-GridView -Title "Aktive Computer mit letztem Logon älter als 1 Monat"
$erg | Where-Object { $_.LastLogon -gt $thresholdDate } | Out-GridView -Title "Aktive Computer mit letztem Logon neuer als 1 Monat"
$myhosts = ($erg | Where-Object { $_.LastLogon -gt $thresholdDate }).Name