admin管理员组

文章数量:1200397

I am creating an ASP.NET MVC web app to query a list of server resources like Disk Space, RAM and CPU using WMI classes. Everything worked as expected from my local development environment. All the queries are called using a user account with Admin privileges to all those servers.

When I hosted the web app on an IIS server, all of the queries failed for the server where the web app is hosted. The failure message was

User credentials cannot be used for local connections

The answer I obtained from searching the web is that you shouldn't pass the user account when you are query resources on the server that you hosted your app so I made the change and almost all queries worked except 2 with a new error

Object reference not set to an instance of an object

The errors are pointed to the line of codes below. I am not sure why this error so I create this post to ask for help with issue.

The 2 queries that caused errors are:

ConnectionOptions options = new ConnectionOptions(); 
option.UserName = <username_goes here> 
option.Password = <password_goes_here> 
ManagementScope scope = new ManagementScope(strNameSpace, options) 
ObjectQuery query = new ObjectQuery("select AvailableBytes FROM Win32_PerfFormattedData_PerfOS_Memory"); 
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

and

ObjectQuery query2 = new ObjectQuery("select AvailableBytes FROM Win32_PerfFormattedData_PerfOS_Memory");

本文标签: cGetting AvailableBytes of memory and PercentIdleTime of CPU from a remote serversStack Overflow