On Aug 11, 9:41*pm, "Steve Schofield" <st...@xxxxxx> wrote:
Quote:
> I'm trying to write a powershell script that does the following
>
> 1) Selects from a database of computers (name of computer and IP address is
> in the table)
> 2) Loops through the results querying the Win32_NetworkAdapterConfiguration
> per machine.
> 3) Logs information to the stringbuilder variable
> 4) Logs errors if a machine isn't available.
>
> I've tried a couple examples on Try/Catch and TRAP exception. *The
> powershell script bombs on a machine not available.
>
> Here is the script. * I can do this in VBS, but I wanted to see if it's
> possible in Powershell 1.0.
>
> Hope someone can point me in the right direction when it comes to error
> trapping.
>
Shay is right, of course, but I thought I'd add that the reason why
your trap didn't catch the error is because even though it's a really
long timeout that makes it seem like the script is bombing, it's
actually just taking a really long time to time out and then returning
with a non-terminating error. These errors don't actually throw
exceptions, and to control the way your script behaves for these
errors you'll want to use the ubiquitous -ea (errorAction) switch or
the $ErrorActionPreference preference variable to force PowerShell to
Continue or Stop or Inquire or ContinueSilently when it hits an error.
In this particular case, though, it's not really all that helpful
because you still have that really long timeout for every server that
isn't online, so you have no choice but to ping it first like Shay
suggested.