Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 关闭主机文件句柄时的powershell行为_Windows_Perl_Powershell_File Io_Powershell 2.0 - Fatal编程技术网

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

Windows 关闭主机文件句柄时的powershell行为,windows,perl,powershell,file-io,powershell-2.0,Windows,Perl,Powershell,File Io,Powershell 2.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 (S

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

假设我有以下简单的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!
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
aoute.txt:

yay1
yay3
yay5
yay7
done!
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
然后生成一个标准输出文件,其中包含所有输出:

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,要么是exec如何在Windows上工作;这有关系吗?您希望发生什么?我希望清楚地了解shell重定向如何影响powershell。我想知道什么时候期望它兑现,什么时候期望它正常运行。Powershell与其他编程语言完全不同。当需要编写一些PS时,我真的必须将自己隔离在powershell中。PS程序是一种完全不同的动物,您所谓的输出重定向实际上是管道操作。说它是“马车”并不准确,你只是在做两件表面上看起来完全相同的完全不同的事情。苹果和桔子。