Last active
October 30, 2015 01:47
-
-
Save jeremybeavon/6fdd0182e1c3dc8c092d to your computer and use it in GitHub Desktop.
Run powershell script in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Management.Automation.Runspaces; | |
| public static class Powershell | |
| { | |
| public static T ExecuteScript<T>(string script, string returnVariableName) | |
| { | |
| using (Pipeline pipeline = RunspaceFactory.CreateRunspace().CreatePipeline()) | |
| { | |
| pipeline.Commands.AddScript(script); | |
| pipeline.Runspace.Open(); | |
| pipeline.Invoke(); | |
| return (T)pipeline.Runspace.SessionStateProxy.GetVariable(returnVariableName); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment