Why in C#, when I invoke a script with PowerShell and capture the buffer, is the output completely different from running in Windows Terminal PS?
I'm supposed to be invoking the same application to run the command (PowerShell) from C#... as is running in Windows Terminal PS.
I'm running this C# application open with elevated privileges:
string command = "docker-compose -p wsdd-projects -f C:\Docker-Structure\init.yml up -d --build"
string policy = "Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted -Force";
string arguments = $"-NoProfile -Command \"{policy} ; '{command}'\"";
ProcessStartInfo processBuilder = new("powershell")
{
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
Verb = "runas",
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8
};
Capture output with:
Process process = new() { StartInfo = processBuilder };
process.OutputDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
if (showOutput) { terminal.AddToTerminal(e.Data); }
output.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
if (showOutput) { terminal.AddToTerminal(e.Data); }
output.AppendLine(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
await process.WaitForExitAsync();
I get this output from PS invoke from C#:
reverse-proxy Pulled
phpmyadmin Pulled
faef57eae888 Pull complete
989a1d6c052e Pull complete
0705c9c2f22d Pull complete
621478e043ce Pull complete
98246dcca987 Pull complete
bfed8c155cb6 Pull complete
7a7c2e908867 Pull complete
d176994b625c Pull complete
2d8ace6a2716 Pull complete
c70df516383c Pull complete
15e1b44fe4c7 Pull complete
65e50d44e95a Pull complete
77f68910bc0a Pull complete
605dd3a6e332 Pull complete
99ce27188f07 Pull complete
74d64e32c5d5 Pull complete
ef5fc9928b9f Pull complete
163f3256e112 Pull complete
8a1e25ce7c4f Pull complete
e78b137be355 Pull complete
39fc875bd2b2 Pull complete
035788421403 Pull complete
87c3fb37cbf2 Pull complete
c5cdd1ce752d Pull complete
33952c599532 Pull complete
80bad5b02d6c Pull complete
b86cede81497 Pull complete
876ba35ce95b Pull complete
ac8fec215cc8 Pull complete
7795496aa545 Pull complete
4f4fb700ef54 Pull complete
Instead of this output in Terminal PS Windows:
[+] Running 33/33
✔ reverse-proxy 13 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 14.3s
✔ 8a1e25ce7c4f Pull complete 8.1s
✔ e78b137be355 Pull complete 8.5s
✔ 39fc875bd2b2 Pull complete 6.9s
✔ 035788421403 Pull complete 7.6s
✔ 87c3fb37cbf2 Pull complete 8.1s
✔ c5cdd1ce752d Pull complete 8.6s
✔ 33952c599532 Pull complete 8.7s
✔ 80bad5b02d6c Pull complete 9.0s
✔ b86cede81497 Pull complete 9.4s
✔ 876ba35ce95b Pull complete 9.8s
✔ ac8fec215cc8 Pull complete 9.6s
✔ 7795496aa545 Pull complete 10.1s
✔ 4f4fb700ef54 Pull complete 10.2s
✔ phpmyadmin 18 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 18.4s
✔ faef57eae888 Pull complete 2.3s
✔ 989a1d6c052e Pull complete 0.5s
✔ 0705c9c2f22d Pull complete 4.7s
✔ 621478e043ce Pull complete 1.0s
✔ 98246dcca987 Pull complete 3.0s
✔ bfed8c155cb6 Pull complete 2.8s
✔ 7a7c2e908867 Pull complete 3.5s
✔ d176994b625c Pull complete 4.3s
✔ 2d8ace6a2716 Pull complete 4.1s
✔ c70df516383c Pull complete 5.1s
✔ 15e1b44fe4c7 Pull complete 4.8s
✔ 65e50d44e95a Pull complete 5.3s
✔ 77f68910bc0a Pull complete 5.4s
✔ 605dd3a6e332 Pull complete 5.9s
✔ 99ce27188f07 Pull complete 6.1s
✔ 74d64e32c5d5 Pull complete 6.5s
✔ ef5fc9928b9f Pull complete 6.4s
Where is this line in my code? [+] Running
And the characters ✔ and [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿]?
Almost all console apps will detect if the output is a terminal or a pipeline or a redirected file and stop outputting formatting in case the stdout is not a terminal. The formatting characters are just for beautified output and aren't useful for processing in scripts, therefore console apps will turn it off in a non-terminal output. If you run
docker-compose | where { $True }in PowerShell ordocker-compose | catin a *nix terminal then the result will be the same as in your C# appIn docker it's the
--ansior--no-ansioption. Usedocker compose --ansi neverordocker compose --no-ansidepending on your docker version for consistent results.docker compose --ansi alwaysalso works but is only suitable for printing the output to an ANSI terminal because it may show hidden ANSI escape sequences which is unexpected in a text editor. It's the same effect as if you allocate a pseudo-tty in your C# and rundocker-composenormallySince you only want to process the output in C# then