将事件日志写入文件,powershell

时间:2012-03-05 14:17:22

标签: powershell powershell-v2.0

所以说你想从lan上的所有桌面查看类型应用程序的事件日志中的最后三件事。我的问题是,$ a下面创建一个数组(我认为),我想将其写入文件。现在它“有效”,但每台机器只吐出几个空白行。如果$ msg =被注释掉,我得到这个“异常调用”加入“with”2“参数:”值不能为空。“

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

1 个答案:

答案 0 :(得分: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
相关问题