1

I have an app that prints text to stderr, so to save to a file, I do this:

.\NGPQUERY.exe -spoof -stages -diag 2> c:\foo.txt

None of the text is special characters, other than crlf at the end of lines.

In DOS, the output is fine. In powershell the output is almost fine.

The first line of text is this:

RTE Routings for BRI to LED - 00: 

I get this error message at the top of the output:

NGPQUERY.exe : RTE Routings for BRI to LED - 00: At line:1 char:15 + .\ngpquery.exe <<<< -spoof -stages -diag 2> e:\foo + CategoryInfo : NotSpecified: (RTE Routings for BRI to LED - 00: :String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError

Then throughout the output, line feeds are added at seemingly random locations. So my question is how do I get rid of the error and the random line feeds.

Also powershell outputs file that is twice as large as the dos file, I'm guessing its unicode. So I would like to know the best way get an ansi output too.

johnnycrash
  • 5,184
  • 5
  • 34
  • 58
  • try telling powershell to stop parsing and just run the command like a standard cmd prompt like this: `.\NGPQUERY.exe --% -spoof -stages -diag 2> c:\foo.txt` and see if that gives you better output. – TheMadTechnician Aug 07 '14 at 18:09
  • You get the error message in your output because STDERR is for errors. In other words, it's PowerShell trying to be smart. ) Use `$ErrorActionPreference = "SilentlyContinue"` to suppress. – Alexander Obersht Aug 07 '14 at 19:01
  • @TMT I tried that and it didn't seem to make a difference. – johnnycrash Aug 08 '14 at 15:42

2 Answers2

0

If you only need STDERR, this should be enough:

$oPsi = New-Object -TypeName System.Diagnostics.ProcessStartInfo

$oPsi.FileName = "NGPQUERY.exe"
$cArgs = @("-spoof", "-stages", "-diag")

$oPsi.Arguments = $cArgs
$oPsi.CreateNoWindow = $true
$oPsi.UseShellExecute = $false
$oPsi.RedirectStandardError = $true

$oProcess = New-Object -TypeName System.Diagnostics.Process
$oProcess.StartInfo = $oPsi
[Void]$oProcess.Start()
$sStdErr = $oProcess.StandardError.ReadToEnd()
[Void]$oProcess.WaitForExit()

$sStdErr | Out-File -Encoding "ASCII" -FilePath "C:\foo.txt"
Alexander Obersht
  • 3,215
  • 2
  • 22
  • 26
  • any way to do that in one line? Also, the app writes to stdout and stderr synchronously, so this solution will block. Well it might not block if i'm not grabbing stdout. – johnnycrash Aug 07 '14 at 19:39
  • It seems to me that bash really handles this a lot better. – johnnycrash Aug 07 '14 at 19:40
  • Not sure about one line, never tried. For async version see my answer here: http://stackoverflow.com/questions/24370814/how-to-capture-process-output-asynchronously-in-powershell/24371479#24371479 – Alexander Obersht Aug 07 '14 at 19:59
0

This is an old question, but I got here because I had a similar problem and found the easiest solution to be to call cmd.exe and surround the command in quotes, to prevent Powershell from interpreting it. So in the above case that would be:

cmd.exe /c ".\NGPQUERY.exe -spoof -stages -diag 2> c:\foo.txt"
Ronny D'Hoore
  • 752
  • 7
  • 9