views:

85

answers:

1

Hello! There's already question addressing my issue (http://stackoverflow.com/questions/563600/can-i-get-to-work-in-powershell), but with one difference. I need an OUTPUT from both commands. See, if I just run:

(command1 -arg1 -arg2) -and (command2 -arg1)

I won't see any output, but stderr messages. And, as expected, just typing:

command1 -arg1 -arg2 -and command2 -arg1 

Gives syntax error.

+3  A: 

Try this:

$(command -arg1 -arg2 | Out-Host;$?) -and $(command2 -arg1 | Out-Host;$?)

The $() is a subexpression allowing you to specify multiple statements within including a pipeline. Then execute the command and pipe to Out-Host so you can see it. Then in the next statement (the actual output of the subexpression) output $? i.e. the last command's success result.

BTW the $? works fine for native commands (console exe's) but for cmdlets it leaves something to be desired. That is, $? only seems to return $false when a cmdlet encounters a terminating error. Seems like $? needs at least three states (failed, succeeded and partially succeeded). So if you're using cmdlets, this works better:

$(command -arg1 -arg2 -ev err | Out-Host;!$err) -and 
$(command -arg1 -ev err | Out-Host;!$err)

This kind of blows still. Perhaps something like this would be better:

function ExecuteUntilError([scriptblock[]]$Scriptblock)
{
    foreach ($sb in $scriptblock)
    {
        $prevErr = $error[0]
        . $sb
        if ($error[0] -ne $prevErr) { break }
    }
}

ExecuteUntilError {command -arg1 -arg2},{command2-arg1}
Keith Hill
Andy
Keith Hill