Rebooting a Remote Server using WMI

Windows Management Instrumentation or WMI holds many surprises and features but when my colleague's machine rebooted even I was surprised. Using WMI allows you to create management type applications and of course, being able to reboot a remote computer is a necessary feature. Of course, it requires you have an admin logon account to the machine.
 
In this code written with Beta 2, you will need to change the username, password, and ip address to access your own machines.
 
While WMI is a great feature it may be exposed as a security risk. If you have no intention of using its features on your network you may want to disable it. Enjoy.
  1. //WMI3  
  2. //Demonstrates how to remotely reboot a computer on the network  
  3. //Written 02/01/02 By John O'Donnell - [email protected]  
  4. using System;  
  5. using System.Management;  
  6. namespace WMI3 {  
  7.   /// <summary>  
  8.   /// Summary description for Class1.  
  9.   /// </summary>  
  10.   class Class1 {  
  11.     static void Main(string[] args) {  
  12.       Console.WriteLine("Computer details retrieved using Windows Management Instrumentation (WMI)");  
  13.       Console.WriteLine("Written 02/01/02 By John O'Donnell - [email protected]");  
  14.       Console.WriteLine("=================================");  
  15.       //Connect to the remote computer  
  16.       ConnectionOptions co = new ConnectionOptions();  
  17.       co.Username = "john";  
  18.       co.Password = "john";  
  19.       System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\192.168.1.2\\root\\cimv2", co);  
  20.       //Query remote computer across the connection  
  21.       System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");  
  22.       ManagementObjectSearcher query1 = new ManagementObjectSearcher(ms, oq);  
  23.       ManagementObjectCollection queryCollection1 = query1.Get();  
  24.       foreach(ManagementObject mo in queryCollection1) {  
  25.         string[] ss = { "" };  
  26.         mo.InvokeMethod("Reboot", ss);  
  27.         Console.WriteLine(mo.ToString());  
  28.       }  
  29.     }  
  30.   }  


Recommended Free Ebook
Similar Articles