Issue retrieving output with either Get-CIMInstance/gwmi cmdlets through C# code

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);
            // of System.Diagnostics.Process instances.

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

            StringBuilder stringresult = new StringBuilder();

            foreach (PSObject obj in results)

            StringBuilder stringVerbose = new StringBuilder();

            foreach (VerboseRecord obj in verbResult)

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

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?

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

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

Yes it is running


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