Reputation: 676
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?
Upvotes: 0
Views: 864
Reputation: 52480
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...
}
}
Upvotes: 4