PowerCLI

 View Only
  • 1.  Format-List: Unable to cast object of type

    Posted Jun 15, 2020 07:50 AM

    Hello Community Members,

    I am seeing the below error when  I try to do Format-List for a array variable. What could be the issue?

    $vm=Get-VM 'Test01','Test02'

    $output=@()

    $vm.Name |

    ForEach-Object -Process {

    $output+=Invoke-VMScript -VM $_ -GuestUser '' -GuestPassword '' -ScriptText 'dir' -ScriptType Powershell -RunAsync -Confirm:$false

    }

    $output |fl



  • 2.  RE: Format-List: Unable to cast object of type
    Best Answer

    Posted Jun 15, 2020 09:34 AM

    This looks to be a bug in PowerCLI in PSv6 and later.
    Since you use the RunAsync switch, the result of the Invoke-VMScript cmdlet will be a Clien Side Task object.

    This kind of object os dynamically updated when the background task completes.

    Especially the Result property will now get the 'results' of the background task (like ExitCode, ScriptOutput...).

    The Format-List cmdlet uses casting instead of the ToString() member method on the objects.

    And that is where it goes wrong, the VMScriptResultImpl object (which is the Result property) apparently has an issue, in PSv6 and beyond, with this casting.

    I would definitely open an SR to report this bug.

    To bypass the issue you can leave out the RunAsync switch or use PSv5.1



  • 3.  RE: Format-List: Unable to cast object of type

    Posted Jun 15, 2020 09:48 AM

    Yeah You are right LucD. I have checked in psversion 5.1 and its showing up the results. Thanks letting me know the issue.