troubleshooting Question

Powershell - Remote PC Report: Logon, Logoff, Lock and Unlock

Avatar of Mike Dias
Mike Dias asked on
6 Comments1 Solution1641 ViewsLast Modified:
I need to silently access a machine on the domain, to generate an "access" report, by date (recent to oldest) that references the following Event IDs:

7001 = 'Logon'
7002 = 'Logoff'
4800 = 'Lock'
4801 = 'UnLock'

I found the following script, however I need it amended to include 'Lock' and 'Unlock' Event IDs.

On the same request, I also need a copy of the XML saves as a txt files.

function get-logonhistory{
Param (
 [string]$Computer = (Read-Host Remote computer name),
 [int]$Days = 10
 $Result = @()
 Write-Host "Gathering Event Logs, this can take awhile..."
 $ELogs = Get-EventLog System -Source Microsoft-Windows-WinLogon -After (Get-Date).AddDays(-$Days) -ComputerName $Computer
 If ($ELogs)
 { Write-Host "Processing..."
 ForEach ($Log in $ELogs)
 { If ($Log.InstanceId -eq 7001)
   { $ET = "Logon"
   ElseIf ($Log.InstanceId -eq 7002)
   { $ET = "Logoff"
   { Continue
   $Result += New-Object PSObject -Property @{
    Time = $Log.TimeWritten
    'Event Type' = $ET
    User = (New-Object System.Security.Principal.SecurityIdentifier $Log.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])
 $Result | Select Time,"Event Type",User | Sort Time -Descending | Format-Table

 Write-Host "Done."
 { Write-Host "Problem with $Computer."
 Write-Host "If you see a 'Network Path not found' error, try starting the Remote Registry service on that computer."
 Write-Host "Or there are no logon/logoff events (XP requires auditing be turned on)"

get-logonhistory -Computer "COMPUTERNAME" -Days "7"
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros