最后,我使用以下代码运行我的命令集:
public static string RunCommand(string cmd)
{
var base64EncodedString = Convert.ToBase64String(ASCIIEncoding.Unicode.GetBytes(cmd));
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "pwsh",
Arguments = $"-encodedcommand \"{base64EncodedString}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result;
}
其中,cmd param是我的所有命令与;分隔符的连接。
对于windows到windows远程处理,文件名应为:
FileName = @"C:\Program Files\PowerShell\6.0.4\pwsh.exe",