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.

1

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

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,

3 Comments

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.
You should try looking @ psobject.immediateBaseObject instead of poking @ the property values thru the psobject (as your the code above does)
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

1 Comment

This. Format-Table returns formatting objects that are only intended for the console's format handling. Use Select-Object instead.
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.

2 Comments

using 'computer' was a typo on my part. As for using localhost, I already tried that, and the results would still be the same.
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. . .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.