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
SeanFromIT
7141 gold badge10 silver badges18 bronze badges
1 Answer 1
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
x0n
52.7k8 gold badges97 silver badges116 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-bash