I’m running a powershell script that attempts to use remote WMI. The script is run from .net (using System.Management.Automation.PowerShell)
$SessionOption = New-CimSessionOption -Culture $Script:InvariantCulture -UICulture $Script:InvariantCulture -Protocol DCOM
$sessionParameters = @{
ComputerName = $ComputerAddress
SessionOption = $SessionOption
Credential = $credentials
# Create session
$session = New-CimSession @sessionParameters -ErrorAction SilentlyContinue
The script is running fine when running it from visual studio, however, when attempting to run a published executable of the .net project I’m getting the following error from the script:
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”
From what I can see DComSessionOptions inherits from CimSessionOptions (and indeed New-CimSessionOption returns DComSessionOptions in both versions for -Protocol DCOM). It seems it cannot cast it to CimSessionOptions because it got deserialized in the published version. However, I do not understand why it got deserialized in the published version and not in the regular release. Does anyone know why publishing the project effect the script? Is there anyway to avoid the deserialization or reserialize it?
In both cases I’m using .net 5.0 and powershell 7.1.3
oO idea why. I can;t really repeat it here.
I suppose you can you try this:
$sessionParameters = @{
ComputerName = $ComputerAddress
SessionOption = [Microsoft.Management.Infrastructure.Options.DComSessionOptions] $SessionOption
Credential = $credentials
This forces the session option to be of the right type.
Thank you for your response. However, I have tried casting it this way, as well as several other castings. None of them worked.
Im not surprized it works for you it works for me as well when I’m not running the published version. It also worked when Im running it in a regular powershell console.
- [X] Write a descriptive title.
- [X] Make sure you are able… to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)
- [X] Search the existing issues.
- [X] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).
- [X] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://docs.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).
### 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
```console
$session has a valid CimSession
(it happens this way in some modes of running the script)
### Actual behavior
```console
$session is null error detailed bellow.
### Error details
```console
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
```powershell
$PSVersionTable.PSVersion 7.1.3
.Net 5.0
### Visuals
_No response_