开发者

Why do Powershell return as null when called from C# app code?

开发者 https://www.devze.com 2023-03-21 05:39 出处:网络
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 proce

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?

Th开发者_JAVA技巧anks.


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


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,


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消