powershell exe调用在某些shell实例上失败,而在其他实例上失败

powershell exe调用在某些shell实例上失败,而在其他实例上失败,shell,powershell,output,exe,Shell,Powershell,Output,Exe,我试图通过powershell脚本运行一些基本的Win32可执行文件,但奇怪的是,这些可执行文件没有输出。经过大量调查,我发现: 1) 可执行文件以空输出静默失败。外壳上不显示任何文本。将输出管道化到变量会导致该变量为NULL 2) 出现此问题时,可执行文件似乎根本没有运行。我制作了一个虚拟可执行文件,它写入stdout、stderr,并使用fstream创建两个文件。调用exe时,这三个输出都不会发生 3) 这只影响powershell的某些实例。一个ISE实例和一个powershell实例受

我试图通过powershell脚本运行一些基本的Win32可执行文件,但奇怪的是,这些可执行文件没有输出。经过大量调查,我发现:

1) 可执行文件以空输出静默失败。外壳上不显示任何文本。将输出管道化到变量会导致该变量为NULL

2) 出现此问题时,可执行文件似乎根本没有运行。我制作了一个虚拟可执行文件,它写入stdout、stderr,并使用fstream创建两个文件。调用exe时,这三个输出都不会发生

3) 这只影响powershell的某些实例。一个ISE实例和一个powershell实例受到影响。它们都是在同一天开张的。第二天打开的新实例正确运行可执行文件,并按预期在shell上显示stdout输出。受影响的实例未以管理员身份运行。以管理员身份运行了一些新shell:所有shell的行为都正常

4) 这已经使用两个可执行文件进行了测试和验证。两者都是在VisualStudio2010中构建的Win32控制台应用程序。两者都写入标准输出、标准输出和文件

5) 在受影响的shell中对输出进行管道和重定向的所有尝试都会失败,因为没有输出。下面的例子

6) 使用带有可执行文件名的Invoke项会短暂弹出另一个控制台窗口,但捕获该输出的任何尝试都失败了

7) 我比较了两个shell的环境变量,没有区别

8) 可执行文件存在,并且在两个shell中,如果使用无效的名称或路径,则会出现通常的“未找到文件”错误。只有当可执行文件存在时,才会发生不一致的行为

我已经没有什么可以尝试的了,谷歌也推出了很多关于重定向输出的文章,但我从未见过有人提到过可执行文件只是默默地失败了

正常外壳上的行为:

PS C:\Users\Foo\dev\Testing\OutputTest\Release> dir


    Directory: C:\Users\Foo\dev\Testing\OutputTest\Release


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        12/13/2013  10:34 AM      20992 OutputTest.exe
-a---        12/13/2013  10:34 AM     543744 OutputTest.pdb


PS C:\Users\Foo\dev\Testing\OutputTest\Release> .\OutputTest.exe
Output to stdout.
Output to stderr.
Wrote to file 1.
Wrote to file 1.
PS C:\Users\Foo\dev\Testing\OutputTest\Release> & ".\OutputTest.exe"
Output to stdout.
Output to stderr.
Wrote to file 1.
Wrote to file 1.
PS C:\Users\Foo\dev\Testing\OutputTest\Release> $output = cmd /c OutputTest.exe 2`>`&1
PS C:\Users\Foo\dev\Testing\OutputTest\Release> $output
Output to stdout.
Output to stderr.
Wrote to file 1.
Wrote to file 1.
PS C:\Users\Foo\dev\Testing\OutputTest\Release> $output = $(cmd /c OutputTest.exe 2`>`&1)
PS C:\Users\Foo\dev\Testing\OutputTest\Release> $output
Output to stdout.
Output to stderr.
Wrote to file 1.
Wrote to file 1.
PS C:\Users\Foo\dev\Testing\OutputTest\Release>
受影响外壳上的行为:

PS C:\Users\Foo\dev\Testing\OutputTest\Release> dir


    Directory: C:\Users\Foo\dev\Testing\OutputTest\Release


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        12/13/2013  10:34 AM      20992 OutputTest.exe
-a---        12/13/2013  10:34 AM     543744 OutputTest.pdb


PS C:\Users\Foo\dev\Testing\OutputTest\Release> .\OutputTest.exe
PS C:\Users\Foo\dev\Testing\OutputTest\Release> & ".\OutputTest.exe"
PS C:\Users\Foo\dev\Testing\OutputTest\Release> $output = cmd /c OutputTest.exe 2`>`&1
PS C:\Users\Foo\dev\Testing\OutputTest\Release> $output
PS C:\Users\Foo\dev\Testing\OutputTest\Release> $output = $(cmd /c OutputTest.exe 2`>`&1)
PS C:\Users\Foo\dev\Testing\OutputTest\Release> $output
PS C:\Users\Foo\dev\Testing\OutputTest\Release>
在上述所有“受影响”的情况下,不会将任何文件写入磁盘。 任何人我假设这是一件简单而愚蠢的事情,但除非我能期望前任能够始终如一地运行,否则powershell对我毫无用处。我做错了什么

系统详情:

Windows 7 Pro Sp1

PS C:\Users\Foo\dev\Testing\OutputTest\Release> $PSVersionTable.psversion

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1
OutputTest.exe的源

#include "stdafx.h"
#include <iostream>
#include <fstream>

int main(int argc, char* argv[])
{
    std::cout << "Output to stdout.\n";
    std::cerr << "Output to stderr.\n";

    std::fstream outfile1;
    std::fstream outfile2;

    outfile1.open("C:\\temp\\outputTest1.txt",std::fstream::out);
    outfile2.open("outputTest2.txt",std::fstream::out);

    if(outfile1.is_open()){
        outfile1 << "Output to fstream 1.\n";
        std::cout << "Wrote to file 1.\n";
        outfile1.close();
    }

    if(outfile2.is_open()){
        outfile2 << "Output to fstream 2.\n";
        std::cout << "Wrote to file 1.\n";
        outfile2.close();
    }

    return 0;
}
#包括“stdafx.h”
#包括
#包括
int main(int argc,char*argv[])
{

std::难道我经常会遇到PowerShell拒绝运行EXE文件(包括记事本)的情况吗?我从来没有追根究底——只是重新启动PowerShell会话。@RogerLipscombe很高兴知道我并不是唯一一个遇到这种情况的人,但如果这是不可避免的,它会使PowerShell对我的目的变得更糟糕:自动化应用程序测试。您是否尝试过使用?据我所知,这是一种非常可靠的方法。