2012-03-05 149 views
2

所以,假设您想从LAN上的所有桌面上查看类型应用程序的事件日志中的最后3件事。我的问题是,下面的$ a创建一个数组(我认为),并且我想把它写到一个文件中。现在它“工作”,但只是每台机器吐出几行空白行。如果$味精=被注释掉了,我得到这个“异常调用‘加入’与‘2’参数(S):‘值不能为空’将事件日志写入文件,powershell

$servers = "machine1, "machine2" 
$date = (get-date).ToString('yyyyMMdd') 
$file = New-Item -type file "c:\temp\$date-test1.txt" -Force 
$msg = "" 
foreach($server in $servers){ 
    Write-Host "Connect to $server..." 
    add-content $file "$server $date" 
    $a = Get-EventLog -Logname application -ComputerName $server -EntryType warning -newest 3 | 
    Format-Table -Wrap -Property Source,Message -Autosize 
    foreach($element in $a) 
    {[string]::join($element, $msg)} 
    Write-Host $msg 
    #add-content $file $msg 
} 

回答

3

你正在考虑的文本,在没有对象。管道。不要试图解析,用绳子周围加入或混乱。

$servers = "machine1, "machine2" 
$date = (get-date).ToString('yyyyMMdd') 
$file = New-Item -type file "c:\temp\$date-test1.txt" -Force 
Get-EventLog -log Application -Computer $servers -entry Warning -newest 3 | Select MachineName,Source,Message | out-file $file 
+0

首先感谢您的回答是99%完美的答复。加入这个“物业资源,信息-AutoSize”允许满事件消息,上面只给你约2/3的第一句话。我添加/更改为:Get-EventLog日志应用程序 - 计算机$服务器 - 入口警告-newest 3 | 选择计算机名称,来源,消息| 格式 - 表 - 包装 - 属性MachineName,源,消息-Autosize | out-file $文件 – user1249998 2012-03-05 21:05:40

+0

您现在就明白了。也就是说,我更简单的方法的一个缺点是,它假定了一小组计算机,并且它们都在线。更健壮的方法需要错误处理,也许尝试/捕捉或甚至写入。在这些情况下,ForEach构造可能会更有意义,但我仍然会放弃使用字符串并将输出管道输出到Out-File。 – 2012-03-05 21:27:33