我想在通过LAN连接的远程计算机上启动应用程序。
这是我使用过的代码:
string sLogin = "Administrator";
string sPassword = "Password";
string sComputer = "192.168.201.224";
ManagementScope ms;
ConnectionOptions co = new ConnectionOptions();
co.Username = sLogin;
co.Password = sPassword;
co.EnablePrivileges = true;
co.Impersonation = ImpersonationLevel.Impersonate;
ms = new ManagementScope(string.Format(@"\\{0}\root\CIMV2", sComputer), co);
ms.Connect();
ManagementPath path = new ManagementPath("Win32_Process");
System.Management.ManagementClass classObj = new System.Management.ManagementClass(ms, path, null);
System.Management.ManagementBaseObject inParams = null;
inParams = classObj.GetMethodParameters("Create");
inParams["CommandLine"] = "notepad.exe";
inParams["CurrentDirectory"] = "C:\\WINDOWS\\system32\\";
ManagementBaseObject outParams = classObj.InvokeMethod("Create", inParams, null);
问题在于Notepad.exe进程已启动,我可以在任务管理器中看到此进程,但没有记事本窗口。
有人可以提出我做错了或错过了吗?
我尝试使用复制方法。并尝试添加授权。
var serverName = "192.168.201.224";
var psss = "Password";
try
{
System.Security.SecureString str = new System.Security.SecureString();
for(int i=0; i<psss.Length;i++)
{
str.AppendChar(psss[i]);
}
//Start the process
ProcessStartInfo info = new ProcessStartInfo("C:\\PsTools");
info.Domain = "192.168.201.224";
info.UserName = "Administrator";
info.Password = str;
info.FileName = @"C:\PsTools\psexec.exe";
info.Arguments = @"\\" + serverName + @" -i C:\WINDOWS\notepad.exe";
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
Process p = Process.Start(info);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
我的UserName或密码Exceprion无效,而不是FileNotFound。
P.S。 我找到了解决方案!如果它对那里有人感兴趣:
var serverName = "192.168.201.224";
var psss = "Password";
var user = "Administrator";
var appPath = @"Path to destination app with cmd line args";
try
{
//Start the process
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = @"C:\PsTools\psexec.exe";
var args = String.Format(@"\\{0} -d -u {1} -p {2} -i {3} ", serverName,user,psss,appPath);
info.Arguments = args;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
Process p = Process.Start(info);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
但是有一些额外的要求: 您需要在远程PC上打开LanmanServer并共享ADMIN $或C $