C# 在远程计算机c上执行进程#

C# 在远程计算机c上执行进程#,c#,wmi,remoting,C#,Wmi,Remoting,我通过远程计算机上的WMI连接 用户名和密码设置正确 var options = new ConnectionOptions(); servicePath = "\\\\Testserver\\root\\cimv2"; options.EnablePrivileges = true; options.Username = username; options.Password = pwd; serviceScope = new ManagementScope(servicePath, optio

我通过远程计算机上的WMI连接

用户名和密码设置正确

var options = new ConnectionOptions();
servicePath = "\\\\Testserver\\root\\cimv2";
options.EnablePrivileges = true;
options.Username = username;
options.Password = pwd;
serviceScope = new ManagementScope(servicePath, options);
serviceScope.Connect();
这是我想在远程机器上运行的代码序列

Process process = new Process();
process.StartInfo.FileName = "diskpart.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.StandardInput.WriteLine("list volume");
process.StandardInput.WriteLine("exit");
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();

// extract information from output
string table = output.Split(new string[] { "DISKPART>" }, StringSplitOptions.None)[1];
var rows = table.Split(new string[] { "\n" }, StringSplitOptions.None);
for (int i = 3; i < rows.Length; i++)
{
    if (rows[i].Contains("Volume"))
    {
        int index = Int32.Parse(rows[i].Split(new string[] { " " }, StringSplitOptions.None)[3]);
        string label = rows[i].Split(new string[] { " " }, StringSplitOptions.None)[8];
        Console.WriteLine($@"Volume {index} {label}:\");
    }
}
我可以调用该进程,但没有能力移交我希望在此进程中执行的命令


如何解决此问题?

您可以使用脚本作为包含命令的命令行参数的一部分传入

参考:

此外,您还应该使用cmd将输出重定向到一个文件,因为您也应该直接获得输出


对于脚本,请确保使用其他计算机/用户有权访问的unc。对于输出文件,请确保使用其他计算机/用户可以写入并可以通过程序读取的unc。

您缺少问题描述。我确信有WMI类用于卷信息
列表卷
返回,为什么不改为走那条路线呢。这句话是什么意思
但是我没有能力交出我想在这个过程中执行的命令…
我知道,WMI类可以给我卷信息。我知道。但我需要一个进程调用,以及我必须执行的命令。目前我正在使用命令“列出卷”,因为它们没有破坏任何东西。稍后我必须扩展卷,但首先我必须将命令传输到该进程的命令行。为了更好地解释问题描述。我想通过WMI连接到远程计算机。这是我已经做过的。之后,我想调用diskpart进程,因为我以后必须扩展一个卷。不幸的是,我不能转移命令,我通常可以在上面的文章中的中间代码部分使用。那么如何将命令传输到进程?使用Psexe在远程机器上执行进程c#好的,好的,我可以调用如下函数对象[]处理器un={“diskpart/select volume f”};->所以diskpart使用的是卷f。我需要第二个命令来扩展磁盘。我不能调用它两次,如果我再次调用该命令,据我所知,我调用该进程的一个新实例,对吗?是的。你将调用第二个进程……除非你使用我上面提到的脚本方法。好的,对于这一部分,我必须有一个脚本文件。隐马尔可夫模型。。。。首先,我认为我可以接受命令,但那行不通。我想我必须这样做
object[] theProcessToRun = { "diskpart" };
using (var managementClass = new ManagementClass(serviceScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()))
{
managementClass.InvokeMethod("Create", theProcessToRun);
}