1 2 Previous Next 16 Replies Latest reply on Aug 22, 2011 11:55 AM by LucD

    How to increase speed get-vm get-view

    numulite Novice

      Hi,

       

      I'm new in PowerCLI.

      I'm using next script to obtain Virtual Machine Inventory:

       

      $outputFileVM = 'D:\PowerShell_scripts\Inventory.csv'

      $VMReport = @()

      get-vm | sort name | %{

      $VM = Get-View $_.ID

      $row = "" | Select-Object VM_Name, VM_Host, VM_Host_Model, VM_Memory, VM_vCpu, VM_CpuLimit, VM_CpuShares

      $row.VM_Name = $VM.Config.Name

      $row.VM_Host = Get-VMHost -VM $_

      $row.VM_Host_Model = Get-VMHost -VM $_ | Get-View | % {$_.Hardware.SystemInfo.Model}

      $row.VM_Memory = $VM.Config.Hardware.memoryMB

      $row.VM_vCpu = $VM.Config.Hardware.numCPU

      $row.VM_CpuLimit = $VM.Config.CpuAllocation.Limit

      $row.VM_CpuShares = $VM.Config.CpuAllocation.Shares.Shares

      $VMReport += $row

      }

      $VMReport | Export-Csv $outputFileVM -NoTypeInformation

       

      This script runs very slow. I read something about how to increase speed using get-view -viewType. But I don't know how to use it to refactory this script.

      Anyone can help me?

      Thanks.

       

       

       

      Eduard.

        • 1. Re: How to increase speed get-vm get-view
          LucD Guru

          You can replace the sequence

          Get-VM | Get-View
          

          by

          Get-View -ViewType VirtualMachine
          

           

           

          ____________

          Blog: LucD notes

          Twitter: lucd22

          • 2. Re: How to increase speed get-vm get-view
            numulite Novice

            Thanks LucD,

             

            I know it, but i don't know how to apply in this script.

            I'm beginner in PowerCLI...

            • 3. Re: How to increase speed get-vm get-view
              LucD Guru

              This should make it a bit faster

              $outputFileVM = 'D:\PowerShell_scripts\Inventory.csv'
              $VMReport = @()
              Get-View -ViewType VirtualMachine | Sort-Object -Property Name | %{
                   $row = "" | Select-Object VM_Name, VM_Host, VM_Host_Model, VM_Memory, VM_vCpu, VM_CpuLimit, VM_CpuShares
                   $row.VM_Name = $_.Name
                   $esx = Get-View $_.Runtime.Host
                   $row.VM_Host = $esx.Name
                   $row.VM_Host_Model = $esx.Hardware.SystemInfo.Model
                   $row.VM_Memory = $_.Config.Hardware.memoryMB
                   $row.VM_vCpu = $_.Config.Hardware.numCPU
                   $row.VM_CpuLimit = $_.Config.CpuAllocation.Limit
                   $row.VM_CpuShares = $_.Config.CpuAllocation.Shares.Shares
                   $VMReport += $row
              }
              $VMReport | Export-Csv $outputFileVM -NoTypeInformation
              

               

               

               

               

              ____________

              Blog: LucD notes

              Twitter: lucd22

              1 person found this helpful
              • 4. Re: How to increase speed get-vm get-view
                Novice

                To continue LucD's improvements and make it run very fast, make use of get-view's "-Property " parameter and specify the properties of the VirtualMachine or ESX host that should be retrieved. This prevents all the properties you aren't interested in using from being retrieved, and speeds up your code dramatically. In other words, replace your call to "Get-VM" with this:

                  Get-View -ViewType VirtualMachine -Property Name,Runtime.Host,Config.Hardware,Config.CpuAllocation
                

                because you are only interested in the Name, Runtime.Host, Config.Hardware, and Config.CpuAllocation properties of VirtualMachine. Replace your call to "Get-VMHost" in a similar fashion.

                 

                $outputFileVM = 'D:\PowerShell_scripts\Inventory.csv'
                $VMReport = @()
                Get-View -ViewType VirtualMachine -Property Name,Runtime.Host,Config.Hardware,Config.CpuAllocation | Sort-Object -Property Name | %{
                     $row = "" | Select-Object VM_Name, VM_Host, VM_Host_Model, VM_Memory, VM_vCpu, VM_CpuLimit, VM_CpuShares
                     $row.VM_Name = $_.Name
                     $esx = Get-View $_.Runtime.Host -Property Name,Hardware.SystemInfo.Model
                     $row.VM_Host = $esx.Name
                     $row.VM_Host_Model = $esx.Hardware.SystemInfo.Model
                     $row.VM_Memory = $_.Config.Hardware.memoryMB
                     $row.VM_vCpu = $_.Config.Hardware.numCPU
                     $row.VM_CpuLimit = $_.Config.CpuAllocation.Limit
                     $row.VM_CpuShares = $_.Config.CpuAllocation.Shares.Shares
                     $VMReport += $row
                }
                $VMReport | Export-Csv $outputFileVM -NoTypeInformation
                

                 

                For some more details on this, you may wish to check out: http://www.vmdev.info/?p=125

                 

                Thanks,

                Keshav

                • 5. Re: How to increase speed get-vm get-view
                  LucD Guru

                  Great article Keshav and indeed an extra speed improvement.

                  To give an idea of what these speed improvements are, I ran some tests

                  Original script                 100.0 %
                  Get-View                          9.7 %
                  Get-View & Property               3.9 % 
                  

                  In other words, Keshav's method is more than 25 times faster than the original script !

                   

                   

                   

                  ____________

                  Blog: LucD notes

                  Twitter: lucd22

                  • 6. Re: How to increase speed get-vm get-view
                    numulite Novice

                    Wow!!!

                     

                    My script: --> 5 minutes 51 seconds

                    LucD script --> 1 minutes 4 seconds !!!

                    Keshav08 script --> 12 seconds !!!

                     

                    My script is a turtle...

                     

                    Thanks!!!

                    • 7. Re: How to increase speed get-vm get-view
                      pmeqix Novice

                      In the latest script with get-view -viewitem how do i get the following additional info appended? i couldn't figure out from the document from VMW on this property

                       

                      - list clustername for the esx host

                      - list guest IP address, guest tools status, vm disk sizes, vm mac address

                       

                      any help is appreciated.

                       

                       

                       

                      $outputFileVM = 'c:\tmp\Inventory.csv'
                      $VMReport = @()
                      Get-View -ViewType VirtualMachine -Property Name,Runtime.Host,Config.Hardware,Config.CpuAllocation | Sort-Object -Property Name | %{
                           $row = "" | Select-Object VM_Name, VM_Host, VM_Host_Model, VM_Memory, VM_vCpu, VM_CpuLimit, VM_CpuShares
                           $row.VM_Name = $_.Name
                           $esx = Get-View $_.Runtime.Host -Property Name,Hardware.SystemInfo.Model
                           $row.VM_Host = $esx.Name
                           $row.VM_Host_Model = $esx.Hardware.SystemInfo.Model
                           $row.VM_Memory = $_.Config.Hardware.memoryMB
                           $row.VM_vCpu = $_.Config.Hardware.numCPU
                           $row.VM_CpuLimit = $_.Config.CpuAllocation.Limit
                           $row.VM_CpuShares = $_.Config.CpuAllocation.Shares.Shares
                           $VMReport += $row
                      }
                      $VMReport | Export-Csv $outputFileVM -NoTypeInformation

                       

                      • 8. Re: How to increase speed get-vm get-view
                        LucD Guru

                        Try this version

                         

                        $outputFileVM = 'c:\tmp\Inventory.csv' 
                        $VMReport
                        = @() Get-View -ViewType VirtualMachine -Property Name,Runtime.Host,Config.Hardware,Config.CpuAllocation,Guest.Net,Guest.ToolsStatus, Guest.ToolsVersionStatus | Sort-Object -Property Name | %{     $row = "" | Select-Object VM_Name, VM_Host, VM_Host_Model, VM_Host_Cluster,VM_Memory, VM_vCpu, VM_CpuLimit, VM_CpuShares, VM_IP, VM_MAC, VM_Tools_Status, VM_Tools_VersionSTatus, VM_Harddisk_SizeKB    $row.VM_Name = $_.Name     $esx = Get-View $_.Runtime.Host -Property Name,Hardware.SystemInfo.Model,Parent     $row.VM_Host = $esx.Name     $row.VM_Host_Model = $esx.Hardware.SystemInfo.Model     $row.VM_Host_Cluster = (Get-View $esx.Parent).Name     $row.VM_Memory = $_.Config.Hardware.memoryMB     $row.VM_vCpu = $_.Config.Hardware.numCPU     $row.VM_CpuLimit = $_.Config.CpuAllocation.Limit     $row.VM_CpuShares = $_.Config.CpuAllocation.Shares.Shares     if($ip = ($_.Guest.Net | %{$_.IpAddress})){         $row.VM_IP = [string]::Join(',',$ip)     }     if(($mac = $_.Guest.Net | %{$_.MacAddress})){         $row.VM_MAC = [string]::Join(',',$mac)     }     $row.VM_Tools_Status = $_.Guest.ToolsStatus     $row.VM_Tools_VersionSTatus = $_.Guest.ToolsVersionStatus     if(($hdSize = $_.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} | %{$_.CapacityInKB})){         $row.VM_Harddisk_SizeKB = [string]::Join(',',$hdSize)     }     $VMReport += $row
                        }
                        $VMReport | Export-Csv $outputFileVM -NoTypeInformation
                        • 9. Re: How to increase speed get-vm get-view
                          pmeqix Novice

                          I have deleted the error message as there was indeed and CR/LF issue that i was finally able to track down. the script appears to be running without errors at this point in time. Once complete and results are verified I will post the final script as attachment for others to use.

                          • 10. Re: How to increase speed get-vm get-view
                            pmeqix Novice

                            OK I have a  fully working script now which also does the math to show HDD size in GB and beauty of this script  is that the size includes comma separated values for all the HDDs that are created for a given VM.

                             

                            Major thanks to LucD for assisting in re-engineering the script.

                            • 11. Re: How to increase speed get-vm get-view
                              pizzim13 Novice

                              Is it possible to use get-view's filter parameter with multiple properties?

                               

                              Works:

                              Get-View -ViewType VirtualMachine -Filter @{"Guest.toolsVersionStatus" = "guestToolsUnmanaged"}
                              

                               

                              Doesn't work:

                              Get-View -ViewType VirtualMachine -Filter @{"Guest.toolsVersionStatus" = "guestToolsUnmanaged"} `
                              -and @{"RunTime.PowerState" = "PoweredOn"}
                              
                              • 12. Re: How to increase speed get-vm get-view
                                LucD Guru

                                The filter condition(s) need to be passed as a hash table.

                                A hash table with multiple entries is constructed like this

                                 

                                Get-View -ViewType VirtualMachine `
                                -Filter @{"Guest.toolsVersionStatus" = "guestToolsUnmanaged";"RunTime.PowerState" = "PoweredOn"}
                                • 13. Re: How to increase speed get-vm get-view
                                  pizzim13 Novice

                                  That returns an error:

                                   

                                  8/22/2011 2:21:39 PM    Get-View              

                                  At :line:1 char:9

                                  + Get-View  <<<< -ViewType VirtualMachine `

                                  • 14. Re: How to increase speed get-vm get-view
                                    pizzim13 Novice

                                    Also, is there an equivalent of != for filters?

                                    1 2 Previous Next