powershell - Match the User Name against the Security EventLog -
i want take domain user , want check security event logs logon , print events match returns me null value:
get-eventlog -log security -computer pc1 -instanceid 4624 -after(get-date).adddays(-2) | ? { $_.message -match "account name:\s+qasimali\s" -and $_.message -match 'logon type:\s+(2|10)\s" }
but generates no data output
read-host : name cannot null or empty.
whereas command runs , gives no error. want check whether command running fine or not.
the way have done in past follows ( thoroughly commented clarity) :
## set username input $userinput = "domainuser" ## set date in past retrieve events $starttime = ((get-date).addminutes(-2)) ##set domain controller search on $computername = "dc1" ## retrieve event 4624 dc eveng logs $logons = get-winevent -computername $computername -filterhashtable @{logname="security"; id="4624"; starttime=$starttime;endtime=(get-date)} ## initialize variable store outputs in $eventoutput = @() ## enumerate events retrieve usernames compare against user input foreach ($logon in $logons) { ## convert event xml $logonxml = [xml]$logon.toxml() ## retrieve username xml object $logonuser = (($logonxml.event.eventdata.data | select "#text")[5])."#text" ## retrieve logon type xml object $logontype = (($logonxml.event.eventdata.data | select "#text")[8])."#text" ## check event username matches user input if ($logonuser -match $userinput) { ## check logontype correct if ($logontype -eq 2 -or $logontype -eq 10) { ## append event object event output $eventoutput += $logon } } } ## output resulting event output object $eventoutput
the resulting output can manipulated retrieve whatever details wish. find converting each object xml parse further values useful.
note : i've thrown memory, can restructured enable other queries if required. start , end times need changed extract information correct timespan.
Comments
Post a Comment