Skip to content

Type coersion exception when calling Get-ExecutionPolicy #84

Description

@cagyirey

Description

The type inference engine gives the wrong result type when trying to list ExecutionPolicy objects.

Repro steps

  1. Create a new folder and initialize paket with the following paket.dependencies
source https://nuget.org/api/v2
nuget FSharp.Management
nuget System.Management.Automation
  1. Run this script from the root directory. Call getExecutionPolicy() or getLocalExecutionPolicy() to observe the results.

Expected behavior

The type inferer should not try to cast from a PSObject directly to List<ExecutionPolicy>. Perhaps it's the BaseObject that contains the execution policy list?

Actual behavior

An exception with the message Object of type 'System.Management.Automation.PSObject' cannot be converted to type 'Microsoft.FSharp.Collections.FSharpList1[Microsoft.PowerShell.ExecutionPolicy]' is thrown.

Related information

  • Operating system: Windows 10
  • Branch: master
  • .NET 4.5

Repro

#I @".\packages\FSharp.Management\lib\net45"
#I @".\ackages\System.Management.Automation\lib\net40"

#r "FSharp.Management"
#r "FSharp.Management.PowerShell"
#r "System.Management.Automation"

open System
open System.Management.Automation

open Microsoft.PowerShell

open FSharp.Management

type PowerShell = PowerShellProvider<"Microsoft.PowerShell">

let private runCmdlet (cmd: _ -> PsCmdletResult<_, List<ErrorRecord>>) =
        let res = cmd ()
        match res with
        | Success psObj -> psObj
        | Failure err ->
            let ex =  (Seq.head err).Exception
            printfn "Error running cmdlet: %s" ex.Message; raise ex
    
let private unboxCmdlet (result: PsCmdletResult<_, List<ErrorRecord>>) =
    match result with
    | Success psObj -> psObj
    | Failure err -> raise (Seq.head err).Exception

// System.ArgumentException: Object of type 'System.Management.Automation.PSObject' cannot be converted to type 'Microsoft.FSharp.Collections.FSharpList`1[Microsoft.PowerShell.ExecutionPolicy]'.
let getExecutionPolicy () =
    PowerShell.``Get-ExecutionPolicy``(list=true)

// val it : PsCmdletResult<List<ExecutionPolicy>,List<ErrorRecord>> = Success [RemoteSigned {value__ = 1;}]
let getLocalExecutionPolicy () =
    PowerShell.``Get-ExecutionPolicy``(ExecutionPolicyScope.LocalMachine)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions