-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathGet-LoggedOnUser.ps1
executable file
·45 lines (37 loc) · 1.51 KB
/
Get-LoggedOnUser.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
# https://gallery.technet.microsoft.com/scriptcenter/0e43993a-895a-4afe-a2b2-045a5146048a
function Get-LoggedOnUser ($computername = $env:COMPUTERNAME) {
$regexa = '.+Domain="(.+)",Name="(.+)"$'
$regexd = '.+LogonId="(\d+)"$'
$logontype = @{
0 = 'Local System'
2 = 'Interactive' #(Local logon)
3 = 'Network' # (Remote logon)
4 = 'Batch' # (Scheduled task)
5 = 'Service' # (Service account logon)
7 = 'Unlock' #(Screen saver)
8 = 'NetworkCleartext' # (Cleartext network logon)
9 = 'NewCredentials' #(RunAs using alternate credentials)
10 = 'RemoteInteractive' #(RDP\TS\RemoteAssistance)
11 = 'CachedInteractive' #(Local w\cached credentials)
}
$logon_sessions = @(gwmi win32_logonsession -ComputerName $computername)
$logon_users = @(gwmi win32_loggedonuser -ComputerName $computername)
$session_user = @{}
$logon_users | % {
$_.antecedent -match $regexa > $nul
$username = $matches[1] + "\" + $matches[2]
$_.dependent -match $regexd > $nul
$session = $matches[1]
$session_user[$session] += $username
}
$logon_sessions | % {
$starttime = [management.managementdatetimeconverter]::todatetime($_.starttime)
New-Object psobject -Property @{
Session = $_.logonid
User = $session_user[$_.logonid]
Type = $logontype[$_.logontype.tostring()]
Auth = $_.authenticationpackage
StartTime = $starttime
}
}
}