Skip to content

Instantly share code, notes, and snippets.

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

Embed
What would you like to do?
F#-ception. ExecutePsScript
let ExecPsScript<'T> script =
let runspaceConfiguration = RunspaceConfiguration.Create()
let runspaceAssemblies = runspaceConfiguration.Assemblies
let assembliesConfiguration =
PSUsableAssemblies
|> Array.map (fun asm -> new AssemblyConfigurationEntry(asm.FullName, asm.Location))
assembliesConfiguration |> runspaceAssemblies.Append
assembliesConfiguration
|> Array.map (fun asm -> asm.FileName)
|> Array.map (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.