【问题标题】:Remote query of multiple servers using Get-EventLog in PowerShell在 PowerShell 中使用 Get-EventLog 远程查询多台服务器
【发布时间】:2014-08-14 03:01:27
【问题描述】:

我正在为早上的例行程序编写一个 PowerShell 脚本。我只想从远程服务器列表中提取警告和错误。目前我只收到警告或错误。我不确定如何检索两者。一旦我提取信息,它不包括服务器。我的查询如下:

# SERVER LIST PROPERTIES
# Get computer list to check disk space. This is just a plain text file with the servers listed out.
 $computers = Get-Content "C:\MorningChecks.txt"; 

# QUERY COMPUTER SYSTEM EVENT LOG
foreach($computer in $computers)
{
 Get-EventLog -LogName System -EntryType Error -After (Get-Date).Adddays(-1) | Format-Table -Wrap ;
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    -EntryType 参数接受用于过滤的字符串数组。

    所以要仅过滤错误,您将使用参数:

    Get-EventLog -LogName System -EntryType Error -After (Get-Date).Adddays(-1) 
    

    过滤错误和警告:

    Get-EventLog -LogName System -EntryType "Error","Warning" -After (Get-Date).Adddays(-1) 
    

    要获取计算机名称,必须将其添加到Format-Table 末尾的-Property 参数中:

    Format-Table -Wrap -Property MachineName, Index, TimeGenerated, EntryType, Source, InstanceID, Message -AutoSize
    

    -- 编辑

    要回答关于显示您自己机器的机器名称的问题,这是因为当您运行Get-EventLog 时,您只是为您的本地机器运行它。您忘记在 foreach 循环中指定 -ComputerName 参数。您的 foreach 循环应如下所示:

    foreach($computer in $computers)
    {
     Get-EventLog -ComputerName $computer -LogName System -EntryType "Error","Warning" -After (Get-Date).Adddays(-1) | Format-Table -Wrap -Property MachineName, Index, TimeGenerated, EntryType, Source, InstanceID, Message -AutoSize ;
    }
    

    【讨论】:

    • 非常感谢。我仍然有一个问题。 MachineName 返回我的本地主机名。不是远程主机名。
    猜你喜欢
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    相关资源
    最近更新 更多