gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:34:43 27 4
gpt4 key购买 nike

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

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 ;");

关于c# - PowerShell 字符串命令中的管道字符在 Csharp C# 代码中无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60604617/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com