joebalt
joebalt

Reputation: 989

Process.StandardOutput.ReadToEnd() and extraneous newline chars

I am trying to understand why, when I call the above function, I am getting hex 0D0A every 80th column on the output I am reading.

I have a powershell script, for testing that has two lines in it for brevity's sake:

$xmlSpew = "<IISAppPoolConfig><DefaultWebSite><ApplicationPoolName>DefaultAppPool</ApplicationPoolName></DefaultWebSite><AuthN>Basic</AuthN></IISAppPoolConfig>"
Write-Output $xmlSpew

I am calling the script using the Process object with ProcessStartInfo as follows:

var psi = new ProcessStartInfo
{
    WorkingDirectory = Path.GetDirectoryName(FileToRun),
    FileName = FileToRun,
    Arguments = Arguments,
    UseShellExecute = false,
    CreateNoWindow = true,
    RedirectStandardError = true,
    RedirectStandardOutput = true,
};

var process = new Process
{
    StartInfo = psi,
    EnableRaisingEvents = true,
};

FileToRun value is:

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe

Arguments value is:

-File "C:\Program Files\MyTest\MyProgInputs\read.d\IISAppPoolConfig.ps1"

The script runs fine and I get back exit code 0, but I have this mysterious (to me) 0D0A newline every 80th char in standard out that I capture using:

var Stdout = new List<string>;

...

Stdout.Add(process.StandardOutput.ReadToEnd());

This is wreaking havoc on my XML efforts once I have standard out stored in a string var. I expected to get exactly what I write to the stdout in the ps1 script, not the extra newline.

What am I missing? I've looked for others with this issue, but I have not found an answer. Hopefully it is not me being search-challenged.

Upvotes: 2

Views: 1436

Answers (2)

joebalt
joebalt

Reputation: 989

Final and tested resolution for now (because I need to ship something), is to have output from powershell come from the Write-Host cmdlet instead of Write-Output. The process for obtaining stdout remained the same as in my original post. Hope this helps others. Thanks for all the inputs.

Upvotes: 1

Jirka Hanika
Jirka Hanika

Reputation: 13529

Follow this P/Invoke method and set dwXCountChars to a very large value. Don't forget to include STARTF_USECOUNTCHARS in the flags as well.

Upvotes: 1

Related Questions