FSharp.Management
FSharp.Management copied to clipboard
TFailure of PsCmdletResult<TSuccess, TFailure> is not returned on errors
In the following script
#I @"packages\FSharp.Management\lib\net40"
#r "FSharp.Management.PowerShell.dll"
#r "System.Management.Automation"
open FSharp.Management
open System.Management.Automation
type PS = PowerShellProvider< "Microsoft.PowerShell.Management;Microsoft.PowerShell.Core" >
PS.``Get-Variable`` (name=[|"Host"|])
|> printfn "%A"
PS.``Get-Variable`` (name=[|"Missing"|])
|> printfn "%A"
PS.``Get-Variable`` (name=[|"Missing"|], errorAction=ActionPreference.Stop)
|> printfn "%A"
the type of PS.``Get-Variable``
is PsCmdletResult<List<PSVariable>,List<ErrorRecord>>
.
The script makes three calls:
-
Get-Variable
returns a variable, the result isSuccess [System.Management.Automation.PSVariable]
. -
Get-Variable
writes a non-terminating error, the result isSuccess []
. -
Get-Variable
writes a terminating error, this results in an exception.
[Loading ...\TestError.fsx]
Success [System.Management.Automation.PSVariable]
Success []
System.Management.Automation.ActionPreferenceStopException: The running command
stopped because the preference variable "ErrorActionPreference" or common
parameter is set to Stop: Cannot find a variable with the name 'Missing'.
...
Stopped due to error
Thus, TFailure
of PsCmdletResult<TSuccess, TFailure>
is never used.
Is this a bug? What is the expected result in (2) and (3), by design?
The design of the result type is not clear, though. It does not cover all
possible cases, for example a case when a command writes both data and
errors, e.g. with existing and missing variables specified at the same
time on Get-Variable
calls.