windows-7 – 用于查找用户自上次重启后是否已登录的Powershell脚本

所以我一直在研究一个Power shell脚本,需要检测自上次重启以来是否发生了交互式登录.我可以在启动任务之前强制系统重新启动,但我想在脚本中添加一些智能.

注意事项:

>必须使用Powershell.
>不能要求特殊的电源组或插件.
>无法使用Active Directory命令. (没有get-qaduser)

我能够在最后一次系统重新启动时获得:

$date = Get-WmiObject Win32_OperatingSystem | %{$_.LastBootUpTime} 
$Reboottime = [System.DateTime]::ParseExact($date.split(".")[0],'yyyyMMddHHmmss',$null)

有任何想法吗?提前致谢

如果您已开始使用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

考虑到这一点,我们搜索安全事件日志,因为$reboottime,用于包含登录类型2 – 交互式登录的最新成功的eventID 4624:

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,2011 3:19:34 PM

然后,快速比较一下:

PS C:\> $lastlogon -gt $reboottime
True

上述代码可以转储到脚本中和/或在远程计算机上执行.我只是以交互方式运行命令来演示示例输出.

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...