stand on the shoulders of giants

[转]Terminate / kill /close process on remote machine using WMI

The WMI technique is very powerful for system related activities. They provide operating system interface.

The following code can be used to close instance of visual studio on remote machine provided you have access to it. It kills the instance of it. This way you can make your colleague frustrated.

// connection will required user having access to remote m/c

System.Management.ConnectionOptions
conO =
new System.Management.ConnectionOptions();

conO.Username = "DOMAIN\\sandeep";
conO.Password =
"*******";

System.Management.ManagementScope
oMs =
new System.Management.ManagementScope(@"\\<remoteMachineName>\root\cimv2",conO);

//get all the Processes running on remote m/c

System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("Select
* from Win32_Process"
);

//Execute the query

System.Management.ManagementObjectSearcher oSearcher = new System.Management.ManagementObjectSearcher(oMs,oQuery);

//Get the results

System.Management.ManagementObjectCollection oReturnCollection = oSearcher.Get();

//loop through found process and terminate

foreach (System.Management.ManagementObject oReturn in oReturnCollection)
{
string[] argList = new string[] { string.Empty };

//Name of process
if (oReturn["Name"].ToString().ToLower() == "devenv.exe")
{
object[] obj = new object[] { 0 };
oReturn.InvokeMethod(
"Terminate", obj);
}
}

 

Ref:
sandeep: http://inquest.wordpress.com/2008/04/08/terminate-process-on-remote-machine-using-wmi/

posted @ 2010-08-04 18:37  DylanWind  阅读(373)  评论(0编辑  收藏  举报