Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We have a windows service which is running under a account that is not part of local admin. With in the service we call a powershell file using processstartinfo. With in powershell file we call the WMI class

"$AVGCPU = Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select-object -expand Average"

The above code always returns 0.

What I have tried:

When i make the user as local admin then the cpu usage value is returned. Also when i run the same code in powershell launched under the same user account (without giving local admin privileges), i am able to get the value for CPU usage. Its only when the cmdlets is called from windows service i am getting this problem. Due to security restriction, i cannot make the user as local administrator. Any idea what rights should be given to the user that WMI class return value when called from windows service. Service is running in windows 2008 R2 server.

In addition, if i run the windows service as system user, i am getting the CPU usage value. This has to be something related to access rights. Does any have any idea on this
Posted
Updated 12-Oct-18 9:01am

1 solution

Services run under a different desktop from the one the user is logged in on.

If you want code to cross the boundary between desktops, yes, you need admin permissions to do that.

You can see this yourself quite easily. Open Task Manager as a normal user. On the Processes tab, all you see are the processes running under your own account and desktop. If you click "More details" at the bottom, it'll ask for admin credentials to see all the processes from other users on the machine.

Your WMI code has the same limitations. It can't see process that it doesn't have permissions to see. Code runs as the user that launches it. If your service account doesn't have admin priv's, your code isn't going to have them either.

EDIT
The command line you're using assigns the value returned by the Select to a powershell variable, called $AVGCPU. How are you checking for the return value in your code?
 
Share this answer
 
v2
Comments
manikandanid 15-Oct-18 9:35am    
If what you say is right then when i run the powershell command launched as local user, the cpu usage value should not be returned. But i am able to get the value. Its only when the same powershell script is called from a windows service (run under the same local user )the values are returned only as zero.
Dave Kreskowiak 15-Oct-18 10:46am    
The command line you're using assigns the value returned by the Select to a powershell variable, called $AVGCPU. How are you checking for the return value in your code?

If you're thinking the value should be returned as an Exit Code by the Process object, you're mistaken. The 0 returned means the command you launched execute without any errors.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900