4

I want to launch an application on remote computer that is connected via LAN.

This is the code that I have used:

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);

The trouble with this is that the Notepad.exe process has started and I can see this process in Task Manager but there is no notepad window.

Could someone suggest what I have done wrong or am missing with this?

And I tried to use method from duplicate. And tried to add authorization.

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);
        }

I've got Invalid UserName or Password Exceprion instead of FileNotFound.

P.S. I've found solution! If it's interesting for someone it's there:

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);
        }

But there are some extra requirements: You need to switch on LanmanServer on remote PC and share ADMIN$ or C$

Ivan Kozlov
  • 255
  • 3
  • 13

0 Answers0