0

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

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

使用我自己的 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 ;

}

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

谢谢你的帮助 :)]

凯文3NF

4

1 回答 1

2

有几种不同的方法可以处理这个问题,但我建议将 aTest-Connection与 a结合使用Try/CatchTest-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"
 }

}
于 2016-12-02T19:09:22.557 回答