Home >Backend Development >C++ >How to Pass Command-Line Arguments with Spaces to a PowerShell Script from C#?

How to Pass Command-Line Arguments with Spaces to a PowerShell Script from C#?

Linda Hamilton
Linda HamiltonOriginal
2025-01-23 23:11:22346browse

How to Pass Command-Line Arguments with Spaces to a PowerShell Script from C#?

Passing Spaced Command-Line Arguments to PowerShell Scripts from C#

This guide addresses the challenge of executing PowerShell scripts from C# applications, specifically handling command-line arguments containing spaces.

The Problem: Directly passing arguments with spaces often leads to errors when invoking PowerShell scripts from C#.

The Solution: The key is to correctly encapsulate arguments within the command execution process. This example demonstrates a robust method using the System.Management.Automation namespace:

  1. Command Creation: Initiate a Command object, pointing to your PowerShell script's path.

    <code class="language-csharp">Command myCommand = new Command(scriptfile);</code>
  2. Parameter Definition: Define each command-line argument as a CommandParameter object. Crucially, ensure arguments with spaces are properly handled. This is typically done by enclosing them in double quotes.

    <code class="language-csharp">CommandParameter param1 = new CommandParameter("arg1", "value1");
    CommandParameter param2 = new CommandParameter("arg2", "\"value with spaces\""); // Note the double quotes</code>
  3. Parameter Addition: Add the CommandParameter objects to the Command object's Parameters collection.

    <code class="language-csharp">myCommand.Parameters.Add(param1);
    myCommand.Parameters.Add(param2);</code>
  4. Pipeline Execution: Integrate the Command into a PowerShell pipeline and execute it.

    <code class="language-csharp">RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
    Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.Add(myCommand);
    Collection<PSObject> results = pipeline.Invoke();
    runspace.Close();</code>

Complete Example:

This complete code snippet demonstrates how to execute a PowerShell script with spaced arguments:

<code class="language-csharp">string scriptfile = @"C:\path\to\your\script.ps1"; // Replace with your script path
string arg1 = "value1";
string arg2 = "value with spaces";

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();

Command myCommand = new Command(scriptfile);
myCommand.Parameters.Add(new CommandParameter("arg1", arg1));
myCommand.Parameters.Add(new CommandParameter("arg2", "\"" + arg2 + "\"")); //Escape spaces

Collection<PSObject> results = pipeline.Invoke();

runspace.Close();

// Process the results
foreach (PSObject result in results)
{
    Console.WriteLine(result.BaseObject);
}</code>

Remember to replace "C:pathtoyourscript.ps1" with the actual path to your PowerShell script. This improved solution ensures reliable execution even when dealing with arguments containing spaces.

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

Statement:
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