Windows Server 2008 R2
I'm trying to use PowerShell to get me a list of users who have logged into Remote Desktop Services (formerly known as Terminal Services) during the past day. With little understanding and much copy and pasting, I have this little script:
$a = (Get-Date).AddDays(-1)
Get-EventLog -LogName Security -after $a | Where-Object {($_.EventID -eq '4624') -and $_.EntryType -eq 'SuccessAudit') -and ($_.Message | Select-String "Logon Type:\t\t\t10")}
The default output tells me things have happened and when they happened which is a good start. What I'd really like is to also display the User. Darned if I can figure out how to get the User and/or how to display it.
And that's my question: How can I add the username associated with that Event ID 4624 / Logon Type 10 event? Ideally I'd just like to show the login time and user name.
First I would suggest using the
Get-WinEvent
and passing a hash to do as much filtering as possible there (and thus avoid creating lots of objectsWhere-Object
will throw away):Level 0 is success audit. This can be performed remotely with the
-computer
parameter. Then filter the results to get the login type:Using a regex to avoid hardcoding the whitespace.
To extract the user and domain from the message would be a little awkward as there are two "Account Name' values: one for the computer and one for the user. But all the replaceable values inserting into the (localisable) message text are in the event's Properties property, so a little checking to see the indexes with a sample1
Clearly capturing other details (eg. SID, client IP) follows the same pattern.
Hence:
1 With a single event in
$ev
I used:to give (with a little censorship, and noting a better way to get the logon type at index #8):
I'd do it as follows -
EDIT: This now returns the names of the individuals. You can play around with what exactly you'd like to extract from that XML document.
Note: You'll need to putz around with the TimeCreated values (probably generate them on the fly). I included these so you could see the format they required.
Get-WinEvent will be much faster than Get-EventLog since the filtering will be done server-side instead of in the pipeline. You can also get a bit more specific on your queries by using the FilterXML parameter. The usernames associated with the logon events are in the
Message
property of the returnedEventLogRecord
.