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