Execute Powershell Script from C# With Commandline Arguments

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();

Using C# to execute PowerShell script with command line args using V2 methods

Answering my own question here. It looks like the right approach would be for my application to function as a PowerShell Host

https://msdn.microsoft.com/en-us/library/ee706563(v=vs.85).aspx

Use of PowerShell V2 objects is defined here:
http://msdn.microsoft.com/en-us/library/windows/desktop/system.management.automation.powershell(v=vs.85).aspx

Execute PowerShell Script of flie from C# with Commandline Arguments

Your expression ls . consists of a command (or rather, an alias) ls and a parameter argument .

The proper way to construct that expression would be:

Command lsCmd = new Command("ls");
lsCmd.Parameters.Add("Path",".");
Pipeline.Commands.Add(lsCmd);

How to pass arguments to a powershell script from C# program?

here are the anwers to you question:

  • Execute PowerShell Script from C# with Commandline Arguments
  • Invoking PowerShell Script with Arguments from C#
  • How could call powershell script in c# with parameters
  • Passing parameters to powershell script in C#

How to pass parameters to a powershell script executed from C#?

Assuming that you can use C# v6+ interpolated strings, and assuming that the arguments you're trying to pass to script ScriptName are stored in variables Source, Target, and UserId:

process.StartInfo.Arguments = $"-File \"{ScriptName}\" \"{Source}\" \"{Target}\" \"{UserId}\"";

How to execute a PowerShell script using c#

None of the answers here helped me but this answer did and was taken from this blog post so credit to the author.

https://duanenewman.net/blog/post/running-powershell-scripts-from-csharp/

Here's the full code that will run a PowerShell script when you create a console app in Visual Studio that will ByPass any restrictions.

Just change the ps1File variable to your needs below.

using System;
using System.Diagnostics;

namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{

InstallViaPowerShell();
}

public static void InstallViaPowerShell()
{

var ps1File = @"C:\Users\stevehero\source\repos\RGB Animated Cursors PowerShell\RGB Animated Cursors\install-scheme.ps1";

var startInfo = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy ByPass -File \"{ps1File}\"",
UseShellExecute = false
};
Process.Start(startInfo);

}
}
}

How to execute a powershell script using c# and setting execution policy?

Without knowing what your specific problem was, note that your C# code can be greatly streamlined, which may also resolve your problem:

  • There is no need to resort to reflection in order to set a session's execution policy.

  • Using an instance of the PowerShell class greatly simplifies command invocation.

// Create an initial default session state.
var iss = InitialSessionState.CreateDefault2();
// Set its script-file execution policy (for the current session only).
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Bypass;

// Create a PowerShell instance with a runspace based on the
// initial session state.
PowerShell ps = PowerShell.Create(iss);

// Add the command (script-file call) and its parameters, then invoke.
var results =
ps
.AddCommand(scriptfile)
.AddParameter("key", "value")
.Invoke();

Note: The .Invoke() method only throws an exception if a terminating error occurred during execution of the PowerShell script. The more typical non-terminating errors are instead reported via .Streams.Error.



Related Topics



Leave a reply



Submit