Author Posts

January 1, 2012 at 12:00 am

by turbomcp at 2012-09-10 11:30:14

hi there
another quick question
i have this script:
#requires -version 2.0
< #
author: demo
date: 10/09/2012
purpose: Retrieve event id’s from multiple machines and add to a CSV file.
#>
Try
{
$servers=get-content r:\scripts\servers.txt
$date=(Get-Date).AddDays(-7)
foreach ($server in $servers)
{
if (test-connection $server -quiet)
{
$get1+=get-eventlog -logname application -cn $server -after $date | ?{$_.eventid -eq "902" -and $_.entrytype -eq "error"} | select MachineName,EventID,EntryType,Message
}
}
if ($get1)
{$get1 | export-csv r:\scripts\app.csv -notypeinformation}
else
{"No matching system log events found..."}
}
Catch
{
"An error occurred"
}

so im looking for specific events on multiple machines
two things im missing is some kind of progress bar to show me whats the script doing and where did it get to and the second thing is i noticed if i changed some stuff i get "an error has accured" and i dont have any clue what error on what server(basicly the same thing as no.1:))

how complicate is it to add a status bar with some ino?

Thanks in advance

by DonJ at 2012-09-10 15:19:59

Write-Progress is pretty easy, but if you want progress "statements" rather than a "progress bar" per se, use Write-Verbose instead. It is, for me, a lot easier to track "see where it got to" type stuff.

If you add

[CmdletBinding()] Param()

To the top of your script, it'll enable the -Verbose switch, which turns on Write-Verbose's output.

by coderaven at 2012-09-10 16:33:09

Getting error data from the catch block use the $_ variable – $_.Exception.

by DonJ at 2012-09-11 07:37:47

Ugh, I hate relying on $_ for that – put the -ErrorVariable parameter on the failing command and capture the error into an explicit variable. Safer, and easier to read.