Run the application on a remote PC using C #

I want to run the application on a remote computer connected via a local network.

This is the code I 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 problem is that the Notepad.exe process is running, and I see this process in the task manager, but there is no notepad window.

Can anyone suggest that I did wrong or went missing?

And I tried using a duplicate method. 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 have an Invalid UserName or Password Exceprion instead of FileNotFound.

PS I found a solution! If this is interesting to someone, this is:

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 additional requirements: You need to enable LanmanServer on the remote PC and use ADMIN $ or C $

+4
source share

Source: https://habr.com/ru/post/1695575/


All Articles