VMware Cloud Community
vin01
Expert
Expert
Jump to solution

Format-List: Unable to cast object of type

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

pastedImage_2.png

pastedImage_0.png

Regards Vineeth.K
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vin01
Expert
Expert
Jump to solution

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

Regards Vineeth.K
0 Kudos