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

**URL:** <https://forums.powershell.org/t/issue-retrieving-output-with-either-get-ciminstance-gwmi-cmdlets-through-c-code/4286>\
**Category:** PowerShell Help\
**Created:** [May 29, 2015, 8:56am UTC](https://forums.powershell.org/t/issue-retrieving-output-with-either-get-ciminstance-gwmi-cmdlets-through-c-code/4286 "2015-05-29T08:56:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sushil-tiwari](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@sushil-tiwari](https://forums.powershell.org/u/sushil-tiwari)\
**Post date:** [May 29, 2015, 8:56am UTC](https://forums.powershell.org/t/issue-retrieving-output-with-either-get-ciminstance-gwmi-cmdlets-through-c-code/4286/1 "2015-05-29T08:56:39Z")

</div>

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() + " 
        }

```

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [May 29, 2015, 9:58pm UTC](https://forums.powershell.org/t/issue-retrieving-output-with-either-get-ciminstance-gwmi-cmdlets-through-c-code/4286/2 "2015-05-29T21:58:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sushil-tiwari](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@sushil-tiwari](https://forums.powershell.org/u/sushil-tiwari)\
**Post date:** [June 2, 2015, 1:15am UTC](https://forums.powershell.org/t/issue-retrieving-output-with-either-get-ciminstance-gwmi-cmdlets-through-c-code/4286/3 "2015-06-02T01:15:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [June 2, 2015, 3:53am UTC](https://forums.powershell.org/t/issue-retrieving-output-with-either-get-ciminstance-gwmi-cmdlets-through-c-code/4286/4 "2015-06-02T03:53:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sushil-tiwari](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@sushil-tiwari](https://forums.powershell.org/u/sushil-tiwari)\
**Post date:** [June 3, 2015, 12:57am UTC](https://forums.powershell.org/t/issue-retrieving-output-with-either-get-ciminstance-gwmi-cmdlets-through-c-code/4286/5 "2015-06-03T00:57:20Z")

</div>

Yes it is running

---

<div class="post-metadata">

**Author:** ![sushil-tiwari](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@sushil-tiwari](https://forums.powershell.org/u/sushil-tiwari)\
**Post date:** [June 3, 2015, 1:44am UTC](https://forums.powershell.org/t/issue-retrieving-output-with-either-get-ciminstance-gwmi-cmdlets-through-c-code/4286/6 "2015-06-03T01:44:11Z")

</div>

Gotcha!

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/issue-retrieving-output-with-either-get-ciminstance-gwmi-cmdlets-through-c-code/4286/7 "2024-05-16T20:45:22Z")

</div>


