2 Replies Latest reply on Jun 15, 2020 2:47 AM by vin01

    Format-List: Unable to cast object of type

    vin01 Hot Shot

      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'



      $vm.Name |

      ForEach-Object -Process {

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


      $output |fl



        • 1. Re: Format-List: Unable to cast object of type
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          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

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Format-List: Unable to cast object of type
            vin01 Hot Shot

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