在一次运行中,是否可以将stdout从外部程序重定向到外部程序中的变量和stderr?
原文链接:https://www.f2er.com/windows/372303.html例如:
$global:ERRORS = @(); $global:PROGERR = @(); function test(){ # Can we redirect errors to $PROGERR here,leaving stdout for $OUTPUT? $OUTPUT = (& myprogram.exe 'argv[0]','argv[1]'); if ( $OUTPUT | select-string -Pattern "foo" ) { # do stuff } else { $global:ERRORS += "test(): oh noes! 'foo' missing!"; } } test; if ( @($global:ERRORS).length -gt 0 ) { Write-Host "Script specific error occurred"; foreach ( $err in $global:ERRORS ) { $host.ui.WriteErrorLine("err: $err"); } } else { Write-Host "Script ran fine!"; } if ( @($global:PROGERR).length -gt 0 ) { # do stuff } else { Write-Host "External program ran fine!"; }
一个沉闷的例子,但我想知道是否可能?