Saturday, October 30, 2010

Reboot PC using WMI and C# ASP.NET



I wrote this script with ASP.NET in C# so I can reboot a given server.  The server can be reboot from a web page using WMI methods, and I successfully tested the application on a BlackBerry Bold 9000 device (see screenshot). Here is the code:


using System;
using System.Management;
namespace WMI3
{
class Class1
{
static void Main(string[] args)
{



ConnectionOptions co = new ConnectionOptions();
co.Username = "admin"; 
co.Password = "pwd";

co.Impersonation = ImpersonationLevel.Impersonate;
co.EnablePrivileges = true;


System.Management.ManagementScope ms = 
newSystem.Management.ManagementScope
("\\\\" + txtServer.Text + "\\root\\cimv2", co);

System.Management.ObjectQuery oq = new System.Management.ObjectQuery
("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query1 = new ManagementObjectSearcher(ms,oq);
ManagementObjectCollection queryCollection1 = query1.Get();
foreach( ManagementObject mo in queryCollection1 )
{
  string[] ss={""};
  mo.InvokeMethod("Reboot",ss);
}
}
}
}

No comments:

Post a Comment