public void Run(string name) { // Executionpolicy Bypass - Thanks to @ducke // InitialSessionState initial = InitialSessionState.CreateDefault(); // Replace PSAuthorizationManager with a null manager // which ignores execution policy initial.AuthorizationManager = new AuthorizationManager("Microsoft.PowerShell"); //Create Runspace var runspace = RunspaceFactory.CreateRunspace(initial); runspace.Open(); //Create Pipeline var pipeline = runspace.CreatePipeline(); //Pass in the Scripts text pipeline.Commands.AddScript(ScriptIO.Read(name)); pipeline.Commands.Add("Out-String"); //Invoke and Save Results var results = pipeline.Invoke(); SaveResults(name, results); runspace.Close(); }
public void Run(string name, Dictionary <string, string> psParams) { // Executionpolicy Bypass - Thanks to @ducke // InitialSessionState initial = InitialSessionState.CreateDefault(); // Replace PSAuthorizationManager with a null manager // which ignores execution policy initial.AuthorizationManager = new AuthorizationManager("Microsoft.PowerShell"); //Create Runspace var runspace = RunspaceFactory.CreateRunspace(initial); runspace.Open(); //Create Pipeline Pipeline pipeline = runspace.CreatePipeline(); //TODO: Testing - Please REMOVE! var command = new Command(ScriptIO.FileLocation(name)); if (psParams != null) { //TODO: DoEs This Need Item Key or Will Params Run in Order? foreach (var item in psParams) { command.Parameters.Add(null, item.Value); } } pipeline.Commands.Add(command); //Pass in the Scripts text //pipeline.Commands.AddScript(scriptContents); //Previous Way - would allow for script storage in DB pipeline.Commands.Add("Out-String"); //Invoke and Save Results var results = pipeline.Invoke(); SaveResults(name, results); runspace.Close(); }