Skip to content

Instantly share code, notes, and snippets.

@win2000b
Created October 16, 2020 15:27
Show Gist options
  • Save win2000b/d75bef6f9ea7022b5b54817e8bc94911 to your computer and use it in GitHub Desktop.
Save win2000b/d75bef6f9ea7022b5b54817e8bc94911 to your computer and use it in GitHub Desktop.
Audit Logon / Logoff PowerShell
Param (
[string]$Computer = (Read-Host Remote computer name),
[int]$Days = 20
)
cls
$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"
}
ElseIf ($Log.InstanceId -eq 7002)
{ $ET = "Logoff"
}
Else
{ 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 | Out-GridView
Write-Host "Done."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment