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?

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...
 }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.