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?
Cannot use UpdateViewData when properties may be null. In my case I have datastores with no VMs so I can't UpdateViewData("ChildEntity.VM.Name").
$DSVMArray = @()
$DSClusterArray = Get-View -ViewType StoragePod
$DSClusterArray | % {
$DSCluster = $_
$DSCluster.UpdateViewData("ChildEntity.Name")
$DSClusterName = $DSCluster.Name
if ($DSCluster.LinkedView.ChildEntity.VM.Count -gt 0) {
$DSCluster.LinkedView.ChildEntity.UpdateViewData("VM.Name")
}
$DSCluster.LinkedView.ChildEntity | %{
$DSName = $_.Name
$_.LinkedView.VM | %{
$VMName = $_.Name
$DSVMArray += New-Object -TypeName PSObject -Prop (
@{'DSCluster'=$DSClusterName;
'Datastore'=$DSName;
'VM'=$VMName;
}
)
}
}
}
I had an issue similar to this when trying to use UpdateViewData on the Parent object. The work around I used was drilling down to the next level and issuing the UpdateViewData. Here is the code I just tested and worked in my environment.
$DSVMArray = @()
$DSClusterArray = Get-View -ViewType StoragePod
$DSClusterArray | % {
$_.UpdateViewData("ChildEntity.Name")
$DSClusterName = $_.Name
$_.LinkedView.ChildEntity.UpdateViewData("VM.Name")
$_.LinkedView.ChildEntity | % {
$DSName = $_.Name
$_.LinkedView.VM | % {
$VMName = $_.Name
$DSVMArray += New-Object -TypeName PSObject -Prop (
@{'DSCluster'=$DSClusterName;
'Datastore'=$DSName;
'VM'=$VMName;
}
)
}
}
}
Cannot use UpdateViewData when properties may be null. In my case I have datastores with no VMs so I can't UpdateViewData("ChildEntity.VM.Name").
$DSVMArray = @()
$DSClusterArray = Get-View -ViewType StoragePod
$DSClusterArray | % {
$DSCluster = $_
$DSCluster.UpdateViewData("ChildEntity.Name")
$DSClusterName = $DSCluster.Name
if ($DSCluster.LinkedView.ChildEntity.VM.Count -gt 0) {
$DSCluster.LinkedView.ChildEntity.UpdateViewData("VM.Name")
}
$DSCluster.LinkedView.ChildEntity | %{
$DSName = $_.Name
$_.LinkedView.VM | %{
$VMName = $_.Name
$DSVMArray += New-Object -TypeName PSObject -Prop (
@{'DSCluster'=$DSClusterName;
'Datastore'=$DSName;
'VM'=$VMName;
}
)
}
}
}