Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Having problems grasping error handling.

$
0
0

Hello,

I am new to powershell, scripting, coding etc. Went through the gambit of jumpstart video's and have been reading through online articles/examples and have been making steady progress but I am now stumped at the behavior I am seeing.

Here is what I am doing (this is not going to be the full code.. just the process)

$ErrorActionPreference= "Stop"
Foreach ($c in $computer) { Try{} catch{} finally{} }

In try{} I am using test-connection -computername $c.
In catch{} Right now I am just doing a write-output $Error[0].Exception.Message (to get the value).
in finally{} I am running the rest of my code (Which checks for a registry attribute, creates it if it doesnt exist, or sets the value if it does already exist)

What I am seeing when I specify one computer:

    If try{} does not error it skips catch and finally runs.
    If try{} errors (due to inability to resolve/connect) it goes to catch and finally does not run.

What I am seeing when I specify two or more computers (-ComputerName Works, Fails)

    If try{} does not error it skips catch and finally runs.
    If try{} errors (due to inability to resolve/connect) it goes to catch and finally does not run for either Works or Fails.

Why does it stop the whole entire script even through I thought it would be iterating through the script (Foreach) computer specified?


Viewing all articles
Browse latest Browse all 13067

Trending Articles