Guest User

Untitled

a guest
Jan 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. **#This is sample PowerShell function**
  2.  
  3. function Add-num {
  4.  
  5. param($int1,$int2)
  6.  
  7. Write-Host ($int1 + $int2)
  8.  
  9. }
  10.  
  11.  
  12. **#This is my c# code**
  13.  
  14. class Program
  15. {
  16.  
  17. static void Main(string[] args)
  18. {
  19. String file = @"E:powershellUntitled3.ps1";
  20.  
  21. Runspace runspace = RunspaceFactory.CreateRunspace();
  22. runspace.Open();
  23.  
  24. using (PowerShell PowerShellInstance = PowerShell.Create())
  25. {
  26. PowerShellInstance.AddScript(file);
  27. PowerShellInstance.AddCommand("Add-num").AddParameters(new Dictionary<string, string>
  28. {
  29. {"int1","6" },
  30. {"int2","7" }
  31. });
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. // Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
  39.  
  40. foreach (PSObject outputItem in PowerShellInstance.Invoke())
  41. {
  42. Console.WriteLine(outputItem.BaseObject);
  43. }
  44. runspace.Close();
  45. }
  46. }
  47. }
Add Comment
Please, Sign In to add comment