p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VS.NET 2002/2003 (http://p2p.wrox.com/forumdisplay.php?f=103)
-   -   How to make a WMI call from an ASP.Net Page (http://p2p.wrox.com/showthread.php?t=58816)

vikingsunil June 7th, 2007 05:08 AM

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();
        }
    }
}





All times are GMT -4. The time now is 09:01 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.