Home > Backend Development > C++ > How Can I Pass Command Line Arguments to PowerShell Scripts from C#?

How Can I Pass Command Line Arguments to PowerShell Scripts from C#?

Susan Sarandon
Release: 2025-01-23 22:57:10
Original
483 people have browsed it

How Can I Pass Command Line Arguments to PowerShell Scripts from C#?

Execute PowerShell scripts from C# using command line arguments

Integrating the ability to execute PowerShell scripts in C# applications can bring many advantages. However, to take full advantage of this feature, it's important to understand how to pass command line parameters to PowerShell scripts in C# code. This guide will explore this common scenario and provide a comprehensive solution to help you achieve seamless execution of PowerShell scripts and parameters.

Problem Definition

To execute a PowerShell script from C#, you might use the following code template:

<code class="language-csharp">RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();

RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.Add(scriptFile);

results = pipeline.Invoke();</code>
Copy after login

In this code, scriptFile represents the path to the PowerShell script (for example, "C:Program FilesMyProgramWhatever.ps1"). If your script uses command line arguments in the "-key Value" format (where Value can contain spaces), you may have difficulty passing these arguments effectively.

Solution

To solve this problem, you need to build a separate command object for the script file and specify the parameters using the CommandParameter object:

<code class="language-csharp">Command myCommand = new Command(scriptfile);
CommandParameter testParam = new CommandParameter("key", "value");
myCommand.Parameters.Add(testParam);
pipeline.Commands.Add(myCommand);</code>
Copy after login

This method ensures correct handling of command line arguments, including arguments containing spaces.

Full code example

To summarize, here is the complete modified code containing the solution:

<code class="language-csharp">RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();

Pipeline pipeline = runspace.CreatePipeline();

// 使用参数构建单独的命令
Command myCommand = new Command(scriptfile);
CommandParameter testParam = new CommandParameter("key", "value");
myCommand.Parameters.Add(testParam);

pipeline.Commands.Add(myCommand);

results = pipeline.Invoke();</code>
Copy after login

By integrating this solution into your C# code, you will be able to confidently execute PowerShell scripts and pass command line arguments, even if the arguments contain spaces. This enhanced functionality will enable you to seamlessly integrate PowerShell scripts into your C# applications.

The above is the detailed content of How Can I Pass Command Line Arguments to PowerShell Scripts from C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template