3

I'm using this as a reference: http://msdn.microsoft.com/en-us/library/dd182449(v=VS.85).aspx

So the implementation is very similar. I'm executing the following PowerShell command to retrieve process information from the intended computer through the 1-liner command below:

$computer = "Remote.Computer.Here"; Get-Process -computer $computer | Sort-Object WorkingSet -desc | Select-Object -first 10 | Format-Table -property name, ID, @{Expression= {$_.WorkingSet/1mb};Label="MemoryLoad";} -auto

The command above executes perfectly in PS window. However, when called from C# app code, I get no returns. Especially so, when I access it through the following:

        PowerShell shell = PowerShell.Create();
        shell.AddScript(CmdletMap[PSVocab.OsProcLoad]);
        Collection<PSObject> obj = shell.Invoke();

        DataTable dt = new DataTable();
        dt.Columns.Add("ProcessName");
        dt.Columns.Add("ID");
        dt.Columns.Add("MemoryLoad");

        DataRow row;
        foreach (PSObject resultObject in obj)
        {
            row = dt.NewRow();
            row["ProcessName"] = resultObject.Members["name"].Value;
            row["ID"] = resultObject.Members["id"].Value;
            row["MemoryCol"] = resultObject.Members["MemoryLoad"].Value;

            dt.Rows.Add(row);
        }

Doing a quick-watch of resultObject.Members[].Value would simply return null.

Any help?

Thanks.

George Johnston
  • 31,652
  • 27
  • 127
  • 172
Jae
  • 105
  • 5
  • http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts – Kiquenet Mar 06 '13 at 10:01

4 Answers4

2

Inspect shell.Streams.Error to see what error is happening with your invocation of the script.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
2

In PowerShell, the default for a failing operation is to return nothing. PowerShell has several well-known streams, and your error is either lying in the error stream ([PowerShell].Streams.Error) or it is a terminating error ([Powershell].InvocationStateInfo.Reason).

Hope this helps,

Start-Automating
  • 8,067
  • 2
  • 28
  • 47
  • I inspected both for hints where the error might lie, however, both Streams.Error and InvocationStateInfo.Reason did not yield anything that would indicate there was an error. One thing I did find curious though is that if I add shell.AddCommand("Out-String") it would return the values.. only in string format. So it's definitely there, but being a pain to find than usual. – Jae Jul 19 '11 at 23:07
  • You should try looking @ psobject.immediateBaseObject instead of poking @ the property values thru the psobject (as your the code above does) – Start-Automating Jul 20 '11 at 05:36
  • http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts – Kiquenet Mar 06 '13 at 10:07
1

According to the Technet, your syntax is wrong. . .

http://technet.microsoft.com/en-us/library/dd347630.aspx

Syntax

Get-Process [[-Name] ] [-ComputerName ] [-FileVersionInfo] [-Module] []

Get-Process -Id [-ComputerName ] [-FileVersionInfo] [-Module] []

Get-Process -InputObject [-ComputerName ] [-FileVersionInfo] [-Module] []

Specifically, you need to use -computername, and not computer. And I have no idea what "Remote.Computer.Here" is. . .. you can use localhost.

edit

Nm, my coworker is an idiot. I just had to swap Remote.Computer.here with . and it looks all fine and dandy. See if that works.

surfasb
  • 968
  • 1
  • 13
  • 31
  • using 'computer' was a typo on my part. As for using localhost, I already tried that, and the results would still be the same. – Jae Jul 19 '11 at 22:19
  • Try a `.` instead of `'localhost'`. Actually, I have to modify that script heavily to make it work. I'm not a powershell expert, but he said that script needs some work. . . – surfasb Jul 20 '11 at 00:05
1

Use two slightly different commands: one for C# (and console) and another for console only.

For invoking from C# and console:

$computer = "."
Get-Process -computer $computer | Sort-Object WorkingSet -desc | Select-Object -first 10 |
Select-Object -property name, ID, @{Expression= {$_.WorkingSet/1mb};Label="MemoryLoad"}

For interactive host (i.e. console, ISE, etc.) with prettier look:

$computer = "."
Get-Process -computer $computer | Sort-Object WorkingSet -desc | Select-Object -first 10 |
Select-Object -property name, ID, @{Expression= {$_.WorkingSet/1mb};Label="MemoryLoad"} |
Format-Table -AutoSize

It is the Format-Table that makes problems in C#. Do not use it in C#. As for console, it should be the last command in the pipeline, it produces objects for printing, not for further use. Example: the first command shows two columns name and ID but the second command does not get any name properties:

Get-Process | Format-Table -property name, ID
Get-Process | Format-Table -property name, ID | Select-Object name
Roman Kuzmin
  • 40,627
  • 11
  • 95
  • 117
  • This. Format-Table returns formatting objects that are only intended for the console's format handling. Use Select-Object instead. – JasonMArcher Jul 20 '11 at 03:42