1 2 Previous Next 15 Replies Latest reply on Jan 6, 2016 7:29 PM by leaderone2

    powercli script to get cluster name, datastore name, total size & free space on DS for a VM

    aryan14in Enthusiast

      Hi All,

      need help to get  cluster name, datastore name, total size and available space on DS for a VM

       

      I have referred below links

      http://ict-freak.nl/2009/11/17/powercli-one-liner-to-get-vms-clusters-esx-hosts-and-datastores/comment-page-1/#comment-15394

      +++++++++++++++++

      Get-VM | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}}, `
      @{N="ESX Host";E={Get-VMHost -VM $_}}, `
      @{N="Datastore";E={Get-Datastore -VM $_}} | `
      Export-Csv -NoTypeInformation C:\Scripts\VM_CLuster_Host_Datastore.csv

      +++++++++++++++++

       

       

      I have modified above script as per requirement:

       

      $vmname = Import-csv "C:\vcp-vm.csv"

      foreach ($vm.name in $vmname)
      {
      Get-VM $vm.name | Select Name, @{N="Cluster";E={Get-Cluster -VM $vm.name}}, `
      @{N="ESX Host";E={Get-VMHost -VM $vm.name}}, `
      @{N="Datastore";E={Get-Datastore -VM $vm.name}} | `
      Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv
      }

       

       

      could you plese suggest on adding datastore information

       

      Thanks!

        • 1. Re: powercli script to get cluster name, datastore name, total size & free space on DS for a VM
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          Try this one

           

          $vmnames = Import-csv "C:\vcp-vm.csv"
          $vmnames | %{
              $vm = Get-VM $_.Name
              $ds = Get-Datastore -VM $vm
             
          $vm | Select Name, @{N="Cluster";E={Get-Cluster -VM $vm.name}},
                  @{N="ESX Host";E={(Get-VMHost -VM $vm.name).Name}},
                @{N="Datastore";E={$ds.Name}},         @{N="DS Capacity";E={$ds.CapacityMB}},         @{N="DS Free";E={$ds.FreeSpaceMB}} } | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv

           

          Note that the script doesn't take into account when a VM is using multiple datastores!

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: powercli script to get cluster name, datastore name, total size & free space on DS for a VM
            aryan14in Enthusiast

            Thanks! Luc. This is working as it was asked for..

             

            As you mentioned in your note that the script doesn't take into account when a VM is using multiple datastores. Is it possible to include those details in script.

            • 3. Re: powercli script to get cluster name, datastore name, total size & free space on DS for a VM
              LucD Guru
              User ModeratorsvExpertCommunity Warriors

              The script is nearly the same except that it takes the $ds variable as input for the Select

               

              $vmnames = Import-csv "C:\vcp-vm.csv" $vmnames | %{
                  $vm = Get-VM $_.Name
                  $ds = Get-Datastore -VM $vm    
                  $ds
              | Select @{N="Name";E={$vm.Name}},         @{N="Cluster";E={Get-Cluster -VM $vm.name}},         @{N="ESX Host";E={(Get-VMHost -VM $vm.name).Name}},         @{N="Datastore";E={$_.Name}},         @{N="DS Capacity";E={$_.CapacityMB}},         @{N="DS Free";E={$_.FreeSpaceMB}} } | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv
              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: powercli script to get cluster name, datastore name, total size & free space on DS for a VM
                aryan14in Enthusiast

                Superb! you are the best!

                 

                just one more thing, I tried modifying the same script to run on entire VC. I modified the script as below (even more), but having issues with it. could you please suggest on same as well.

                 

                    $vc = connect-viserver "VC name" 
                    $vm = Get-VM
                    $ds = Get-Datastore -VM $vm    
                    $ds | Select @{N="Name";E={$vm}},         @{N="Cluster";E={Get-Cluster -VM $vm}},         @{N="ESX Host";E={(Get-VMHost -VM $vm).Name}},         @{N="Datastore";E={$_.Name}},         @{N="DS Capacity";E={$_.CapacityMB}},         @{N="DS Free";E={$_.FreeSpaceMB}}| Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv

                 

                Thanks!

                • 5. Re: powercli script to get cluster name, datastore name, total size & free space on DS for a VM
                  LucD Guru
                  User ModeratorsCommunity WarriorsvExpert

                  Easiest is to use a loop over all objects returned by Get-VM.

                  The script stores the entries in an array, $report, and at the end exports the array to the CSV file.

                   

                  $vc = connect-viserver "VC name" 
                  $report = @()
                  foreach($vm in Get-VM){
                      $ds = Get-Datastore -VM $vm 
                      $report += $ds | Select @{N="Name";E={$vm}},
                          @{N="Cluster";E={Get-Cluster -VM $vm}},
                          @{N="ESX Host";E={(Get-VMHost -VM $vm).Name}},
                          @{N="Datastore";E={$_.Name}},
                          @{N="DS Capacity";E={$_.CapacityMB}},
                          @{N="DS Free";E={$_.FreeSpaceMB}}
                  }
                  $report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv 
                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 7. Re: powercli script to get cluster name, datastore name, total size & free space on DS for a VM
                    leaderone2 Novice

                    I found the script run very slow, I have 40 ESX host, and about 1000 vms. I run this script for about 40 minutes, Could it run more quickly? If I donot care the Performance consume on the vCenter or the client I run the scripts. Thanks!

                    • 8. Re: powercli script to get cluster name, datastore name, total size & free space on DS for a VM
                      LucD Guru
                      vExpertUser ModeratorsCommunity Warriors

                      You can try switching to the Get-View cmdlet.

                      That is normally faster than the regular cmdlets.

                       

                      $vmnames = Import-csv "C:\vcp-vm.csv" | %{"^$($_.Name)$"}

                      $vmnames = $vmnames -join '|'

                       

                      $report = Get-View -ViewType VirtualMachine -Property Name,Datastore,Runtime.Host,Parent -Filter @{'Name'=$vmnames} | %{

                          New-Object PSObject -Property @{

                              Name = $_.Name

                              Cluster = &{

                                  $obj = Get-View -Id $_.Parent -Property Name,Parent

                                  while ($obj -isnot [VMware.Vim.Computeresource] -and !$obj.Parent){

                                      $obj = Get-View -Id $obj.Parent -property Name,Parent

                                  }

                                  $obj.Name

                              }

                              'ESX Host' = Get-View -Id $_.Runtime.Host -Property Name | %{$_.Name}

                              Datastore = &{$script:ds = Get-View -Id $_.Datastore -property Name,Summary; $script:ds | %{$_.Name}} -join '|'

                              'DS Capacity MB' = $script:ds.Summary.Capacity/1MB

                              'DS Free MB' = $script:ds.Summary.FreeSpace/1MB

                          }

                      }

                      $report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv

                       

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 10. Re: powercli script to get cluster name, datastore name, total size & free space on DS for a VM
                        LucD Guru
                        vExpertUser ModeratorsCommunity Warriors

                        You're welcome.

                        Out of curiosity, how long did this version run ?

                         

                        Update: I changed the $ds variable to $script:ds

                        Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                        • 11. Re: powercli script to get cluster name, datastore name, total size & free space on DS for a VM
                          leaderone2 Novice

                          Actually I need the script to get vmname,vmid,ESXHost,ESXIP,Cluster,Datastore in one line.

                          I re-test  the script today (last time I did not wait until it run to end, I just estimate the time). The scripts taks about 94minutes with the old script below:( our environment has about 40 ESX host, and about 1000 vms)

                          Get-VM | Select Name,id,@{N="ESXHost";E={(Get-VMHost -VM $_).name}}, `
                          @{N="ESXIp";E={(Get-VMHost | Get-VMHostNetwork | Select Hostname, ConsoleGateway, DNSAddress -ExpandProperty ConsoleNic | Select  IP).ip}}, `
                          @{N="Cluster";E={Get-Cluster -VM $_}}, `
                          @{N="Datastore";E={Get-Datastore -VM $_}},`
                          Time | Export-Csv -NoTypeInformation c:\VMInfo\"Export67VMinfo$(Get-Date -Format 'yyyyMMdd').csv"
                          
                          


                          "

                          Your  new script diid not the same column with above, and the cluster sames get the folder info, so I modify like below (I am a younger with PowerCLI),but it still cannot get the same column to my need, I will trying to modify it tomorrow  . No matter how, the below script is quick than above :

                          $vmnames = get-vm | %{"^$($_.Name)$"}
                          $vmnames = $vmnames -join '|'
                          
                          $report = Get-View -ViewType VirtualMachine -Property Name,Datastore,Runtime.Host,Parent -Filter @{'Name'=$vmnames} | %{
                              New-Object PSObject -Property @{
                                  Name = $_.Name
                                  Cluster = get-cluster -vm $_.name
                                  'ESX Host' = Get-View -Id $_.Runtime.Host -Property Name | %{$_.Name}
                                  Datastore = &{$script:ds = Get-View -Id $_.Datastore -property Name,Summary; $script:ds | %{$_.Name}} -join '|'
                          
                          
                              }
                          }
                          $rert | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv
                          
                          

                           

                           

                          I will have a test to retrieve  vmname,vmid,ESXHost,ESXIP,Cluster,Datastore ,with your method, if I have result about this ,I will test how long it take , and put the result here!

                          Thanks!

                          • 12. Re: powercli script to get cluster name, datastore name, total size & free space on DS for a VM
                            LucD Guru
                            Community WarriorsvExpertUser Moderators

                            Except for the IP address of the ESXi node, everything is there.

                            Or what exactly do you mean with "did not same column" ?

                            Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                            • 13. Re: powercli script to get cluster name, datastore name, total size & free space on DS for a VM
                              leaderone2 Novice

                              I mean the script you provide donot have "vmid","esx IP" column, anyhow ,your script's run time is 45minutes, save half the time than before!

                              I have another question. Below is my script

                              Get-VM | Select Name,id, `
                              @{Name=’ESXIP’;Expression={%{$_.VMHost} | Select ($_.ExtensionData.Config.Network.Vnic | ? {$_.Device -eq "vmk0"}).Spec.Ip.IpAddress}},`
                              vmhost, `
                              @{Name=’Cluster’;Expression={$_.VMHost.Parent}}, `
                              @{N="Storage";E={Get-HardDisk -VM $_ |Select @{N='HD';E={$_.Name}},@{N='Datastore';E={($_.Filename.Split(']')[0]).TrimStart('[')}}}},`
                              Time | Export-Csv -NoTypeInformation -encoding utf8 c:\VMInfo\"Export67VMinfo$(Get-Date -Format 'yyyyMMdd').csv"
                              
                              

                              I want to get the vm's HardDisk and Datastore information. In our Environment, some vms  have more than one HardDisks    and the vm's HardDisks maybe not have the same Datastore.

                              When I use the script to Export to csv file, Some vm's Storage data are missing ,These vms all have more than one HardDisk. I think maybe "storage" column is too long or maybe this column have comma symbol? But it is normal in powercli as show below!

                              小Q截图-20160106211427.png

                              I need to export this to csv file, because I want to use diff command to check line matches ,could you  help me on this .Thanks!

                              • 14. Re: powercli script to get cluster name, datastore name, total size & free space on DS for a VM
                                LucD Guru
                                Community WarriorsUser ModeratorsvExpert

                                Try this extended version.

                                I added the missing columns, and all Datastores where there is vDisk, will be in one string, separated by the '|' character

                                 

                                $vmnames = Import-csv "C:\vcp-vm.csv" | %{"^$($_.Name)$"}

                                $vmnames = $vmnames -join '|'

                                 

                                $report = Get-View -ViewType VirtualMachine -Property Name,Datastore,Runtime.Host,Parent -Filter @{'Name'=$vmnames} | %{

                                    New-Object PSObject -Property @{

                                        Name = $_.Name

                                        VMId = $_.MoRef

                                        Cluster = &{

                                            $obj = Get-View -Id $_.Parent -Property Name,Parent

                                            while ($obj -isnot [VMware.Vim.Computeresource] -and !$obj.Parent){

                                                $obj = Get-View -Id $obj.Parent -property Name,Parent

                                            }

                                            $obj.Name

                                        }

                                        'ESX Host' = $script:esx = Get-View -Id $_.Runtime.Host -Property Name,Config; $esx.Name

                                        'ESX IP' = ($script.esx.Config.Network.Vnic | where{$_.Device -eq 'vmk0'}).Spec.Ip.IpAddress

                                        Datastore = &{$script:ds = Get-View -Id $_.Datastore -property Name,Summary; $script:ds | %{$_.Name}} -join '|'

                                        'DS Capacity MB' = $script:ds.Summary.Capacity/1MB

                                        'DS Free MB' = $script:ds.Summary.FreeSpace/1MB

                                        HD = &{

                                            $ds = $_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualDisk} | %{

                                                $_.Backing.Datastore

                                            }

                                            [string]::Join((Get-View -Id $ds -Property Name | select -ExpandProperty Name),'\')

                                                 }

                                    }

                                }

                                 

                                $report | Export-Csv -NoTypeInformation C:\VM_CLuster_Host_Datastore.csv

                                 

                                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                1 2 Previous Next