3 Replies Latest reply on Aug 9, 2017 6:41 PM by tdubb123

    get vmhosts connected to datastores

    tdubb123 Master

      I need to get a list of all vmhost connected to specific datastores

       

       

      is there a quick way to do this?

       

      get-datastore | ?{$_.name -match "EMC"} | get-vmhost ??

        • 1. Re: get vmhosts connected to datastores
          LucD Guru

          Try like this

           

          Get-Datastore |

          Select Name,@{N='VMHost';E={(Get-View -Id $_.ExtensionData.Host.Key -Property Name).Name -join ','}}

           

          • 2. Re: get vmhosts connected to datastores
            tdubb123 Master

            thanks lucd

             

            can you tell me why I am getting this under some colums here

             

            System.Object[]

             

             

            with this script

             

            $results = @()

             

             

            $vms = get-vm *

             

             

            foreach ($vm in $vms){

            $vmharddisk = $vm | Get-harddisk

            $vmview = $vm | Get-View

            $datastore = $vm | get-datastore

             

             

            $result = ""| select vmNAme, harddiskname, harddiskcapacitygb, totalharddiskgb, datastore, datastoreFreespaceGB, datastoreCapacitygb, Storageformat, esxhost

            $result.vmName = $vm.Name

            $result.harddiskname = $vmharddisk.name

            $result.harddiskcapacitygb = $vmharddisk.capacitygb

            $result.totalharddiskgb = Get-HardDisk -vm $vm | Measure-Object -Property Capacitygb -Sum | select -ExpandProperty Sum

            $result.datastore = $vmview.config.DatastoreUrl.name

            $result.datastoreFreespaceGB = $datastore.FreeSpaceGB

            $result.datastoreCapacityGB = $datastore.CapacityGB

            $result.storageformat = $vmharddisk.StorageFormat

            $result.esxhost = $vm.vmhost.name

             

             

            $results += $result

             

             

            }

             

             

            $results | export-csv "C:\scripts\vms.csv"  -NoTypeInformation -UseCulture

             

            -----------------

             

            its only happening outputting to csv

             

            when I do outgrid-view it works just fine

            • 3. Re: get vmhosts connected to datastores
              tdubb123 Master

              ok i added -join "," to get it to work since i have multiple entries

               

               

              $results = @()

               

               

              $vms = get-vm *

               

               

              foreach ($vm in $vms){

              $vmharddisk = $vm | Get-harddisk

              $vmview = $vm | Get-View

              $datastore = $vm | get-datastore

               

               

              $result = ""| select vmNAme, harddiskname, harddiskcapacitygb, totalharddiskgb, datastore, datastoreFreespaceGB, datastoreCapacitygb, Storageformat, esxhost

              $result.vmName = $vm.Name

              $result.harddiskname = $vmharddisk.name -join ","

              $result.harddiskcapacitygb = $vmharddisk.capacitygb

              $result.totalharddiskgb = Get-HardDisk -vm $vm | Measure-Object -Property Capacitygb -Sum | select -ExpandProperty Sum

              $result.datastore = $vmview.config.DatastoreUrl.name

              $result.datastoreFreespaceGB = $datastore.FreeSpaceGB

              $result.datastoreCapacityGB = $datastore.CapacityGB

              $result.storageformat = $vmharddisk.StorageFormat

              $result.esxhost = $vm.vmhost.name

               

               

              $results += $result

               

               

              }

               

               

              $results | export-csv "C:\scripts\vms.csv"  -NoTypeInformation -UseCulture

              • 4. Re: get vmhosts connected to datastores
                LucD Guru

                When one of your properties is an array, the Export-Csv cmdlet doens't know how to handle that.

                So it just displays the type of the property.

                 

                One way of avoiding this and seeing multiple values in one cell, is by doing a -join (see my code above).

                That way the multiple values in the array are converted to one string.

                • 5. Re: get vmhosts connected to datastores
                  tdubb123 Master

                  hi Luc

                   

                  Trying to round off my numbers but getting an error

                   

                  Screen Shot 2017-08-10 at 6.49.35 AM.png

                  • 6. Re: get vmhosts connected to datastores
                    LucD Guru

                    The [math]::Round function takes two parameters, the 1st is the actual number that you want rounded, and the 2nd is the number of decimal places you want,

                    You can put 0 for the 2nd parameter if you don't want decimals in the rounded number.

                    • 7. Re: get vmhosts connected to datastores
                      tdubb123 Master

                      do you mean like this?

                       

                      $result.harddiskcapacitygb = [math]::round (($vmharddisk.capacitygb),0)

                       

                      still getting the error

                      • 8. Re: get vmhosts connected to datastores
                        LucD Guru

                        Try like this.
                        Does this still produce the same error?

                         

                         

                         

                        $result.harddiskcapacitygb = [math]::round($vmharddisk.capacitygb,0)

                         

                        • 9. Re: get vmhosts connected to datastores
                          tdubb123 Master

                          yes it does

                          • 10. Re: get vmhosts connected to datastores
                            LucD Guru

                            Ok, think I see the issue.

                             

                             

                            You have more than one value in the variable, it's an array.

                            Probably from a VM with more than 1 harddisk