2013-10-28 77 views
2

我想打開進程pon遠程機器,此遠程機器位於本地網絡內。 我試試這個命令,並且在遠程機器上沒有任何事情發生,我連接的這個用戶擁有管理員權限。 運行Windows 7使用WMI在遠程機器上執行進程

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

    } 
} 
+0

['要終止,你沒有自己的過程,使SeDebugPrivilege privilege'(http://msdn.microsoft.com/en-us/library/aa393907(V = vs.85)。 aspx) – rene

回答

2

這兩款機器你不開放的過程與代碼,但你是枚舉所有正在運行的進程名爲"iexplore.exe",並關閉它們。

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

如果你想使用WMI代碼應該是這樣的:

object theProcessToRun = { "YourFileHere" }; 

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

theClass.InvokeMethod("Create", theProcessToRun); 

-------- - 回覆您的評論------------------

首先,你需要改變你的態度和編碼方法,並閱讀你的代碼複製/粘貼。

然後你應該多學習一些編程語言。

不,我不會爲你寫代碼。我給你一個提示,指出正確的方向。現在輪到你開發它了。玩的開心!!

+0

我需要改變(我是一個新開發者...)? – user1860934

+0

添加該代碼或只使用此代碼? – user1860934

+0

看到我的更新,它仍然無法正常工作,我做錯了什麼? – user1860934

0

這是我在使用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 
相關問題