我在服务器中有一个共享文件夹,我需要对某些文件远程执行命令。我怎么做?
需要在服务器上运行哪些服务才能使其正常工作?
一些细节: 只能使用 C#。服务器中不能安装任何东西。
另一种解决方案是使用WMI.NET 或 Windows Management Instrumentation。
使用 .NET Framework 命名空间 System.Management,您可以使用 Windows Management Instrumentation (WMI) 自动执行管理任务。
代码示例
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);
如果您在身份验证方面遇到问题,请检查 DCOM 配置。
dcomcnfg
从命令提示符运行。 Component Services\Computers\My Computer\DCOM Config
8BC3F05E-D86B-11D0-A075-00C04FB68820
(您可以在详细信息视图中看到它)。注意:用于远程进程的所有路径都必须是目标机器的本地路径。
You could use SysInternal's PsExec.
IMO, in your case you can try this:
- 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.
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 on the target machine, then you can try PsExec as @divo and @recursive pointed out, something alongs:
PsExec \\yourServerName -u yourUserName cmd.exe
Which will brings gives you a command prompt at the 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).
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;
}
I use the little utility which comes with PureMPI.net called execcmd.exe
. Its syntax is as follows:
execcmd \\yourremoteserver <your command here>
Doesn't get any simpler than this :)