question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PowershellDirect on Windows Server 2016: Invoke-Command -VMName 'blah' fails with Unable to cast object of type 'System.String' to type 'VMState'.

See original GitHub issue

Steps to reproduce

$vmUserPassword = 'password'; #reproes even without correct password
$vmUser = '.\Administrator'; 
$vmMachineName = 'MyLocalVMName';  #this VM needs to exist
$password = ConvertTo-SecureString $vmUserPassword -AsPlainText -Force; 
$cred = New-Object System.Management.Automation.PSCredential ($vmUser, $password); 
Invoke-Command -VMName $vmMachineName -Credential $cred { Write-Output ('Connection'+'Succeeded') }

Expected behavior

ConnectionSucceeded

Actual behavior

Invoke-Command: Unable to cast object of type 'System.String' to type 'VMState'.

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.1
PSEdition                      Core
GitCommitId                    7.1.1
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Notes:

  • this is about the host, rather than guest guest:
    • Win10 or Windows Server 2019 host + Windows Server 2019 guest => works
    • Windows Server 2016 host + Windows Server 2019 guest => breaks
  • this is specific to powershell core
    • the same command works from Powershell ISE 5.1.14393.3866 from either host
  • In my actual environment I use powershell SDK from c# app, but thankfully it reproes from regular powershell
  • Similar unanswered question on stackoverflow .

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
SeeminglySciencecommented, Feb 9, 2021

I’m guessing the 2016 version is using implicit remoting (or w/e the automatic wincompat layer is called, I forget) due to the HyperV module not getting the Core tag via Windows Updates. As a workaround try Import-Module HyperV -SkipEditionCheck before running the command.

1reaction
ayeltsovcommented, Mar 4, 2021

Ack. We’re ok with workaround, and will probably speed up deprecation of Server 2016 as a supported host. Not sure what is the protocol for closing the bugs, but I’m Ok with closing

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manage Windows Virtual Machines with PowerShell Direct
Problem : Enter-PSSession , Invoke-Command , or New-PSSession do not have a -VMName or -VMId parameter. Potential causes: The most likely issue ......
Read more >
PowerShell issue: Invoke method "Cannot cast object of ...
Once running the code, an error occurs in the function Invoke, it is saying that "it cannot cast System.String to the type System.String[]"....
Read more >
Manage Windows virtual machines with PowerShell Direct
In this article. Create and exit a PowerShell Direct session using PSSession cmdlets; Run script or command with Invoke-Command cmdlet; What's ...
Read more >
Invoke Power Shell: Unable to cast object of type 'System. ...
I am facing this issue ->Invoke Power Shell: Unable to cast object of type 'System.String' to type 'System.Management.Automation.PSModuleInfo.
Read more >
PowerShell Direct Deep Dive HyperV #WS2016 ...
Hello folks,. Microsoft introduced a new feature in Windows Server 2016 called PowerShell Direct. What is PowerShell Direct?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found