用于多台服务器的 Powershell Get_EventLog

Powershell Get_EventLog for multiple servers

我是一名 SQL DBA,n00b 到 Powershell,目前负责系统管理员职责

我需要跨我的服务器查询错误日志以获取错误和警告。

使用我自己的 Google-fu 和来自 this 线程的帮助,我能够让它工作:

 $computers = Get-Content "C:\Servers\ServerList_Short.txt"; 

# QUERY COMPUTER SYSTEM EVENT LOG
foreach($computer in $computers)

{        
     Get-EventLog -ComputerName $computer -LogName System -EntryType "Error","Warning" -After (Get-Date).Adddays(-7) `
     | Select-Object -Property machineName, EntryType, EventID, TimeGenerated, Message `
     | Format-Table -Property MachineName, TimeGenerated, EntryType, Source,  Message -AutoSize ;

}

我现在缺少的是如何在我的 .txt 文件中捕获离线的服务器,忽略它并移动到下一个。之后,我将努力将所有这些转储到 SQL table、results.txt 等

感谢您的帮助:)]

凯文3NF

有几种不同的方法来处理这个问题,但我建议将 Test-ConnectionTry/Catch 结合使用,Test-Connection 会成功,这样您就可以只尝试查询响应 ping 然后 Try/Catch 的服务器将处理可能出现的任何其他错误。

foreach($computer in $computers)

{        
 if(Test-Connection $computer -Quiet -Count 1){
    Try {
        Get-EventLog -ComputerName $computer -LogName System -EntryType "Error","Warning" -After (Get-Date).Adddays(-7) `
     | Select-Object -Property machineName, EntryType, EventID, TimeGenerated, Message `
     | Format-Table -Property MachineName, TimeGenerated, EntryType, Source,  Message -AutoSize
    } Catch {
        Write-Verbose "Error $($error[0]) encountered when attempting to get events from  $computer"
    }
 } else {
    Write-Verbose "Failed to connect to $computer"
 }

}