C# 如何在远程计算机上执行命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/428276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute a command in a remote computer?
提问by user53179
I have a shared folder in a server and I need to remotely execute a command on some files. How do I do that?
我在服务器中有一个共享文件夹,我需要对某些文件远程执行命令。我怎么做?
What services need to be running on the server to make that work?
需要在服务器上运行哪些服务才能使其工作?
Some details: Only C# can be used. Nothing can be installed in the server.
一些细节: 只能使用 C#。什么都不能安装在服务器中。
回答by chakrit
IMO, in your case you can try this:
IMO,在你的情况下,你可以试试这个:
- Map the shared folder to a drive or folder on your machine. (here's how)
- Access the mapped drive/folder as you normally would local files.
- 将共享文件夹映射到您机器上的驱动器或文件夹。(方法如下)
- 像通常访问本地文件一样访问映射的驱动器/文件夹。
Nothing needs to be installed. No services need to be running except those that enable folder sharing.
什么都不需要安装。除了启用文件夹共享的服务外,不需要运行任何服务。
If you can access the shared folder and maps it on your machine, most things should work just like local files, including command prompts and all explorer-enhancement tools.
如果您可以访问共享文件夹并将其映射到您的机器上,那么大多数事情应该像本地文件一样工作,包括命令提示符和所有资源管理器增强工具。
This is different from using PsExec (or RDP-ing in) in that you do not need to have administrative rights and/or remote desktop/terminal services connection rights on the remote server, you just need to be able to access those shared folders.
这与使用 PsExec(或 RDP-ing)不同,您不需要在远程服务器上拥有管理权限和/或远程桌面/终端服务连接权限,您只需要能够访问这些共享文件夹。
Also make sure you have all the necessary security permissions to run whatever commands/tools you want to run on those shared folders as well.
还要确保您拥有所有必要的安全权限,以运行您想要在这些共享文件夹上运行的任何命令/工具。
If, however you wish the processing to be done onthe target machine, then you can try PsExecas @divo and @recursive pointed out, something alongs:
但是,如果您希望在目标机器上完成处理,那么您可以像@divo 和@recursive 指出的那样尝试PsExec,还有一些东西:
PsExec \yourServerName -u yourUserName cmd.exe
Which will brings gives you a command prompt atthe remote machine. And from there you can execute whatever you want.
这将带来给你一个命令提示符在远程机器。从那里你可以执行任何你想要的。
I am not sure but I think you need either the Server (lanmanserver
) or the Terminal Services (TermService
) service to be running (which should have already be running).
我不确定,但我认为您需要运行服务器 ( lanmanserver
) 或终端服务 ( TermService
) 服务(应该已经在运行)。
回答by Dmitri Nesteruk
I use the little utility which comes with PureMPI.netcalled execcmd.exe
. Its syntax is as follows:
我使用PureMPI.net 附带的名为execcmd.exe
. 其语法如下:
execcmd \yourremoteserver <your command here>
Doesn't get any simpler than this :)
没有比这更简单的了:)
回答by ZokiManas
try
{
string AppPath = "\\spri11U1118\SampleBatch\Bin\";
string strFilePath = AppPath + "ABCED120D_XXX.bat";
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = strFilePath;
string pwd = "s44erver";
proc.StartInfo.Domain = "abcd";
proc.StartInfo.UserName = "sysfaomyulm";
System.Security.SecureString secret = new System.Security.SecureString();
foreach (char c in pwd)
secret.AppendChar(c);
proc.StartInfo.Password = secret;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.WorkingDirectory = "psexec \\spri11U1118\SampleBatch\Bin ";
proc.Start();
while (!proc.HasExited)
{
proc.Refresh();
// Thread.Sleep(1000);
}
proc.Close();
}
catch (Exception ex)
{
throw ex;
}
回答by Dennis
Another solution is to use WMI.NET or Windows Management Instrumentation.
另一种解决方案是使用WMI.NET 或 Windows Management Instrumentation。
Using the .NET Framework namespace System.Management, you can automate administrative tasks using Windows Management Instrumentation (WMI).
使用 .NET Framework 命名空间 System.Management,您可以使用 Windows Management Instrumentation (WMI) 自动执行管理任务。
Code Sample
代码示例
using System.Management;
...
var processToRun = new[] { "notepad.exe" };
var connection = new ConnectionOptions();
connection.Username = "username";
connection.Password = "password";
var wmiScope = new ManagementScope(String.Format("\\{0}\root\cimv2", REMOTE_COMPUTER_NAME), connection);
var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
wmiProcess.InvokeMethod("Create", processToRun);
If you have trouble with authentication, then check the DCOM configuration.
如果您在身份验证方面遇到问题,请检查 DCOM 配置。
- On the target machine, run
dcomcnfg
from the command prompt. - Expand
Component Services\Computers\My Computer\DCOM Config
- Find Windows Management Instruction, identified with GUID
8BC3F05E-D86B-11D0-A075-00C04FB68820
(you can see this in the details view). - Edit the properties and then add the username you are trying to login with under the permissions tab.
- You may need to reboot the service or the entire machine.
- 在目标计算机上,
dcomcnfg
从命令提示符运行。 - 扩张
Component Services\Computers\My Computer\DCOM Config
- 找到用 GUID 标识的 Windows 管理说明
8BC3F05E-D86B-11D0-A075-00C04FB68820
(您可以在详细信息视图中看到)。 - 编辑属性,然后在权限选项卡下添加您尝试登录的用户名。
- 您可能需要重新启动服务或整个机器。
NOTE:All paths used for the remote process need to be local to the target machine.
注意:用于远程进程的所有路径都需要是目标机器的本地路径。