Skip to content

Cannot convert value, Deserialized when running powershell from a pulished .net project #16525

@asaf-frieder

Description

@asaf-frieder

Prerequisites

Steps to reproduce

I am trying to open a remote WMI session in a powershell script I'm running from a .Net project (using System.Management.Automation.Powershell)
When running the script in powershell command line, or running the .net project directly from the visual studio the script works, however when publishing the .net project and running the published version it fails.
I receive the error while attempting to bind a parameter to the New-CimSession cmdlet generated by the New-CimSessionOption cmdlet. The New-CimSessionOption generates an object of type DComSessionOptions which is inherits the expected CimSessionOptions however it seems that it got deserialized (lost fidelity) and therefore the parameter failed to bind.
I have made several attempts to fix this using casting as well as create the dcom options my self and pass it explicitly as DComSessionOptions. In all cases attempting to run New-CimSession fails on the same error.

`
$SessionOption = New-CimSessionOption -Culture $Script:InvariantCulture -UICulture $Script:InvariantCulture -Protocol DCOM
$sessionParameters = @{
ComputerName = $ComputerAddress
SessionOption = $DcomOptions
Credential = $credentials
}

    # Create session
    $session = New-CimSession @sessionParameters -ErrorAction SilentlyContinue`

Published using:
dotnet publish C:\repos\WMI\WMITester\WMITester.csproj -c Release -r win7-x64 -o .\publish\WMITester\win\Release\x64 /p:PublishSingleFile=True /p:IncludeNativeLibrariesForSelfExtract=true /p:IncludeAllContentForSelfExtract=true /p:SelfContained=true /p:BuildNumber=8

Expected behavior

$session has a valid CimSession
(it happens this way in some modes of running the script)

Actual behavior

$session is null error detailed bellow.

Error details

Cannot bind parameter 'SessionOption'. Cannot convert value "Microsoft.Management.Infrastructure.Options.DComSessionOptions" to type "Microsoft.Management.Infrastructure.Options.CimSessionOptions". Error: "Cannot convert the "Microsoft.Management.Infrastructure.Options.DComSessionOptions" value of type "Deserialized.Microsoft.Management.Infrastructure.Options.DComSessionOptions" to type "Microsoft.Management.Infrastructure.Options.CimSessionOptions"

Environment data

$PSVersionTable.PSVersion 7.1.3
.Net 5.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.WG-DevEx-SDKhosting PowerShell as a runtime, PowerShell's APIs, PowerShell Standard, or development of modules

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions