简体   繁体   中英

Execute a process in a remote machine using WMI

I want to open process pon remote machine, this remote machine is inside local network. I try this command and in the remote machine nothing happen, this user that i connect with have administrators rights. Both machines running Windows 7

static void Main(string[] args)
{
    try
    {
        //Assign the name of the process you want to kill on the remote machine
        string processName = "notepad.exe";

        //Assign the user name and password of the account to ConnectionOptions object
        //which have administrative privilege on the remote machine.
        ConnectionOptions connectoptions = new ConnectionOptions();
        connectoptions.Username = @"MyDomain\MyUser";
        connectoptions.Password = "12345678";

        //IP Address of the remote machine
        string ipAddress = "192.168.0.100";
        ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2", connectoptions);

        //Define the WMI query to be executed on the remote machine
        SelectQuery query = new SelectQuery("select * from Win32_process where name = '" + processName + "'");
        object[] methodArgs = { "notepad.exe", null, null, 0 };
        using (ManagementObjectSearcher searcher = new
                    ManagementObjectSearcher(scope, query))
        {
            foreach (ManagementObject process in searcher.Get())
            {
                //process.InvokeMethod("Terminate", null);
                process.InvokeMethod("Create", methodArgs);
            }
        }

        Console.ReadLine();

    }
    catch (Exception ex)
    {
        //Log exception in exception log.
        //Logger.WriteEntry(ex.StackTrace);
        Console.WriteLine(ex.StackTrace);

    }
}

you are not opening a process with that code but you are enumerating all the running process named "iexplore.exe" and close them.

I think an easier, better way is to use SysInternals PsExec or the Task Scheduler API

If you want to use WMI your code should look like this:

object theProcessToRun = { "YourFileHere" };

ManagementClass theClass = new ManagementClass(@"\\server\root\cimv2:Win32_Process");

theClass.InvokeMethod("Create", theProcessToRun);

----------In reply to your comment------------------

First of all you need to change your attitude and approach to coding and read the code that your are copy/pasting.

Then you should study a little more about programming languages.

No I will not write the code for you. I gave you an hint to point to the right direction. now it is your turn to develop it. Have fun!!

This is script that i did for my company before this using vbs script. can search the net to convert it to C# or etc. Fundamental of the steps and how to start a service using WMI. Have a nice coding and have fun.

sUser = "TESTDomain\T-CL-S"
sPass = "Temp1234"  

Set ServiceSet = GetObject("winmgmts:").ExecQuery("Select * from Win32_Service where Name = 'netlogon'")

For Each Service In ServiceSet
   Service.StopService
   Service.Change "netlogon",Service.PathName, , ,"Automatic",false,sUser,sPass
   Service.StartService
Next

Set Service = Nothing
Set ServiceSet = Nothing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM