Dol*_*kle 5 powershell login windows-7
所以我一直在研究一个 Powershell 脚本,它需要检测自上次重启以来是否发生了交互式登录。我可以在启动任务之前强制系统重新启动,但我想为脚本添加一些智能。
注意事项:
我能够获得上次系统重新启动的时间:
$date = Get-WmiObject Win32_OperatingSystem | %{$_.LastBootUpTime}
$RebootTime = [System.DateTime]::ParseExact($date.split(".")[0],'yyyyMMddHHmmss',$null)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?提前致谢
如果您决定使用 WMI,则可以变得LastBootUpTime更简洁:
PS C:\> $wmi = Get-WmiObject -Class Win32_OperatingSystem
PS C:\> $rebootTime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
PS C:\> $rebootTime
Tuesday, May 24, 2011 3:18:28 PM
Run Code Online (Sandbox Code Playgroud)
考虑到这一点,我们搜索Security事件日志,因为$rebootTime,最近的成功的eventID4624包含Logon Type 2- 交互式登录:
PS C:\> $entry = Get-EventLog -After $rebootTime -LogName Security | Where-Object {($_.EventID -eq '4624') -and ($_.EntryType -eq 'SuccessAudit') -and ($_.Message | Select-String "Logon Type:\t\t\t2")} | Select-Object -First 1
PS C:\> $lastLogon = $entry.TimeGenerated
PS C:\> $lastLogon
Tuesday, May 24, 2011 3:19:34 PM
Run Code Online (Sandbox Code Playgroud)
然后,快速比较一下:
PS C:\> $lastLogon -gt $rebootTime
True
Run Code Online (Sandbox Code Playgroud)
上述代码可以转储到脚本中和/或在远程计算机上执行。我仅以交互方式运行命令来演示示例输出。