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

This topic contains 5 replies, has 2 voices, and was last updated by  Sushil Tiwari 2 years, 10 months ago.

  • Author
  • #25814

    Sushil Tiwari

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

  • #25820

    Richard Siddaway

    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?

  • #25881

    Sushil Tiwari

    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

  • #25887

    Richard Siddaway

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

  • #25915

    Sushil Tiwari

    Yes it is running

  • #25917

    Sushil Tiwari


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

You must be logged in to reply to this topic.