关闭主机文件句柄时的PowerShell行为

时间:2012-02-13 18:53:53

标签: windows perl powershell file-io powershell-v2.0

我正在寻找以下疯狂的解释(最好是文档)。

假设我有以下简单的Perl脚本:

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);
}

Powershell脚本包含:

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!";

如果我运行Perl脚本,它会按预期生成两个文件,并带有以下输出:

aout.txt:

yay1
yay3
yay5
yay7
done!

aoute.txt:

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

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

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

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

如果在执行Powershell之前修改Perl脚本以保持STDERR关闭:

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);
}

然后它生成一个STDOUT文件,输出的 all

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!

Powershell真的意识到STDERR是关闭的,并且正在干净地重定向到STDOUT吗?或者它是命令行控制台?或者是Perl?

作为参考,这是在Windows Server 2003 x64 SP2上:

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

这是与thisthis相关的持续努力的一部分。

0 个答案:

没有答案