How to call PowerShell cmdlets from C# in Visual Studio
Yes, you can call cmdlets from your C# code.
You'll need these two namespaces:
using System.Management.Automation;
using System.Management.Automation.Runspaces;
Open a runspace:
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
Create a pipeline:
Pipeline pipeline = runSpace.CreatePipeline();
Create a command:
Command cmd= new Command("APowerShellCommand");
You can add parameters:
cmd.Parameters.Add("Property", "value");
Add it to the pipeline:
pipeline.Commands.Add(cmd);
Run the command(s):
Collection output = pipeline.Invoke();
foreach (PSObject psObject in output)
{
....do stuff with psObject (output to console, etc)
}
Does this answer your question?
Invoking powershell cmdlets from C#
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.
Execute PowerShell Script from C# with Commandline Arguments
Try creating scriptfile as a separate command:
Command myCommand = new Command(scriptfile);
then you can add parameters with
CommandParameter testParam = new CommandParameter("key","value");
myCommand.Parameters.Add(testParam);
and finally
pipeline.Commands.Add(myCommand);
Here is the complete, edited code:
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
//Here's how you add a new script with arguments
Command myCommand = new Command(scriptfile);
CommandParameter testParam = new CommandParameter("key","value");
myCommand.Parameters.Add(testParam);
pipeline.Commands.Add(myCommand);
// Execute PowerShell script
results = pipeline.Invoke();
Related Topics
Reverse Sorted Dictionary in .Net
C# Parameterized Queries for Oracle - Serious & Dangerous Bug!
Encoding Trouble with Httpwebresponse
Efficient Way to Round Double Precision Numbers to a Lower Precision Given in Number of Bits
Call and Consume Web API in Winform Using C#.Net
Creating a Database Programmatically in SQL Server
Improving/Fixing a Regex for C Style Block Comments
Convert Datetime to a Specified Format
Using Graphics.Drawimage() to Draw Image with Transparency/Alpha Channel
No Access to the Session Information Through Signalr Hub. Is My Design Is Wrong
Publish a Project with Local Database
Entity Framework Creates a Plural Table Name, But the View Expects a Singular Table Name
How to Atomically Swap 2 Ints in C#
How to Find the Position of a Cursor in a Text Box? C#
Adding Multiple Icons (Win32-Resource) to .Net-Application