PowerShell 字符串命令中的管道字符在 Csharp C# 代码中无法识别

Pipe character from PowerShell string command is not recognized in Csharp C# code

我必须通过代码在远程机器上执行一些 PowerShell 命令。为此,我使用 .net ssh 客户端。除了管道命令解释外,一切正常。下面这段代码 returns 我出错了:

DEBUG: 'Stop-Process' is not recognized as an internal or external command, operable program or batch file.

using System.IO;

using Or1WinAppDriverTests.Properties;

using Renci.SshNet;

using Xunit;
using Xunit.Abstractions;

namespace Or1WinAppDriverTests.aidaTests {

    public class AidaMachineSshAccessTests
    {
        public AidaMachineSshAccessTests(ITestOutputHelper output)
        {
            this._output = output;
        }

        private readonly ITestOutputHelper _output;

        [Fact]
        public void SshTest3()
        {
            var host = Resources.HOST;
            var username = Resources.USERNAME;
            var password = Resources.PASSWORD;

            using (var client = new SshClient(host, username, password))
            {
                #region Example SshCommand CreateCommand Execute ExtendedOutputStream

                client.Connect();

                var cmd2 = client.CreateCommand(
                    $"powershell.exe -executionPolicy bypass ;" +
                    $"Get-Process notepad | Stop-Process ;" +
                    $"exit ;");

                var result = cmd2.Execute();

                _output.WriteLine(result);

                var reader = new StreamReader(cmd2.ExtendedOutputStream);
                _output.WriteLine("DEBUG:");
                _output.WriteLine(reader.ReadToEnd());

                client.Disconnect();

                #endregion Example SshCommand CreateCommand Execute ExtendedOutputStream
            }
        }
    }
}

如果我添加额外的引号,它会起作用:

var cmd2 = client.CreateCommand(
                    "powershell.exe -executionPolicy bypass; " +
                    "\"Get-Process notepad | Stop-Process\"; " +
                    "exit ;");