多个服务器的 Powershell Get_EventLog [英] Powershell Get_EventLog for multiple servers
问题描述
我是一名 SQL DBA,n00b 到 Powershell,目前负责系统管理员职责
I'm a SQL DBA, n00b to Powershell, tasked with sysadmin duties at the moment
我需要在我的服务器上查询错误日志以获取错误和警告.
I need to query error logs across my servers for Errors and Warnings.
使用我自己的 Google-fu 和来自 的帮助这个线程我能够让它工作:
Using my own Google-fu and help from this thread I was able to get this working:
$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 表、results.txt 等中.
What I am missing at the moment is how to trap for a server in my .txt file that is offline, ignore it and move to the next one. After that, I will be working to dump all of this to a SQL table, results.txt, etc.
感谢您的帮助:)]
Kevin3NF
推荐答案
有几种不同的方法可以解决这个问题,但我建议将 Test-Connection
与 Try/Catch
,Test-Connection
将使您只尝试查询响应 ping 的服务器,然后 Try/Catch
将处理任何其他可能出现的错误.
There are a few different ways to handle this but I'd recommend combining a Test-Connection
with a Try/Catch
, the Test-Connection
will make it so that you only try to query the servers which respond to ping and then the Try/Catch
will handle any other errors that may come along.
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"
}
}
这篇关于多个服务器的 Powershell Get_EventLog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!