2

So say you want to look at the last 3 thing in the eventlog for type application, from all the desktops on the lan. my problem is, the $a below makes an array (i think), and i want to write that to a file. right now it "works" but just spits out a few blank lines per machine. if $msg = is commented out, i get this "Exception calling "Join" with "2" argument(s): "Value cannot be null."

$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 Answer 1

4

You are thinking about text, not objects in the pipeline. Don't try to parse, join or mess around with strings.

$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
Sign up to request clarification or add additional context in comments.

2 Comments

first off thanks for the reply that answer is 99% perfect. adding in this "-Property Source,Message -Autosize" allows the full event message, above only gives you about 2/3 of the first sentence. i added/changed to this:Get-EventLog -log Application -Computer $servers -entry Warning -newest 3 | Select MachineName,Source,Message | Format-Table -Wrap -Property MachineName,Source,Message -Autosize | out-file $file
You got the idea now. That said, the one drawback to my simpler approach is that it presumes a short list of computers and that they are all online. A more robust approach would need error handling, perhaps try/Catch or even Write-Progess. In these situations a ForEach construct might make more sense but I'd still forego messing around with strings and simply pipe output to Out-File.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.