0

I'm printing to a file from within a try/catch block. If an exception is caught, I don't want to print. So I think I'm approaching it wrong. Code:

foreach ($strComputer in $arrComputers){
Try {
"Computer Name:" + $strComputer | out-file "somefile.txt" -append
...something involving Get-WmiObject...
} Catch [System.UnauthorizedAccessException] {
...handle error...
}
}

When that error is caught, I would prefer that "Computer Name:" + $strComputer not print out, as if the try block never happened. How do I accomplish this?

asked Nov 10, 2011 at 21:37

1 Answer 1

4

Something tells me this is too simple to work for you, but umm, move the out-file statement after the WMI work?

foreach ($strComputer in $arrComputers){
 try {
 ...something involving Get-WmiObject...
 "Computer Name:" + $strComputer | out-file "somefile.txt" -append
 } Catch [System.UnauthorizedAccessException] {
 ...handle error...
 }
}
answered Nov 10, 2011 at 22:10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.