Automation

 View Only
  • 1.  Get-View of ClusterComputeResource vs. StoragePod

    Posted Oct 14, 2015 03:39 AM

    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?



  • 2.  RE: Get-View of ClusterComputeResource vs. StoragePod

    Posted Oct 15, 2015 09:10 PM

    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;

                    }

                )

            }

        }

    }



  • 3.  RE: Get-View of ClusterComputeResource vs. StoragePod
    Best Answer

    Posted Oct 21, 2015 02:33 PM

    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;

                    }

                )

            }

        }

    }