Is it possible to execute PowerShell script inside c# the same way PowerShell is able to run vb/macro script inside PowerShell like so:
$script = @' (vb script) '
$excel.Run('Script', 'arg1', 'arg2', ...)
Is it possible to execute PowerShell script inside c# the same way PowerShell is able to run vb/macro script inside PowerShell like so:
$script = @' (vb script) '
$excel.Run('Script', 'arg1', 'arg2', ...)
This can be seen as a duplicate of this stackoverflow discussion(s)
Execute PowerShell Script from C# with Commandline Arguments
To call a powershell script file (example.ps1) from C#
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();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
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();