簡體   English   中英

使用WMI在遠程計算機上執行過程

[英]Execute a process in a remote machine using WMI

我想打開進程pon遠程計算機,此遠程計算機在本地網絡內部。 我嘗試此命令,並且在遠程計算機中什么也沒有發生,與我連接的該用戶具有管理員權限。 兩台運行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);

    }
}

您沒有使用該代碼打開進程,而是枚舉了所有正在運行的名為"iexplore.exe"進程並關閉它們。

我認為一種更簡單,更好的方法是使用SysInternals PsExecTask Scheduler API

如果要使用WMI,則代碼應如下所示:

object theProcessToRun = { "YourFileHere" };

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

theClass.InvokeMethod("Create", theProcessToRun);

----------回復您的評論------------------

首先,您需要更改編碼的態度和方式,並閱讀要復制/粘貼的代碼。

然后,您應該學習更多有關編程語言的知識。

不,我不會為您編寫代碼。 我給你一個提示,指出正確的方向。 現在輪到您進行開發了。 玩得開心!!

這是我在使用vbs腳本之前為公司所做的腳本。 可以搜索網絡以將其轉換為C#或其他。步驟的基本原理以及如何使用WMI啟動服務。 編碼愉快,玩得開心。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM