Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a powershell script file.
i need to run this script file in my c# code to execute it.
please help.

What I have tried:

i tried this code.
in the psobjects i am not getting results. please help


RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
Command scriptCommand = new Command(scriptFile);
Collection<commandparameter> commandParameters = new Collection<commandparameter>();
int i = 0;
foreach (string scriptParameter in scriptParameters.Split(','))
{
CommandParameter commandParm = new CommandParameter(scriptName.Split(',')[i], scriptParameter);
commandParameters.Add
C#

(commandParm);
scriptCommand.Parameters.Add(commandParm);
i++;
}
pipeline.Commands.Add(scriptCommand);
Collection<psobject> psObjects;
psObjects = pipeline.Invoke();
Posted
Updated 4-Sep-19 0:14am
v3
Comments
jimmson 4-Sep-19 5:56am    
What are the scriptParameters?

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900