1 2 3 Previous Next 79 Replies Latest reply on Sep 12, 2019 12:44 AM by phatq

    Get List of VMs, Datastores and Folder name per Cluster

    2448Charles Novice


      Hello!!  I'm hoping someone can assist me with a PowerCLI script that can pull the subject info into a txt or csv file.  In an effort to get our environment cleaned up before our next DR exercise with SRM, I want to get a list that shows each VM, the datastore(s) it resides on, and the Folder name within VMs and Templates view.  We keep our VMs organized by creating folder names that correspond to the app running on the VM.  I mention SRM because last year our Protected Groups matched the Rcopy groups from our 3PAR array.  However, we've added over 200 new VMs since that time and my team members basically placed these new VMs on the first available datastore that had enough free space.  Unfortunately, we're not utilizing Storage Profiles...it's scheduled for next year.

       

      The extracted info would look similiar to the following:

       

      Servername    Datastore   Folder Name

      Server1            LUN0           ActiveDirectory

      Server2            LUN0           ActiveDirectory

      Server3            LUN1           ActiveDirectory

      Server4            LUN3           Argo

      Server5            LUN7           Argo

      Server6            LUN6           Lockbox

      Server7            LUN5           Lockbox

      Server8            LUN9           Citrix

      etc...

       

      Any help with extracting this info from vCenter would be greatly appreciated.

       

      Thanks,

      Charles

        • 1. Re: Get List of VMs, Datastores and Folder name per Cluster
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          Try something like this

           

          Get-VM |
          Select Name,
          @{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
          @{N="Folder";E={$_.Folder.Name}}
          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          1 person found this helpful
          • 2. Re: Get List of VMs, Datastores and Folder name per Cluster
            2448Charles Novice

            Thanks LucD!!!  That appears to provide me with what I'm looking for.  However, the above command is outputting the info screen.....what can be added to pipe it to a TXT file?

             

            Thanks,

            Charles

            • 3. Re: Get List of VMs, Datastores and Folder name per Cluster
              LucD Guru
              User ModeratorsCommunity WarriorsvExpert

              You can pipe the result to a CSV file for example

               

              Get-VM |
              Select Name,
              @{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
              @{N="Folder";E={$_.Folder.Name}} |
              Export-Csv C:\report.csv -NoTypeInformation -UseCulture
              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              2 people found this helpful
              • 4. Re: Get List of VMs, Datastores and Folder name per Cluster
                2448Charles Novice

                LucD, I've marked this as "correct answer".  Is there anything else I need to do to get it closed?

                 

                Thanks,

                Charles

                • 5. Re: Get List of VMs, Datastores and Folder name per Cluster
                  LucD Guru
                  vExpertUser ModeratorsCommunity Warriors

                  No, when you mark a reply as Correct, the thread is marked Answered automatically.

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: Get List of VMs, Datastores and Folder name per Cluster
                    2448Charles Novice

                    LucD, I have 2 additonal columns of info I forgot to mention.  I need to know how much stoarge has been provisioned and how much storage is being used.

                     

                    Example:

                    Servername    Datastore     Provisioned     Used        Folder Name

                    Server1            LUN0            80GB                 45GB       ActiveDirectory

                    Server2            LUN0            1.2TB                730GB     ActiveDirectory

                     

                    Thanks,

                    Charles

                    • 7. Re: Get List of VMs, Datastores and Folder name per Cluster
                      LucD Guru
                      Community WarriorsvExpertUser Moderators

                      No problem, try this

                       

                      Get-VM |
                      Select Name,
                      @{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
                      @{N="UsedSpaceGB";E={[math]::Round($_.UsedSpaceGB,1)}},
                      @{N="ProvisionedSpaceGB";E={[math]::Round($_.ProvisionedSpaceGB,1)}},
                      @{N="Folder";E={$_.Folder.Name}} |
                      Export-Csv C:\report.csv -NoTypeInformation -UseCulture
                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: Get List of VMs, Datastores and Folder name per Cluster
                        russjar Enthusiast

                        Hi how's it going?

                         

                        I haven't done any powershell for years and I'm trying to remember how to do this stuff...

                         

                        The example script is pretty good bit it's missing something for me.

                         

                        I'm looking for the following in a list form if possible;

                         

                        HOST     VM     DATASTORE NAME     DATASTORE CAPACITY      DATASTORE FREE SPACE

                         

                        I'd really appreciate some help

                         

                        Thanks in advance.

                        • 9. Re: Get List of VMs, Datastores and Folder name per Cluster
                          LucD Guru
                          vExpertUser ModeratorsCommunity Warriors

                          You mean like this ?

                           

                          foreach($vm in Get-VM){

                              Get-Datastore -RelatedObject $vm |

                              Select @{N='Host';E={$vm.VMhost.Name}},

                              @{N='VMName';E={$vm.Name}},

                              Name,

                              @{N='Capacity';E={[math]::Round($_.CapacityGB,1)}},

                              @{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}}

                          }

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 11. Re: Get List of VMs, Datastores and Folder name per Cluster
                            matt_jo Lurker

                            How do you pipe that to a .csv like your earlier commands?  I keep getting this error:

                             

                            PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> foreach($vm in Get-VM){ Get-Datastore -RelatedObject $vm | Select @{N='Host';E={$vm.VMhost.Name}}, @{N='VMName';E={$vm.Name}}, Name,@{N='Capacity';E={[math]::Round($_.CapacityGB,1)}}, @{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}}} | Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCulture

                            At line:1 char:235

                            + ... eSpaceGB,1)}}} | Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCultu ...

                            +                    ~

                            An empty pipe element is not allowed.

                                + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

                                + FullyQualifiedErrorId : EmptyPipeElement

                             

                             

                            PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

                             

                            And is there a way to also list the RDMs for each VM if any?

                            • 12. Re: Get List of VMs, Datastores and Folder name per Cluster
                              LucD Guru
                              Community WarriorsvExpertUser Moderators

                              Try like this, a ForEach doesn't place anything in the pipeline.

                              Use the call operator (&) to force that.

                               

                              &{foreach($vm in Get-VM){

                              Get-Datastore -RelatedObject $vm |

                              Select @{N='Host';E={$vm.VMhost.Name}},

                                @{N='VMName';E={$vm.Name}},

                                Name,

                                @{N='Capacity';E={[math]::Round($_.CapacityGB,1)}},

                                @{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}}}} |

                              Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCulture

                               

                              You can get the RDM info through the Get-Harddisk cmdlet, but that would require a somewhat different organisation of the script.

                              Perhaps you better make a new thread for that.

                              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                              1 person found this helpful
                              • 13. Re: Get List of VMs, Datastores and Folder name per Cluster
                                nmedard Enthusiast

                                Hello,

                                 

                                I am very new to powercli and I think that it is a great tool to manage vmware infrastructure.

                                 

                                however, I do not know where to start.

                                 

                                Could someone tell me what the following code does

                                Get-ResourcePool | Where {$_.Name -ne Resources }| Select Name, @{N=NumVM;E={($_ | Get-VM).Count}} | Sort Name|Export-Csv -NoTypeInformation c:\res-numvm.csv

                                 

                                 

                                Globally I know that it list ressource pools etc, but what I want to know is

                                1. Why do we use "$_.xxx"

                                2. Why and where do we use the "@"

                                3. What the "N" means

                                4. What the "E" means

                                5. Why the codes "$_ | Get-VM" is like it is and not like "$_Get-VM"

                                6. What the use of "|"

                                 

                                Nicolas

                                • 14. Re: Get List of VMs, Datastores and Folder name per Cluster
                                  RaviMV Enthusiast

                                  Hi Luc,

                                   

                                  Good to see that script you have provided. its worked for entire VM in a cluster/ VCenter. 

                                   

                                  I'm looking for the script that can provide the report, only list of the VMs.

                                   

                                  Eg :

                                   

                                  VM1

                                  VM3

                                  VM10

                                  VM14 ....

                                   

                                  Could you please help me out on this ?

                                  1 2 3 Previous Next