Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking powershell cmdlets from C#

Tags:

I'm trying to learn how to call PS cmdlets from C#, and have come across the PowerShell class. It works fine for basic use, but now I wanted to execute this PS command:

Get-ChildItem | where {$_.Length -gt 1000000} 

I tried building this through the powershell class, but I can't seem to do this. This is my code so far:

PowerShell ps = PowerShell.Create(); ps.AddCommand("Get-ChildItem"); ps.AddCommand("where-object"); ps.AddParameter("Length"); ps.AddParameter("-gt"); ps.AddParameter("10000");   // Call the PowerShell.Invoke() method to run the  // commands of the pipeline. foreach (PSObject result in ps.Invoke()) {     Console.WriteLine(         "{0,-24}{1}",         result.Members["Length"].Value,         result.Members["Name"].Value); } // End foreach. 

I always get an exception when I run this. Is it possible to run the Where-Object cmdlet like this?

like image 400
NullPointer Avatar asked Jun 12 '13 14:06

NullPointer


People also ask

How do I run cmdlets?

Run an old-fashioned command line (cmd.exe), type powershell and execute. Or, you can hit the PowerShell icon on the taskbar. Either way, you'll get a ready-to-use Windows PowerShell console. Use “Get-Help” cmdlet from before as a starting point for your journey.

How do I run a PowerShell script from the command line?

Running a PowerShell script from the Command Prompt If you would like to run a PowerShell script in CMD, you'll need to execute it by calling the PowerShell process with the -File parameter, as shown below: PowerShell -File C:\TEMP\MyNotepadScript. ps1. PowerShell -File C:\TEMP\MyNotepadScript.


1 Answers

Length, -gt and 10000 are not parameters to Where-Object. There is only one parameter, FilterScript at position 0, with a value of type ScriptBlock which contains an expression.

PowerShell ps = PowerShell.Create(); ps.AddCommand("Get-ChildItem"); ps.AddCommand("where-object"); ScriptBlock filter = ScriptBlock.Create("$_.Length -gt 10000") ps.AddParameter("FilterScript", filter) 

If you have more complex statements that you need to decompose, consider using the tokenizer (available in v2 or later) to understand the structure better:

    # use single quotes to allow $_ inside string PS> $script = 'Get-ChildItem | where-object -filter {$_.Length -gt 1000000 }' PS> $parser = [System.Management.Automation.PSParser] PS> $parser::Tokenize($script, [ref]$null) | select content, type | ft -auto 

This dumps out the following information. It's not as rich as the AST parser in v3, but it's still useful:

     Content                   Type     -------                   ----     Get-ChildItem          Command     |                     Operator     where-object           Command     -filter       CommandParameter     {                   GroupStart     _                     Variable     .                     Operator     Length                  Member     -gt                   Operator     1000000                 Number     }                     GroupEnd 

Hope this helps.

like image 75
x0n Avatar answered Oct 12 '22 18:10

x0n