2

I'm attempting to run some basic Win32 executables via powershell scripts, and I was mysteriously getting no output from the executables. After much investigation, this is what I have discovered:

1) Executables fail silently with NULL output. No text is shown on the shell. Piping the output to a variable results in that variable being NULL.

2) When this problem occurs, the executable does not appear to be run at all. I made a dummy executable that writes to stdout, stderr, and creates two files using an fstream. None of these three outputs happen when the exe is invoked.

3) This only affects certain instances of the powershell. One ISE instance and one powershell instance are affected. They were both opened on the same day. New instances opened the next day run the executable correctly, showing the stdout output on the shell as expected. Affected instances are not running as administrator. Ran some new shells as administrator: all behaved normally.

4) This has been tested and verified using two executables. Both are Win32 console applications built in Visual Studio 2010. Both write to stdout, stderr and to files.

5) All attempts to pipe and redirect the output in the affected shell fail, because there is no output. Examples below.

6) Using Invoke-Item with the executable name pops up another console window briefly, but any attempt to capture that output has failed.

7) I've compared the environment variables for the two shells, and there's no difference.

8) The executable is present, and in both shells if you use an invalid name or path then you get the usual "file not found" error. The inconsistent behaviour only happens if the executable is present.

I've run out of things to try, and Googling has come up with many articles on redirecting output, but nowhere that I've seen does anyone refer to executables just silently failing.

Behaviour on normal 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>

Behaviour on affected 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
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>

In all of the above "affected" cases, no files are written to disk. Anyone? I'm assuming this is something simple and stupid, but unless I can expect EXEs to run consistently, powershell is of zero use to me. What am I doing wrong?

Details of system:

Windows 7 Pro Sp1

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

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1

Source of the 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;
}

Yes, I see now that I didn't edit the last std::cout to change the number :P

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I've regularly had occasions when PowerShell simply refuses to run EXE files, including notepad. I never got to the bottom of it -- just restart the PowerShell session. – Roger Lipscombe Dec 13 '13 at 20:10
  • @RogerLipscombe Good to know I'm not alone in experiencing this, but if this is unavoidable it makes powershell worse than useless for my purposes: automating application testing. – user3100604 Dec 13 '13 at 20:22
  • Have you tried starting the process with [System.Diagnostics.Process](http://msdn.microsoft.com/en-us/library/System.Diagnostics.Process(v=vs.110).aspx) ? To my knowledge, this is a very reliable approach. – bouvierr Dec 14 '13 at 06:52

0 Answers0