8 Replies Latest reply on Sep 1, 2016 7:10 AM by jantharan

    How to get a list of DRS rules and their virtual machines.

    aenagy Hot Shot

      If I try the following I don't get any output. (PowerShell 2, vCenter & ESXi 5.1, PowerCLI 5.1 Release 1)


      (get-cluster ).ExtensionData.ConfigurationEx.group|?{$_.vm}

      or

      (get-cluster ).ExtensionData.ConfigurationEx.group

       

      I know I have a DRS rule because when I try the following I am able to see it in the object browser in PowerGUI and I just created it in Web Client.

       

      $Clusters = Get-Cluster

        • 1. Re: How to get a list of DRS rules and their virtual machines.
          kunaludapi Expert
          vExpert

          (get-cluster).ExtensionData.ConfigurationEx.group.vm

          • 2. Re: How to get a list of DRS rules and their virtual machines.
            esstokes1 Enthusiast

            (get-cluster).ExtensionData.ConfigurationEx.group is an array and so is the vm object so you need to parse through them.  How about something like this:


            (get-cluster).ExtensionData.ConfigurationEx.group| foreach-object {

              $drsGroup = $_

              write-host "$($drsGroup.name)"

              foreach ($vm in $drsGroup.vm) {

               $vmname = (get-vm -id $vm).name

               write-host "$vmname"

              }

              echo ""

            }

            • 3. Re: How to get a list of DRS rules and their virtual machines.
              kunaludapi Expert
              vExpert

              $groups = ((get-cluster).ExtensionData.ConfigurationEx.group) | foreach {$_ | where-object {$_.gettype().name -eq "ClusterVmGroup"}}

              foreach ($group in $groups) {

                   $props = @{'Groupname'=$group.Name; 

                                 'Vm'= $group.vm | foreach {get-vm -id $_ }

                              }

                      $obj = New-Object -TypeName PSObject -Property $Props

                      $obj

              }

              • 4. Re: How to get a list of DRS rules and their virtual machines.
                aenagy Hot Shot

                This does not produce any output.

                • 5. Re: How to get a list of DRS rules and their virtual machines.
                  aenagy Hot Shot

                  I get the following output:

                   

                  Get-VM : Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument th at is not null or empty and then try the command again. At G:\Collab_Winserv\Lanserv\_scripts\VMware_vSphere\CTC_vSphere_DRS_rules\CTC_get_list_of_DRS_rules_and_ their_virtual_machines_esstokes1_ver01.ps1:7 char:25 +    $vmname = (get-vm -id <<<

                  • 6. Re: How to get a list of DRS rules and their virtual machines.
                    aenagy Hot Shot

                    I get the following output:

                     

                    You cannot call a method on a null-valued expression. At G:\Collab_Winserv\Lanserv\_scripts\VMware_vSphere\CTC_vSphere_DRS_rules\CTC_get_list_of_DRS_rules_and_their_virtual_machines_kunaludapi_ ver01.ps1:3 char:103 + $groups = ((get-cluster).ExtensionData.ConfigurationEx.group) | foreach {$_ | where-object {$_.gettype <<<< ().name -eq "ClusterVmGroup"} }     + CategoryInfo          : InvalidOperation: (gettype:String) [], RuntimeException     + FullyQualifiedErrorId : InvokeMethodOnNull Get-VM : Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or empty and then t ry the command again. At G:\Collab_Winserv\Lanserv\_scripts\VMware_vSphere\CTC_vSphere_DRS_rules\CTC_get_list_of_DRS_rules_and_their_virtual_machines_kunaludapi_ ver01.ps1:7 char:57 +                    'Vm'= $group.vm | foreach {get-vm -id <<<

                    • 7. Re: How to get a list of DRS rules and their virtual machines.
                      esstokes1 Enthusiast

                      The problem is that you have DRS groups that don't have vms - possibly host DRS groups.  If you only wants vms then something like this should work and not give the error:

                       

                      foreach ($cluster in (get-cluster)) {

                        write-host "$($cluster.name)"

                        foreach ($drsGroup in $cluster.ExtensionData.ConfigurationEx.group) {

                          write-host "$($drsGroup.name)"

                          foreach ($vm in $drsGroup.vm) {

                            if ($vm) {

                              $vmname = (get-vm -id $vm).name

                              write-host "$vmname"

                            }

                          }

                        }

                        echo ""

                      }

                       

                      If you want to get both VMhosts and VirtualMachines then try this:

                       

                      foreach ($cluster in (get-cluster)) {

                        write-host "$($cluster.name)"

                        foreach ($drsGroup in $cluster.ExtensionData.ConfigurationEx.group) {

                          write-host "$($drsGroup.name)"

                          write-host "VMHosts"

                          foreach ($vmhost in $drsGroup.host) {

                            if ($vmhost) {

                              $esx = (get-vmhost -id $vmhost).name

                              write-host "$esx"

                            }

                          }

                          echo ""

                          write-host "Virtual Machines"

                          foreach ($vm in $drsGroup.vm) {

                            if ($vm) {

                              $vmname = (get-vm -id $vm).name

                              write-host "$vmname"

                            }

                          }

                          echo ""

                        }

                        echo ""

                      }

                      • 8. Re: How to get a list of DRS rules and their virtual machines.
                        jantharan Lurker

                        For Single cluster:

                        Get-DrsRule -Cluster MyCluster | select Name,Cluster, Enabled, Type,  @{N="VMnames";E={ $_.Vmids|%{(get-view -id $_).name}}}

                        Get-DrsRule -Cluster MyCluster | select Name,Cluster, Enabled, Type,  @{N="VMnames";E={ $_.Vmids|%{(get-view -id $_).name}}} | export-csv -path c:\admin\drs_vms.csv

                         

                        For all clusters:

                        Get-DrsRule -Cluster * | select Name,Cluster, Enabled, Type,  @{N="VMnames";E={ $_.Vmids|%{(get-view -id $_).name}}}

                        Get-DrsRule -Cluster * | select Name,Cluster, Enabled, Type,  @{N="VMnames";E={ $_.Vmids|%{(get-view -id $_).name}}} | export-csv -path c:\admin\drs_vms.csv