Skip to content

Instantly share code, notes, and snippets.

@bpatra bpatra/gist:6098637
Created Jul 28, 2013

What would you like to do?
F#-ception. ExecutePsScript
let ExecPsScript<'T> script =
let runspaceConfiguration = RunspaceConfiguration.Create()
let runspaceAssemblies = runspaceConfiguration.Assemblies
let assembliesConfiguration =
|> (fun asm -> new AssemblyConfigurationEntry(asm.FullName, asm.Location))
assembliesConfiguration |> runspaceAssemblies.Append
|> (fun asm -> asm.FileName)
|> (fun path -> new TypeConfigurationEntry(path))
|> runspaceConfiguration.Types.Append
use runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration)
do runspace.Open()
let scriptInvoker = new RunspaceInvoke(runspace)
scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted") |> ignore
let pipeline = runspace.CreatePipeline()
do pipeline.Commands.AddScript(script)
let psObjects = pipeline.Invoke()
if (psObjects.Count <> 1) then failwith "ExecPsScript should only return one psObject"
psObjects.[0].ImmediateBaseObject :?> 'T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.