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 Profile photo of Sushil Tiwari Sushil Tiwari 2 years, 1 month ago.

  • Author
    Posts
  • #25814
    Profile photo of Sushil Tiwari
    Sushil Tiwari
    Participant

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

  • #25820
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    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
    Profile photo of Sushil Tiwari
    Sushil Tiwari
    Participant

    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
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

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

  • #25915
    Profile photo of Sushil Tiwari
    Sushil Tiwari
    Participant

    Yes it is running

  • #25917
    Profile photo of Sushil Tiwari
    Sushil Tiwari
    Participant

    Gotcha!

    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.