-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwin-unlocked-time.ps1
More file actions
55 lines (52 loc) · 1.54 KB
/
win-unlocked-time.ps1
File metadata and controls
55 lines (52 loc) · 1.54 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
$Days = 8
$events = @()
$events += Get-WinEvent -FilterHashtable @{
LogName = 'Security'
Id = @(4800, 4801)
StartTime = (Get-Date).AddDays( - $Days)
}
$events += Get-WinEvent -FilterHashtable @{
LogName = 'System'
Id = @(7000, 7001, 1, 42)
StartTime = (Get-Date).AddDays( - $Days)
}
$type_lu = @{
7001 = 'Logon'
7002 = 'Logoff'
4800 = 'Lock'
4801 = 'UnLock'
}
$ns = @{'ns' = 'http://schemas.microsoft.com/win/2004/08/events/event'}
$target_xpath = "//ns:Data[@Name='TargetUserName']"
$usersid_xpath = "//ns:Data[@Name='UserSid']"
If ($events) {
$results = ForEach ($event in $events) {
$xml = $event.ToXml()
Switch -Regex ($event.Id) {
'4...' {
$user = (
Select-Xml -Content $xml -Namespace $ns -XPath $target_xpath
).Node.'#text'
Break
}
'7...' {
$sid = (
Select-Xml -Content $xml -Namespace $ns -XPath $usersid_xpath
).Node.'#text'
$user = (
New-Object -TypeName 'System.Security.Principal.SecurityIdentifier' -ArgumentList $sid
).Translate([System.Security.Principal.NTAccount]).Value
Break
}
}
New-Object -TypeName PSObject -Property @{
Time = $event.TimeCreated
Id = $event.Id
Type = $type_lu[$event.Id]
User = $user
}
}
If ($results) {
$results
}
}