1

I have a powershell script i need to execute it using c#

string psData = System.IO.File.ReadAllText(@"C:\Users\m\Desktop\power.ps1");
        using (PowerShell PowerShellInstance = PowerShell.Create())
        {
            PowerShellInstance.AddScript(psData);

            IAsyncResult result = PowerShellInstance.BeginInvoke();
            while (!result.IsCompleted)
            {
                Logger.Info("Wait initiated");
                Thread.Sleep(5000);
            }
            Logger.Info("Execution completed");
        }

Using above code script is executing but how can i pass arguments to shell script

I have changed code

string psData = System.IO.File.ReadAllText(@"C:\Users\m\Desktop\power.ps1");
        Process pr = new Process();
        var processStartInfo = new ProcessStartInfo()
        {
            FileName = "powershell.exe",
            Arguments = $"-NoProfile -ExecutionPolicy unrestricted -file \""+psData+"\" \"arg1\" \"arg2\"",
            UseShellExecute = false
        };
        pr.StartInfo = processStartInfo;
        pr.Start();
        pr.WaitForExit();
4
  • Have you tried using the AddArgument or AddParameter methods described in the documentation? Commented Apr 29, 2020 at 8:08
  • @fedrik can i pass all my arguments as a string in one .AddArgument()? it seems for all arguments i need to add them separately Commented Apr 29, 2020 at 8:42
  • If it is not in the documentation, no. Commented Apr 29, 2020 at 9:03
  • Tried this? stackoverflow.com/questions/57646703/… Commented Apr 29, 2020 at 11:04

1 Answer 1

2

Use AddArgument Method.
More Info -> https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.powershell.addargument?view=pscore-6.2.0

-------- Old Answer-----------------

Not Sure about How to do it via PowerShellInstance class, But you can use Process class for invoking PowerShell script with argument.

var file = @"C:\<Directory>\GetServices.ps1";
var processStartInfo = new ProcessStartInfo()
{
    FileName = "powershell.exe",
    Arguments = $"-NoProfile -ExecutionPolicy unrestricted \"{ps1File}\"",
    UseShellExecute = false
};
Process.Start(startInfo);

For more info -> https://duanenewman.net/blog/post/running-powershell-scripts-from-csharp/
Edit 2

  string psData = @"C:\Users\manis\Desktop\test.ps1";
    Process pr = new Process();
    var processStartInfo = new ProcessStartInfo()
    {
        FileName = "powershell.exe",
        Arguments = $"-NoProfile -ExecutionPolicy unrestricted -file {psData} hello",
        UseShellExecute = false
    };
    pr.StartInfo = processStartInfo;
    pr.Start();
    pr.WaitForExit();

And my Powershell script looks like this

param ($param1)
write-host $param1 
Sign up to request clarification or add additional context in comments.

10 Comments

how can i pass arguments using this method
same as you run in PowerShell, example command -param1 value, in this case, it will be like -Param1 value1 - Param2 value2 in argument property, all will be in string @Iceberg
Arguments = $"-NoProfile -ExecutionPolicy unrestricted \"{ps1File}\" arg1 arg2", this is not working
In this example, -NoProfile and ExecutionPolicy is argument, you can remove those, and add your argument, make sure that your script is running correctly in power shell
powershell.exe -NoProfile -ExecutionPolicy unrestricted -file "C:\Users\m\Desktop\power.ps1" "arg1" "arg2" this code works in cmd. But when i pass same code in arguments inside c# it is not working
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.