Powershell Get_EventLog多个服务器

我是一名SQL DBA,现在是Powershell的n00b,负责系统pipe理任务

我需要在我的服务器上查询错误日志以查找错误和警告。

使用我自己的Google-fu和从这个线程的帮助,我能够得到这个工作:

$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 ; } 

我现在缺less的是如何陷阱我​​的.txt文件中的服务器脱机,忽略它,并移动到下一个。 之后,我将努力将所有这些转储到SQL表,results.txt等。

谢谢你的帮助 :)]

Kevin3NF

有几种不同的方法来处理这个问题,但是我建议将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" } }