1

这个线程让我开始得很好,但现在我需要更多帮助

我正在尝试遍历我的 serverlist.txt 文件,并将 Get-EventLog 的结果传递给 Out-GridView,然后传递给 .csv 文件。我有这个工作,但我必须选择 GridView 窗口中的所有记录,然后为每个服务器单击“确定”。

所以,我想在循环外创建一个 $sys 变量,进入,将结果附加到每个服务器的该变量,然后退出循环并将 $sys 传递给 Grid-view。

我的困惑来自代码中的变量声明、类型、附加和放置......

我现在只是在学习 PS,所以这对你来说可能有点基础:)

此代码有效...需要在正确的位置添加变量idea:

#Drop the existing files
Remove-Item C:\system.csv

# 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:\ServerList.txt"; 

#Declare $sys here ??

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

{        
 if(Test-Connection $computer -Quiet -Count 1)
        {
   Try {
        # $sys = 
        Get-EventLog -ComputerName $computer -LogName System -EntryType "Error","Warning" -After (Get-Date).Adddays(-7) `
        | Select-Object -Property machineName, EntryType, EventID, Source, TimeGenerated, Message `
        | Out-GridView -PassThru | Export-Csv C:\System.csv -NoTypeInformation -Append;
       } 
   Catch 
       {
       Write-Verbose "Error $($error[0]) encountered when attempting to get events from  $computer"
       }
       } 
   else {
         Write-Verbose "Failed to connect to $computer"
        }


}
# $sys | Out-GridView....etc.

谢谢!

凯文3NF

4

1 回答 1

0

为了解决这个问题,我使用了来自多个评论的建议:

$sys = @() (循环外)

$sys += Get-EventLog(在循环内)

$系统 | Export-Csv(循环后发送到 .csv)

我什至写了整个博客,包括我经历的所有各种学习迭代:http: //dallasdbas.com/getting-to-know-powershell-from-an-old-dba/

感谢所有帮助。这给了我一个框架,我将在需要时继续在这些服务器上使用。

凯文3NF

于 2016-12-14T16:37:48.960 回答