Skip to content

Instantly share code, notes, and snippets.

@jeremybeavon
Last active October 30, 2015 01:47
Show Gist options
  • Select an option

  • Save jeremybeavon/6fdd0182e1c3dc8c092d to your computer and use it in GitHub Desktop.

Select an option

Save jeremybeavon/6fdd0182e1c3dc8c092d to your computer and use it in GitHub Desktop.
Run powershell script in C#
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