Author Posts

May 29, 2015 at 2:26 pm

Hello ,
I am a developer and trying to build a web API which helps to retrieve process CPU usage with help of powershell WMI acess capabilities. I wrote following simple code to retrieve CPU result. This code work fine in my local dev machine but have issue when I deploy this on production server hosted on Window 2012 OS , IIS 8.5. I traced every line of executed code , turned on verbose, warning as well error streams but no error recorded or issue reported in verbose record expect zero record returned. Same cmdlet script successfully run on powershell console on same production server and work as expected. Also, I have tried other cmdlets like get-help, get-process on same server to test if there is any issue with SDK but able to get output without an issue. Only problem I had and no results retrieved when I tried Get-CIMInstance and get-WMIObject cmdlet only. I am at total loss here . Appreciate any pointers or help toward right direction.

//strPwrShellCmd = "gwmi Win32_PerfFormattedData_PerfProc_Process | select IDProcess,Name,PercentProcessorTime | where { $_.Name -ne '_Total' -and $_.Name -ne 'Idle'} | sort PercentProcessorTime -Descending | select -First " + intTop;
//create pipeline
PowerShell ps = PowerShell.Create(RunspaceMode.NewRunspace);
ps.AddScript("$verbosepreference='continue'");
ps.AddScript(strPwrShellCmd);

// of System.Diagnostics.Process instances.

// pipeline.Commands.Add("Out-String");
Collection results = ps.Invoke();
// execute the script
//Collection results = ps.Invoke();
Trace.Indent();
//Trace.WriteLine(" Total Result: " + results.Count );
Collection verbResult = ps.Streams.Verbose.ReadAll();
// close the runspace

StringBuilder stringresult = new StringBuilder();

foreach (PSObject obj in results)
{
stringresult.AppendLine(obj.ToString());

}

StringBuilder stringVerbose = new StringBuilder();

foreach (VerboseRecord obj in verbResult)
{
stringVerbose.AppendLine(obj.ToString());

}
return "Result: " + results.Count + " :: " + strPwrShellCmd + " ::" + stringVerbose.ToString() + "::" + stringresult.ToString() + "
}

May 30, 2015 at 3:28 am

Your PowerShell code won't run. It should be

gwmi Win32_PerfFormattedData_PerfProc_Process | select IDProcess,Name,PercentProcessorTime | where { $_.Name -ne '_Total' -and $_.Name -ne 'Idle'} | sort PercentProcessorTime -Descending | select -First 1

though I would recommend that you use full cmdlet and parameter names rather than aliases and positional parameters

where do you tell the code which machine you want to run against?

June 2, 2015 at 6:45 am

I am trying to execute right now for localhost only. Also I tried using full cmdlet name as well as full name.

As per verbose Code is executing without an error or warning but I am getting null results back with any WMI (Get-CMIInstance/Get-WMIObject) cmdlet although able to retrieve result successfully if I use get-process or other commands

June 2, 2015 at 9:23 am

Check the WMI service – Winmgmt is running on the machine you are trying to access

June 3, 2015 at 6:27 am

Yes it is running

June 3, 2015 at 7:14 am

Gotcha!

figured out issue with using "ApplicationPoolIdentity" in web api pool service identity. Now I am getting result by using network service