I would like to script a dcdiag test to alert me if it finds any errors. I thought I may able to do this in powershell by...

$test = dcdiag 2>$err

I don't have any errors from dcdiag at the moment so I couldn't test that directly, but I wrote another ps script to throw an exception hoping I could test this method using that script. This didn't work using the method above so I opted for

try{$test = dcdiag}catch{$err = $_.Exception.Message}

It worked for my test case, but I don't know if this will pick up stderr from dcdiag.

I would like to know how I should best achieve stderr redirect to a variable in powershell given I would like to use it with dcdiag?

link|improve this question

feedback

1 Answer

up vote 3 down vote accepted

try..catch wouldn't help in this case.

You might want to do:

$test = dcdiag 2>&1
$err = $test | ?{$_.gettype().Name -eq "ErrorRecord"}
if($err){
#error has occurred
}
link|improve this answer
Ah, I didn't say in the original question; is it possible to keep both stdout and stderr? i.e $test = stdout and $err = stderr? – Ablue Dec 14 '11 at 2:14
1  
@Ablue - $test will have both, that is why I am filtering out the error to $err. – manojlds Dec 14 '11 at 2:15
Thanks a million :) – Ablue Dec 14 '11 at 2:17
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.