So I've been working on a Powershell script that needs to detect if an interactive logon has happened since the last reboot. I can force the system to restart before I launch the task, but I'd like to add some intelligence to the script.
Caveats:
- Must use Powershell.
- Can't require special power packs or addins.
- Can't use Active directory commands. (No get-qaduser)
I am able to get the last time the system rebooted already:
$date = Get-WmiObject Win32_OperatingSystem | %{$_.LastBootUpTime}
$RebootTime = [System.DateTime]::ParseExact($date.split(".")[0],'yyyyMMddHHmmss',$null)
Any ideas? Thanks in advance
If you're set on using WMI, you can get
LastBootUpTime
a bit cleaner:Taking that, we search the
Security
Event Log, since$rebootTime
, for the most recent, successful, eventID4624
containingLogon Type 2
-- Interactive Logon:Then, a quick compare:
The above code can be dumped into a script and/or executed on a remote computer. I only ran the commands interactively to demonstrate the example output.