write progress

by turbomcp at 2012-09-10 11:46:58

Hi
i have this code that goes and looks for warnings and error events but i want to add a progress bar so i know whats going on.how hard is it?:slight_smile:
#requires -version 2.0
<#

#>
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 | ?{$.entrytype -eq "warning" -or $.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"
}



would this be the right way to do it?
#requires -version 2.0
<#
author: newbie

#>
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 | ?{$.entrytype -eq "warning" -or $.entrytype -eq "error"} | select MachineName,EventID,EntryType,Message
For($i = 1; $i -le $get1.count; $i++)

{ Write-Progress -Activity "gettting info from $server" -status "Found $i items on $server" -percentComplete ($i / $get1.count100)}
}
}
if ($get1)
{$get1 | export-csv r:\scripts\app.csv -notypeinformation}
else
{"No matching system log events found…"}
}
Catch
{
"An error occurred"
}
by DonJ at 2012-09-10 15:26:28
Well… do you like what you see when you run that second variation? If so, that’s the right way to do it.
by turbomcp at 2012-09-10 16:33:57
good one:)
yeah it seems fine to me:)
but who am i, some amature powersheller i just started with this thing ,want sure if thats correct way of doing it:)
by DonJ at 2012-09-11 07:36:57
If it works, it’s right :wink:
by turbomcp at 2012-09-11 08:57:13
another quick question regrading this:
i ran this script in alab with two servers and it seems to work fine, no issues.
when i run it on my production servers(i minimized the servers to 1 for start)
it seems to just hang there and do nothing(no progress bar or anything)
at the end after 5min or so i get:
An error occurred

what can be the cause of this?
how can i know when and where its failing?

Thanks
by DonJ at 2012-09-11 09:22:34
I’d suggest adding verbose output for each step. For example:


# At the top of your script, add $VerbosePreference=‘Continue’ to enable Write-Verbose
Write-Verbose "Starting loop"
foreach ($server in $servers)
{
Write-Verbose "Now on server $server"
if (test-connection $server -quiet)
{
Write-Verbose "Test-Connection successful, getting event log"
$get1+=get-eventlog -logname application -cn $server -after $date | ?{$.entrytype -eq "warning" -or $.entrytype -eq "error"} | select MachineName,EventID,EntryType,Message
Write-Verbose "Finished getting event log"


That way, you’ll know where your script was when the error occurs.
by turbomcp at 2012-09-11 09:48:33
thanks i did that:
#requires -version 2.0
<#
date: 10/09/2012
purpose: Retrieve event id’s from multiple machines and add to a CSV file.
#>
$VerbosePreference='Continue'
Try
{
$servers=get-content r:\scripts\servers.txt
$date=(Get-Date).AddDays(-14)
Write-Verbose "Starting loop"
foreach ($server in $servers)
{Write-Verbose "Now on server $server"
if (test-connection $server -quiet)
{Write-Verbose "Test-Connection successful, getting event log from $server"
$get1+=get-eventlog -logname application -cn $server -after $date | ?{$.eventid -eq "902" -and $.entrytype -eq "error"} | select MachineName,EventID,EntryType,Message
Write-Verbose "Finished getting event log on server $server"
For($i = 1; $i -le $get1.count; $i++)

{ Write-Progress -Activity "gettting info from $server" -status "Found $i items on $server" -percentComplete ($i / $get1.count
100)}
}
}
if ($get1)
{$get1 | export-csv r:\scripts\app.csv -notypeinformation}
else
{"No matching system log events found…"}
}
Catch
{
"An error occurred"
}


and this is the output im getting:
[PS] R:\Scripts>.\events.ps1
VERBOSE: Starting loop
VERBOSE: Now on server server1
VERBOSE: Test-Connection successful, getting event log
VERBOSE: Finished getting event log on server server1
VERBOSE: Now on server
An error occurred
[PS] R:\Scripts>
by DonJ at 2012-09-11 10:13:12
Ok. So, that output tells you what the problem is. Look at the last line of VERBOSE output. Do you see a server name? Nope. So we know that your $server variable is empty. The error is happening because Test-Connection can’t ping a blank server name.

I’m betting your servers.txt file has a blank line in it. Maybe at the end.
by turbomcp at 2012-09-11 10:36:59
sorry, my mistake
i added more than one server(alhough that shouldnt matter)
what is going on is when it reaches the last server it doesnt do the export it just displayes:
An error occurred
ill check the blank line thing
by DonJ at 2012-09-11 10:41:27
You’re getting an error because, at that point, $server is empty. Figure out why that’s the case, and you’ll be able to fix the error.
by turbomcp at 2012-09-11 10:48:59
thanks alot
i just opened that servers.txt file and made sure cursor was on last server and saved
and didnt get the error:)
so now ill add all my servers back and it should be good to go.
Thanks
by turbomcp at 2012-09-21 10:32:27
Hi
any other way to know whats going on at the point where i get "an error has accrued"
this script worked fine last time ,now again it stops randomly at server no.6 or server no.3
so i know its failing on that server when it fails i just don’t know why? because i know it passed the connectivity test for that server,it fails after the test when getting the event.
i even tried to change get $servers from text and now using live get=get-exchangeserver
by DonJ at 2012-09-21 10:38:07
You’re going to have to debug it. Adding verbose output will help - you need to know WHERE it’s failing. You should consider adding error handling, using try…catch blocks, so that you can trap errors and examine them to see what’s happening. I don’t even know at what point your script is failing ;).

If you do want to pursue this further, please start a new post, as we’ve moved pretty far away from the original topic of this one.