1 2 3 4 Previous Next 53 Replies Latest reply on Sep 28, 2015 11:58 AM by mypcgeek

    Get List of VMs, Datastores and VMDK / path per Cluster

    markus81 Lurker

      Hi Folks,

       

      as I´m new to this stuff, I´m wondering if anybody has already done some scripting work to gather a list of all virtual machines, its corresponding datastore(s) and the path to the VMDK-Files (or at least the name of it) - I know this part is already solved - found a couple of them ;-)

      But my problem is, I need this script drilled down per Cluster or Datacenter. As I said I´m completly new to this CLI-Stuff and don´t have a clue about how to solve this..

       

      Cheers

      Markus

        • 1. Re: Get List of VMs, Datastores and VMDK / path per Cluster
          RvdNieuwendijk Virtuoso
          User ModeratorsvExpert

          Hi Markus,

           

          Welcome to the VMware Communities!

           

          The next PowerCLI script will give you a list of all your VM's and their Datacenter, Cluster, HardDisks, Datastores and VMDK file paths. If you want to have the VM's only for a certain datacenter or cluster, you can filter the output by piping it to the Where-Object cmdlet.

           

          ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
            ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) { 
              ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
                ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
                  "" | Select-Object -Property @{N="VM";E={$VM.Name}},
                    @{N="Datacenter";E={$Datacenter.name}},
                    @{N="Cluster";E={$Cluster.Name}},
                    @{N="Hard Disk";E={$HardDisk.Name}},
                    @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
                    @{N="VMDKpath";E={$HardDisk.FileName}}
                }
              }
            }
          }    
          
          

           

          Don't hesitate to ask in this forum if you need more help.

           

          Regards, Robert

          • 2. Re: Get List of VMs, Datastores and VMDK / path per Cluster
            markus81 Lurker

            Hi Robert,

             

            many thanks! That´s exactly what I´m looking for I think. I´m only struggling with one last thing... the Export-CSV-Thing... My  understanding would be someting like

             

            $myVariable | Export-CSV blablabla..

             

            But how would i define the $myVariable in your script?

            I´m sorry for those really stupid rookie-questions ... have to talk to my boss that I need time and lessons to learn this stuff ;-)

            Thanks,

            Markus

            • 3. Re: Get List of VMs, Datastores and VMDK / path per Cluster
              RvdNieuwendijk Virtuoso
              vExpertUser Moderators

              There are two options if you want to write the output of the script to a .csv file. You can change the last line of the script into:

               

              } | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"
              
              

               

              This will pipe the output of the script to the Export-CSV cmdlet. And it will create a .csv file called VmInfo.csv.

               

              The second option is to change the first line of the script into:

               

              $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
              
              

               

              And append the following line at the end of the script:

               

              $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"
              
              

               

              This will put the output of the script from my previous post into a variable $VmInfo. In the last line the variable is used to output it to a .csv file.

              • 4. Re: Get List of VMs, Datastores and VMDK / path per Cluster
                LucD Guru
                Community WarriorsUser ModeratorsvExpert

                Isn't option 1 giving you the infamous "An empty pipe element is not allowed." error ?

                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                • 5. Re: Get List of VMs, Datastores and VMDK / path per Cluster
                  RvdNieuwendijk Virtuoso
                  User ModeratorsvExpert

                  Luc, you are right. I should have tested this. 

                   

                  This is a disadvantage of the ForEach command that I almost never use. Normally I use the Foreach-Object cmdlet and that works fine with pipeing the output to Export-CSV.

                   

                  Markus you have to use option 2 in this case.

                  • 6. Re: Get List of VMs, Datastores and VMDK / path per Cluster
                    markus81 Lurker

                    You guys are crazy ;-)

                    Exactly what I  need :-) Many thanks to you ! Have just tested and works fine! If I find some spare time I´m trying to modify it a little bit more for more granularity in regards of choosing the cluster manually but for now that works perfect :-)

                     

                    Cheers
                    Markus

                    • 7. Re: Get List of VMs, Datastores and VMDK / path per Cluster
                      markus81 Lurker

                      Shame on me... Forgot one more thing .... Configuratio File of the VM...

                      Can anyone give a hint how to include this also?

                      • 8. Re: Get List of VMs, Datastores and VMDK / path per Cluster
                        RvdNieuwendijk Virtuoso
                        User ModeratorsvExpert

                        You can add the VM's configuration file to the script by changing the script into:

                         

                        $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
                          ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) { 
                            ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
                              ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
                                "" | Select-Object -Property @{N="VM";E={$VM.Name}},
                                  @{N="Datacenter";E={$Datacenter.name}},
                                  @{N="Cluster";E={$Cluster.Name}},
                                  @{N="Hard Disk";E={$HardDisk.Name}},
                                  @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
                                  @{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
                                  @{N="VMDKpath";E={$HardDisk.FileName}}
                              }
                            }
                          }
                        }
                        $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"
                        
                        

                        • 9. Re: Get List of VMs, Datastores and VMDK / path per Cluster
                          GlennL10 Enthusiast

                          Hi guys,


                          Great script, works well, but I was wondering if it's possible to get the size of the VMDK file added to the output as well? How would I do this?

                          • 10. Re: Get List of VMs, Datastores and VMDK / path per Cluster
                            CRad14 Hot Shot
                            I will just add one line to this
                            
                            $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
                              ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) { 
                                ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
                                  ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
                                    "" | Select-Object -Property @{N="VM";E={$VM.Name}},
                                      @{N="Datacenter";E={$Datacenter.name}},
                                      @{N="Cluster";E={$Cluster.Name}},
                                      @{N="Hard Disk";E={$HardDisk.Name}},
                                      @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
                                      @{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
                                      @{N="VMDKpath";E={$HardDisk.FileName}},
                                      @{N="VMDK Size";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB}}
                                  }
                                }
                              }
                            }
                            $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"

                             

                            Message was edited by: CRad14

                            • 11. Re: Get List of VMs, Datastores and VMDK / path per Cluster
                              GlennL10 Enthusiast

                              Thanks for the reply, but I don't get the sizes in the final report, in fact it's still the same?

                              • 12. Re: Get List of VMs, Datastores and VMDK / path per Cluster
                                CRad14 Hot Shot

                                Hah!!! I missed a comma, I edited that post, it should be correct now.

                                • 13. Re: Get List of VMs, Datastores and VMDK / path per Cluster
                                  GlennL10 Enthusiast

                                  Thanks for your help, it works!

                                  • 14. Re: Get List of VMs, Datastores and VMDK / path per Cluster
                                    alize77 Enthusiast

                                    Is there a way to capture multiple IP address of those VMs as well? Tried to include some of the scripts I got online to merge with this one but just couldn't get it to work correctly. Currently, I only manage to capture 1 IP address.

                                     

                                     

                                    $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
                                      ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
                                        ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
                                          ForEach ($IP in $VM.Guest.IPAddress) {
                                            ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
                                            "" | Select-Object -Property @{N="VM";E={$VM.Name}},
                                              @{N="Datacenter";E={$Datacenter.name}},
                                              @{N="Cluster";E={$Cluster.Name}},
                                              @{N="IP";E={$IP}},
                                              @{N="Hard Disk";E={$HardDisk.Name}},
                                              @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
                                              @{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
                                              @{N="VMDKpath";E={$HardDisk.FileName}},
                                              @{N="VMDK Size";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB}}
                                    }
                                          }
                                        }
                                      }
                                    }
                                    $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"
                                    1 2 3 4 Previous Next