Guest User

Untitled

a guest
Dec 13th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. string script = System.IO.File.ReadAllText(@"C:UsersBobDesktopCallPS.ps1");
  2.  
  3. using (Runspace runspace = RunspaceFactory.CreateRunspace())
  4. {
  5. runspace.Open();
  6. using (Pipeline pipeline = runspace.CreatePipeline(script))
  7. {
  8. Command c = new Command("BatAvg",false);
  9. c.Parameters.Add("Name", "John");
  10. c.Parameters.Add("Runs", "6996");
  11. c.Parameters.Add("Outs", "70");
  12. pipeline.Commands.Add(c);
  13.  
  14. Collection<PSObject> results = pipeline.Invoke();
  15. foreach (PSObject obj in results)
  16. {
  17. // do somethingConsole.WriteLine(obj.ToString());
  18. }
  19. }
  20. }
  21.  
  22. Function BatAvg
  23. {
  24. param ($Name, $Runs, $Outs)
  25. $Avg = [int]($Runs / $Outs*100)/100
  26. Write-Output "$Name's Average = $Avg, $Runs, $Outs "
  27. }
  28.  
  29. using (Runspace runspace = RunspaceFactory.CreateRunspace())
  30. {
  31. runspace.Open();
  32. PowerShell ps = PowerShell.Create();
  33. ps.Runspace = runspace;
  34. ps.AddScript(script);
  35. ps.Invoke();
  36. ps.AddCommand("BatAvg").AddParameters(new Dictionary<string, string>() {
  37. {"Name" , "John"}, {"Runs", "6996"}, {"Outs","70"}
  38. });
  39. foreach (PSObject result in ps.Invoke())
  40. {
  41. Console.WriteLine(result);
  42. }
  43. }
  44.  
  45. var ps = PowerShell.Create();
  46. ps.AddScript(script);
  47. ps.Invoke();
  48. ps.AddCommand("BatAvg").AddParameters(new Dictionary<string, string>
  49. {
  50. {"Name" , "John"}, {"Runs", "6996"}, {"Outs","70"}
  51. });
  52. foreach (var result in ps.Invoke())
  53. {
  54. Console.WriteLine(result);
  55. }
Add Comment
Please, Sign In to add comment