Execute PowerShell Script from C# with Commandline Arguments


SUBMITTED BY: Guest

DATE: Nov. 14, 2013, 7:05 a.m.

FORMAT: Text only

SIZE: 3.4 kB

HITS: 887

  1. RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
  2. Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
  3. runspace.Open();
  4. RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
  5. Pipeline pipeline = runspace.CreatePipeline();
  6. pipeline.Commands.Add(scriptFile);
  7. // Execute PowerShell script
  8. results = pipeline.Invoke();
  9. Command myCommand = new Command(scriptfile);
  10. CommandParameter testParam = new CommandParameter("key","value");
  11. myCommand.Parameters.Add(testParam);
  12. pipeline.Commands.Add(myCommand);
  13. ProcessStartInfo startInfo = new ProcessStartInfo();
  14. startInfo.FileName = @"powershell.exe";
  15. startInfo.Arguments = @"& 'c:Scriptstest.ps1'";
  16. startInfo.RedirectStandardOutput = true;
  17. startInfo.RedirectStandardError = true;
  18. startInfo.UseShellExecute = false;
  19. startInfo.CreateNoWindow = true;
  20. Process process = new Process();
  21. process.StartInfo = startInfo;
  22. process.Start();
  23. string output = process.StandardOutput.ReadToEnd();
  24. Assert.IsTrue(output.Contains("StringToBeVerifiedInAUnitTest"));
  25. string errors = process.StandardError.ReadToEnd();
  26. Assert.IsTrue(string.IsNullOrEmpty(errors));
  27. $someVariable = "StringToBeVerifiedInAUnitTest"
  28. $someVariable
  29. string cmdArg = ".script.ps1 -foo bar"
  30. Collection<PSObject> psresults;
  31. using (Pipeline pipeline = _runspace.CreatePipeline())
  32. {
  33. pipeline.Commands.AddScript(cmdArg);
  34. pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
  35. psresults = pipeline.Invoke();
  36. }
  37. return psresults;
  38. private static void RunPowershellScript(string scriptFile, string scriptParameters)
  39. {
  40. RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
  41. Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
  42. runspace.Open();
  43. RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
  44. Pipeline pipeline = runspace.CreatePipeline();
  45. Command scriptCommand = new Command(scriptFile);
  46. Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();
  47. foreach (string scriptParameter in scriptParameters.Split(' '))
  48. {
  49. CommandParameter commandParm = new CommandParameter(null, scriptParameter);
  50. commandParameters.Add(commandParm);
  51. scriptCommand.Parameters.Add(commandParm);
  52. }
  53. pipeline.Commands.Add(scriptCommand);
  54. Collection<PSObject> psObjects;
  55. psObjects = pipeline.Invoke();
  56. }
  57. pipeline.Commands.AddScript(script));
  58. FillVariables(pipeline, scriptParameter);
  59. Collection<PSObject> results = pipeline.Invoke();
  60. private static void FillVariables(Pipeline pipeline, Hashtable scriptParameters)
  61. {
  62. // Add additional variables to PowerShell
  63. if (scriptParameters != null)
  64. {
  65. foreach (DictionaryEntry entry in scriptParameters)
  66. {
  67. CommandParameter Param = new CommandParameter(entry.Key as String, entry.Value);
  68. pipeline.Commands[0].Parameters.Add(Param);
  69. }
  70. }
  71. }
  72. Object resultcollection = runspace.SessionStateProxy.GetVariable("results");

comments powered by Disqus