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();
Ijas
  • 367
  • 1
  • 3
  • 18
  • Have you tried using the `AddArgument` or `AddParameter` methods described in the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.powershell?view=pscore-6.2.0)? – fredrik Apr 29 '20 at 08: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 – Ijas Apr 29 '20 at 08:42
  • If it is not in the documentation, no. – fredrik Apr 29 '20 at 09:03
  • Tried this? https://stackoverflow.com/questions/57646703/passing-argument-from-c-sharp-to-called-powershell-script – Alex Apr 29 '20 at 11:04

1 Answers1

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 
Manish
  • 1,139
  • 7
  • 21
  • how can i pass arguments using this method – Ijas Apr 29 '20 at 09:11
  • 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 – Manish Apr 29 '20 at 09:14
  • `Arguments = $"-NoProfile -ExecutionPolicy unrestricted \"{ps1File}\" arg1 arg2",` this is not working – Ijas Apr 29 '20 at 09:22
  • 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 – Manish Apr 29 '20 at 09:25
  • `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 – Ijas Apr 29 '20 at 10:06
  • i tried this but not working `$servername=$args[0] $envname=$args[1] New-Item -Path "C:\$servername $envname.txt" -ItemType File ` I used this script file and after executing code file name with passed arguments are not created – Ijas Apr 29 '20 at 10:37
  • $servername=$args[0] $envname=$args[1] New-Item -Path $servername -Name "$envname.txt" -ItemType File – Manish Apr 29 '20 at 10:48
  • I modified it a bit – Manish Apr 29 '20 at 10:48
  • var processStartInfo = new ProcessStartInfo() { FileName = "powershell.exe", Arguments = @$"-NoProfile -ExecutionPolicy unrestricted -file {psData} C:\ hello", UseShellExecute = false }; – Manish Apr 29 '20 at 10:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212776/discussion-between-iceberg-and-manish). – Ijas Apr 29 '20 at 10:50