F# friendly PowerShell Core helper
(1) The NuGet package FarNet.FSharp.PowerShell
may be used as usual in F# projects. In this case PowerShell Core comes
with its dependency Microsoft.PowerShell.SDK
(batteries
included because Windows PowerShell cannot be used).
(2) The package is also used by FarNet.FSharpFar
in Far Manager and its satellite fsx.exe
. In this case
PowerShell Core comes with another package FarNet.PowerShellFar.
To install FarNet packages, follow these steps.
Thus, you need these packages in Far Manager:
FarNet
- Far Manager .NET host and API for modulesFarNet.FSharpFar
- F# compiler service host and
toolsFarNet.PowerShellFar
- PowerShell Core, host and
toolsFarNet.FSharp.PowerShell
- this package connects 2. and
3. in 1.It looks like a lot but includes all the batteries and the power plant. This setup is portable with Far Manager, with caveats about prerequisites.
F# code
The PS
type wraps the PowerShell
class and exposes somewhat similar members.
Use PS.Create()
instead of
PowerShell.Create()
.
Use Script()
and Command()
instead of
AddScript
and AddCommand()
. PS
does not directly support command chains. But it is fine to invoke
several scripts and commands using the same PS
instance.
Use the type safe generic InvokeAs()
in addition to
Invoke()
. Result objects must be compatible with the
specified type.
Use F# asynchronous InvokeAsync()
and
InvokeAsyncAs()
. For parallel scenarios use different
PS
instances.
Use the operator ?
for getting PSObject
properties. Note, it also works for Hashtable
wrapped by
PSObject
.
PowerShell code
The default $ErrorActionPreference
is Stop
,
safe for non-interactive.
Use Get-Type
to get a type defined in the calling F#
assembly or script.
"Hello, world!" example:
open FarNet.FSharp.PowerShell
.Create().Script("Write-Output 'Hello, world!'").Invoke()
PS"%A" |> printfn