PowerShell via C # Console

Pretty new for C #, not so new for PowerShell.

I want to use C # to execute PowerShell code. However, the output of the objects does not match the running commands in the PowerShell window. I return the name of the object, not its data. Here is what I am trying:

string script = @" get-process | ft "; PowerShell powerShell = PowerShell.Create(); powerShell.AddScript(script); var results = powerShell.Invoke(); foreach (var item in results) { Console.WriteLine(item); } 

Returns:

Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData p>

However, a more formalized view of the table is returned in the PowerShell command window:

PS C: \ Dropbox \ PowerShell> ps | feet

Processes NPM (K) PM (K) WS (K) VM (M) CPU (s) Id SI Process Name ------- ------ ----- ---- --- - ------ ------ ----------- 131 12 1980 2572 85 0.45 13216 1 acrotray

I really would like two things:

  • which will produce the same result as the PowerShell command prompt window
  • an explanation of what is happening, so I can learn to better navigate

Thanks in advance!

+5
source share
2 answers

I realized this, thanks to the codeproject code example, I realized that the "out-string" command should be added to the end of all commands. Therefore, the additional additional code:

ps.AddCommand ("Out-String");

I wonder if the console just does this or is there something else.

+3
source

PowerShell uses its own XML formatting document to return table style output to the console and ISE. Out-String is where this formatting comes into play in your case, as it will preserve formatting (you can use the "-Stream" switch with a string to return each line as it runs).

You can extract information from an object passed back to C # without using out-string; however, in my experience, if you just show this information; this is your best option to return a string. However, if you need to perform additional processing on an object, you can return the object, and then carry out conventions based on what type of object you are dealing with. For example, if I need to manipulate objects after they return from PowerShell (without using 'out-string'), I use the following:

System.Diagnostic.Process is the return type of the object from the Get-Process command. Make sure you know exactly what type of object you are returning in C #

 using (PowerShell psRunspace = PowerShell.Create()) { psRunspace.AddCommand("Get-Process"); psRunspace.AddParameter("ComputerName", computerName); Collection<PSObject> outputObj = psRunspace.Invoke(); foreach (PSObject obj in outputObj) { var objProperties = obj.Properties; var objMethods = obj.Methods; var baseObj = obj.BaseObject; if (baseObj is System.Diagnostic.Process) { var p = (System.Diagnostic.Process)baseObj; //you can now access all members of this object normally //Do something here with p.Properties or p.Methods etc... } } 

I would also recommend, since I don't see it in your question, consider using the 'using' statement to execute the powershell command. this will open and close the conveyor / space for you. In your example, it does not look like you closed this pipeline unless you excluded it from your sample code. Also, indicate in your invoke command that you are using the appropriate type for the return objects and use the collection, as shown above, with the return object type (PSObject).

0
source

Source: https://habr.com/ru/post/1238555/


All Articles