如何在 C# 中在远程计算机上执行进程

发布于 2024-08-23 06:40:12 字数 761 浏览 8 评论 0 原文

如何使用 System.Diagnostics.Process 类在 C# 中的远程计算机上启动进程,例如计算机名称 =“someComputer”?

我在那台远程计算机上创建了一个小型控制台应用程序,只需将“Hello world”写入 txt 文件,我想远程调用它。

控制台应用程序路径:c:\MyAppFolder\MyApp.exe

目前我有这个:

ProcessStartInfo startInfo = new ProcessStartInfo(string.Format(@"\\{0}\{1}", someComputer, somePath);

            startInfo.UserName = "MyUserName";
            SecureString sec = new SecureString();
            string pwd = "MyPassword";
            foreach (char item in pwd)
            {
                sec.AppendChar(item);
            }
            sec.MakeReadOnly();
            startInfo.Password = sec;
            startInfo.UseShellExecute = false;

            Process.Start(startInfo);

我不断收到“找不到网络路径”。

How can I start a process on a remote computer in c#, say computer name = "someComputer", using System.Diagnostics.Process class?

I created a small console app on that remote computer that just writes "Hello world" to a txt file, and I would like to call it remotely.

Console app path: c:\MyAppFolder\MyApp.exe

Currently I have this:

ProcessStartInfo startInfo = new ProcessStartInfo(string.Format(@"\\{0}\{1}", someComputer, somePath);

            startInfo.UserName = "MyUserName";
            SecureString sec = new SecureString();
            string pwd = "MyPassword";
            foreach (char item in pwd)
            {
                sec.AppendChar(item);
            }
            sec.MakeReadOnly();
            startInfo.Password = sec;
            startInfo.UseShellExecute = false;

            Process.Start(startInfo);

I keep getting "Network path was not found".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

离鸿 2024-08-30 06:40:12

可以使用 http://technet.microsoft.com/en-us 中的 PsExec /sysinternals/bb897553.aspx

或 WMI:

object theProcessToRun() = { "YourFileHere" };

ManagementClass theClass = new ManagementClass(@"\\server\root\cimv2:Win32_Process");

theClass.InvokeMethod("Create", theProcessToRun);

Can can use PsExec from http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

Or WMI:

object theProcessToRun() = { "YourFileHere" };

ManagementClass theClass = new ManagementClass(@"\\server\root\cimv2:Win32_Process");

theClass.InvokeMethod("Create", theProcessToRun);
一个人的旅程 2024-08-30 06:40:12

使用以下其中一项:(

或者,如果您愿意,可以注入您自己的服务或 COM 组件。这与 PsExec 的做法非常接近。

在所有这些方法中,我更喜欢任务调度程序。我认为这是所有 API 中最干净的。连接到远程任务计划程序,为可执行文件创建一个新任务,然后运行它。注意:可执行文件名称应该是该机器的本地名称。不是 \servername\path\file.exe,而是 c:\path\file.exe。如果您愿意,可以删除该任务。

所有这些方法都要求您具有目标计算机的管理访问权限。

ProcessStartInfo 无法启动远程进程。

Use one of the following:

Or if you feel like it, inject your own service or COM component. That would be very close to what PsExec does.

Of all these methods, I prefer task scheduler. The cleanest API of them all, I think. Connect to the remote task scheduler, create a new task for the executable, run it. Note: the executable name should be local to that machine. Not \servername\path\file.exe, but c:\path\file.exe. Delete the task if you feel like it.

All those methods require that you have administrative access to the target machine.

ProcessStartInfo is not capable of launching remote processes.

才能让你更想念 2024-08-30 06:40:12

根据MSDN,一个进程 对象仅允许访问远程进程,而不能启动或停止远程进程。因此,要回答您关于使用此类的问题,您不能。

According to MSDN, a Process object only allows access to remote processes not the ability to start or stop remote processes. So to answer your question with respect to using this class, you can't.

谁对谁错谁最难过 2024-08-30 06:40:12

以 WMI 和其他凭据作为当前进程的示例,默认情况下它使用与进程运行相同的用户。

var hostname = "server"; //hostname or a IpAddress

var connection = new ConnectionOptions();
//The '.\' is for a local user on the remote machine
//Or 'mydomain\user' for a domain user
connection.Username = @".\Administrator";
connection.Password = "passwordOfAdministrator";

object[] theProcessToRun = { "YourFileHere" }; //for example notepad.exe

var wmiScope = new ManagementScope($@"\\{hostname}\root\cimv2", connection);
wmiScope.Connect();
using (var managementClass = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()))
{
    managementClass.InvokeMethod("Create", theProcessToRun);
}

An example with WMI and other credentials as the current process, on default it used the same user as the process runs.

var hostname = "server"; //hostname or a IpAddress

var connection = new ConnectionOptions();
//The '.\' is for a local user on the remote machine
//Or 'mydomain\user' for a domain user
connection.Username = @".\Administrator";
connection.Password = "passwordOfAdministrator";

object[] theProcessToRun = { "YourFileHere" }; //for example notepad.exe

var wmiScope = new ManagementScope($@"\\{hostname}\root\cimv2", connection);
wmiScope.Connect();
using (var managementClass = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()))
{
    managementClass.InvokeMethod("Create", theProcessToRun);
}
柠檬心 2024-08-30 06:40:12

我不相信您可以直接通过 UNC 路径启动进程;也就是说,如果 System.Process 使用 Windows comspec 来启动应用程序...如何通过将驱动器映射到“\someComputer\somePath”来测试这个理论,然后将 ProcessStartInfo 的创建更改为该理论?如果它以这种方式工作,那么您可能需要考虑以编程方式临时映射驱动器,启动您的应用程序,然后删除映射(就像在命令窗口中的 pushd/popd 工作一样)。

I don't believe you can start a process through a UNC path directly; that is, if System.Process uses the windows comspec to launch the application... how about you test this theory by mapping a drive to "\someComputer\somePath", then changing your creation of the ProcessStartInfo to that? If it works that way, then you may want to consider temporarily mapping a drive programmatically, launch your app, then remove the mapping (much like pushd/popd works from a command window).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文