Instantly share code, notes, and snippets.

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