添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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