2013-10-28 76 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 
相关问题