VMware Cloud Community
DZ1
Hot Shot
Hot Shot

Data truncated in report

This is something that I come across from time-to-time, and I usually just change up the entire script when this happens.

$GenReport = @()

Get-VM | foreach {

$vm = $_

$Report = "" | Select VM, Cluster, "DVdata", "HData"

$Report.VM = $vm.Name

$Report.Cluster = $vm.VMHost | Get-Cluster | Select -ExpandProperty Name

$Report."DVdata" = $vm.VMHost

$Report."HData" = $vm.VMHost | Get-Cluster | Get-VMHost | foreach { $_.Name, (($_ | Get-Datastore | Select -First 5 -ExpandProperty Name ) ) }

$GenReport += $Report

}

$GenReport

The grey line is the culprit.  The object for that shows a "System.Object[] Hosts in Cluster=System.Object[]"  Now, if I run the report and only select "Hdata", I can see everything, but it does not matter if I export the file, or even when I try and use -join, I can't see all of the data.  If I only try a foreach {} without using Select-Object, then I could see all my data, but every now and then I need to have certain columns for easy reference.  What can I do about this?

The data for the portion of the script in grey will show up as {Host, datastore datastore datastore, Host, datastore datastore datastore...}

Again, it's not really this particular script, I just need to know an overall way to see all the data when I create a report like the one above.  Thanks for any help.

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership

There is nothing wrong with the script, it's just that what you see is the way PowerShell displays an array of objects.

The PowerShell output engine doesn't know how to display that property, so it shows that it is an array.

You should store these values differently in the object ($report) you are creating, in fact in such a way that PowerShell knows how to display the property.

For example something like this.

With the Join method all the datastores are placed together in 1 string

$GenReport = @()

Get-VM | foreach {

    $vm = $_

    $Report = "" | Select VM, Cluster, "DVdata", "HData"

    $Report.VM = $vm.Name

    $Report.Cluster = Get-Cluster -VMHost $vm.VMHost | Select -ExpandProperty Name

    $Report."DVdata" = $vm.VMHost.Name

    $Report."HData" = [string]::Join((Get-Datastore -Location $vm.VMHost | Select -ExpandProperty Name),',')

    $GenReport += $Report

}

$GenRepo


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

DZ1
Hot Shot
Hot Shot

Thanks for the help LucD.

So it seems as if there isn't a way to really output data like that without changing up the entire script.  The main issue is that $Report."HData" = $vm.VMHost | Get-Cluster | Get-VMHost | foreach { $_.Name, (($_ | Get-Datastore | Select -First 5 -ExpandProperty Name ) ) } is because I want to see every host and all their datastores on one property, and I've tried to modify what I have with -join, but it seems like it cannot be accomplished that way.  I'll play around with it more, because I understand what you're saying with the output of System.Object[], so I'll keep playing around with it, or just modify the entire thing.  As always, thanks for the help. 

Reply
0 Kudos