View Single Post
  #1 (permalink)  
Old June 7th, 2007, 05:08 AM
vikingsunil vikingsunil is offline
Registered User
 
Join Date: Aug 2004
Location: Delhi, , India.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vikingsunil
Default How to make a WMI call from an ASP.Net Page

Has anybody got any experience calling WMI from AP.Net page...? I am getting a strange problem when I am trying to make WMI calls from my ASP.Net page using the code given below, My website is configured to use the Windows Integrated Authentication and impersonation is ON. The IIS host server is allowed for delegation from the Active Directory settings, still I am getting these exceptions:

Access is denied.
at System.Management.ManagementScope.Initialize() at System.Management.ManagementObject.Initialize(Bool ean getObject) at System.Management.ManagementObject.InvokeMethod(St ring methodName, ManagementBaseObject inParameters, InvokeMethodOptions options)

The code is as following:

ManagementPath path = new ManagementPath();
path.NamespacePath = "root\\CIMV2";

path.Server = txtMachine.Text; // User Supplied Machine Name essentially in the same windows domain.
path.RelativePath = string.Format("Win32_service.Name='{0}'", txtService.Text); //Formaing the Relative path with user supplied windows service name.
ManagementObject serviceManagementObject = new ManagementObject(path);
serviceManagementObject.InvokeMethod("StopService" ,null, null); //Trying to stop Windows Service


//Collecting Suppied machine's Storage media information
SelectQuery selectQuery = new SelectQuery("SELECT * FROM Win32_LogicalDisk");
using (ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(new ManagementScope(GetManagementPath(networkID)), selectQuery))
{
    ManagementObjectCollection driveMangementObjects = managementObjectSearcher.Get();

    //Drive[] drives = new Drive[driveMangementObjects.Count];
    //int i = 0;
    foreach (ManagementObject driveMangementObject in driveMangementObjects)
    {
        String DriveType = driveMangementObject["DriveType"].ToString();
        String DeviceID = driveMangementObject["DeviceID"].ToString();

        if(Convert.ToInt64(DriveType)==3)
        {
            long DriveSize = Convert.ToInt64(driveMangementObject["Size"]);
            long DriveFreeSpace = Convert.ToInt64(driveMangementObject["FreeSpace"]);

            //displaying the info on UI.
            if(lblDevices.Text =="")
                lblDevices.Text = lblDevices.Text + DeviceID + "<BR> Size: "+ DriveSize.ToString() + "<BR> Freespace :"+ DriveFreeSpace.ToString();
            else
                lblDevices.Text = lblDevices.Text + "<BR>"+ DeviceID + "<BR> Size: "+ DriveSize.ToString() + "<BR> Freespace :"+ DriveFreeSpace.ToString();
        }
    }
}



Reply With Quote