Powershell获取关机事件


支持所有PS版本

Windows所有日志中所有关机事件都在系统日志中。你可以从那里获取到这些信息。

这里有一个函数能获得指定的事件日志,从替换后的字符串数组读取相关信息并返回一个关机信息的对象。

function Get-ShutdownInfo
{
  
  Get-EventLog -LogName system -InstanceId 2147484722 -Source user32 |
  ForEach-Object {
    
    $result = 'dummy' | Select-Object -Property ComputerName, TimeWritten, User, Reason, Action, Executable
    
    $result.TimeWritten = $_.TimeWritten
    $result.User = $_.ReplacementStrings[6]
    $result.Reason = $_.ReplacementStrings[2]
    $result.Action = $_.ReplacementStrings[4]
    $result.Executable = Split-Path -Path $_.ReplacementStrings[0] -Leaf
    $result.ComputerName = $_.MachineName
    
    $result 
  }
}

现在可以轻松的查看关机事件了:

PS> Get-ShutdownInfo |  Out-GridView

原文地址:Getting Shutdown Information

本文链接: https://www.pstips.net/getting-shutdown-information.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注