windows - powerhell adfærd når host filhåndtaget er lukket

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg leder efter en forklaring (helst dokumentation) af følgende vanvid.


Sig jeg har følgende simple Perl script:


use strict;
use warnings;

my $output = "C:\Temp\aout.txt";
my $outpute = "C:\Temp\aoute.txt";
my $command = "powershell C:\test.ps1";

close STDOUT;
close STDERR;
if ( (my $pid = fork()) == 0 ) {
    open (STDOUT,">>$output") or die "cannot open $output as stdout: $!";
    open (STDERR,">>$outpute") or die "cannot open $outpute as stderr: $!";
    exec $command or die "couldn't exec $command: $!";
} else {
    my $ret = waitpid($pid,0);
}


Og at Powershell-scriptet indeholder:


write-output "yay1";
write-error "nay2";
write-output "yay3";
write-error "nay4";
write-output "yay5";
write-error "nay6";
write-output "yay7";
write-error "nay8";
write-host "done!";


Hvis jeg kører Perl-scriptet, producerer det to filer, som forventet, med følgende output:


aout.txt:


yay1
yay3
yay5
yay7
done!


aoute.txt:


C:	est.ps1 : nay2
At line:1 char:41
+ C:	est.ps1 <<<< 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1

C:	est.ps1 : nay4
At line:1 char:41
+ C:	est.ps1 <<<< 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1

C:	est.ps1 : nay6
At line:1 char:41
+ C:	est.ps1 <<<< 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1

C:	est.ps1 : nay8
At line:1 char:41
+ C:	est.ps1 <<<< 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1


Hvis jeg ændrer Perl-scriptet for at holde STDERR lukket, inden jeg gennemfører Powershell:


use strict;
use warnings;

my $output = "C:\Temp\aout.txt";
my $command = "powershell C:\test.ps1";

close STDOUT;
close STDERR;
if ( (my $pid = fork()) == 0 ) {
    open (STDOUT,">>$output") or die "cannot open $output as stdout: $!";
    exec $command or die "couldn't exec $command: $!";
} else {
    my $ret = waitpid($pid,0);
}


Derefter producerer den en STDOUT-fil med alle af output:


yay1
C:\test.ps1 : nay2
At line:1 char:41
+ C:\test.ps1 <<<< 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1

yay3
C:\test.ps1 : nay4
At line:1 char:41
+ C:\test.ps1 <<<< 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1

yay5
C:\test.ps1 : nay6
At line:1 char:41
+ C:\test.ps1 <<<< 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1

yay7
C:\test.ps1 : nay8
At line:1 char:41
+ C:\test.ps1 <<<< 
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1

done!


Er Powershell virkelig klar over, at STDERR er lukket, og gør en ren omdirigering til STDOUT? Eller er det kommandolinjekonsollen? Eller er det Perl?


Til reference er dette på Windows Server 2003 x64 SP2 med:


C:>perl --version

This is perl, v5.8.8 built for MSWin32-x64-multi-thread
(with 33 registered patches, see perl -V for more detail)

Copyright 1987-2006, Larry Wall

Binary build 819 [267479] provided by ActiveState http://www.ActiveState.com
Built Aug 27 2006 22:13:23

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.


C:>powershell get-host


Name             : ConsoleHost
Version          : 2.0
InstanceId       : 1ac67afa-f020-414c-b47b-031bdc8cc703
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace


og dette er en del af en løbende indsats relateret til dette og dette.

Bedste reference