I found the following example of looping through cluster/host/vm at PowerCLI LinkedView – A Simple Example | The Handsome Hippo
$VMArray = @()
$ClusterArray = Get-View -ViewType ClusterComputeResource
$ClusterArray | % {
$_.UpdateViewData("Host.Name","Host.VM.Name")
$ClusterName = $_.Name
$_.LinkedView.Host | % {
$HostName = $_.Name
$_.LinkedView.VM | % {
$VMName = $_.Name
$VMArray += New-Object –TypeName PSObject –Prop (
@{'Cluster'=$ClusterName;
'Host'=$HostName;
'Name'=$VMName
}
)
}
}
}
I tried the equivalent code for storage cluster/datastore/vm
$DSVMArray = @()
$DSClusterArray = Get-View -ViewType StoragePod
$DSClusterArray | % {
$_.UpdateViewData("ChildEntity.Name","ChildEntity.VM.Name")
$DSClusterName = $_.Name
$_.LinkedView.ChildEntity | % {
$DSName = $_.Name
$_.LinkedView.VM | % {
$VMName = $_.Name
$DSVMArray += New-Object -TypeName PSObject -Prop (
@{'DSCluster'=$DSClusterName;
'Datastore'=$DSName;
'VM'=$VMName;
}
)
}
}
}
But this errors out with
Exception calling "UpdateViewData" with "2" argument(s): "The specified path
is not correct. Element 'vM' doesn't exist."
At line:4 char:5
+ $_.UpdateViewData("ChildEntity.Name","ChildEntity.VM.Name")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException
The data structures look the same to me. Would someone please explain the error?