вот один из способов сделать то, что я думаю, что вы хотите. У меня нет событий 4634
или 4648
- только 4624
, поэтому я добавил это в список для проверки. при этом используются переменные среды $env:USERNAME
& $env:COMPUTERNAME
, чтобы получить имя целевого пользователя и имя системы.
строит filterhashtable
для получения информации, а затем восклицательный знак для хранения параметров для вызова командлета Get-WinEvent
.
#requires -RunAsAdministrator
# there REALLY otta be a way to get this list programmatically
$LogonTypeTable = [ordered]@{
'0' = 'System'
'2' = 'Interactive'
'3' = 'Network'
'4' = 'Batch'
'5' = 'Service'
'6' = 'Proxy'
'7' = 'Unlock'
'8' = 'NetworkCleartext'
'9' = 'NewCredentials'
'10' = 'RemoteInteractive'
'11' = 'CachedInteractive'
'12' = 'CachedRemoteInteractive'
'13' = 'CachedUnlock'
}
$EventLevelTable = [ordered]@{
LogAlways = 0
Critical = 1
Error = 2
Warning = 3
Informational = 4
Verbose = 5
}
$TargetUserName = $env:USERNAME
$WantedLogonTypes = @(2, 3, 10, 11)
$AgeInDays = 30
$StartDate = (Get-Date).AddDays(-$AgeInDays)
$ComputerName = $env:COMPUTERNAME
$GWE_FilterHashTable = @{
Logname = 'Security'
ID = 4624, 4634, 4648
StartTime = $StartDate
#Level = 2
}
$GWE_Params = @{
FilterHashtable = $GWE_FilterHashTable
ComputerName = $ComputerName
MaxEvents = 100
}
$RawLogonEventList = Get-WinEvent @GWE_Params
$LogonEventList = foreach ($RLEL_Item in $RawLogonEventList)
{
$UserName = $RLEL_Item.Properties[5].Value
$LogonTypeID = $RLEL_Item.Properties[8].Value
#<#
# if you want all logins, use the other version of this IF block
if (($LogonTypeID -in $WantedLogonTypes) -and
($UserName -eq $TargetUserName))
#>
<#
# this will get all accounts
if ($LogonTypeID -in $WantedLogonTypes)
#>
{
$DaysAgo = [math]::Round(([datetime]::Now - $RLEL_Item.TimeCreated).TotalDays, 0)
[PSCustomObject]@{
LogName = $RLEL_Item.LogName
EventID = $RLEL_Item.Id
TimeCreated = $RLEL_Item.TimeCreated
DaysAgo = $DaysAgo
UserName = $RLEL_Item.Properties[5].Value
LogonTypeID = $LogonTypeID
LogonTypeName = $LogonTypeTable[$LogonTypeID.ToString()]
}
}
}
$LogonEventList |
Sort-Object -Property TimeCreated
вывод ...
LogName : Security
EventID : 4624
TimeCreated : 2019-06-22 11:10:35 AM
DaysAgo : 10
UserName : [MyUserName]
LogonTypeID : 2
LogonTypeName : Interactive
есть только один за последние 30 дней для меня. [ ухмылка ]