Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 8 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
}
share|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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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