10 Replies Latest reply on May 7, 2014 9:06 AM by vin01

    To list VM, cluster, hosts, folders, vmdk file path

    fajarpri Hot Shot

      Hi all,

      Does anyone have a working script for the above?

      I'm on 4.1

      Thanks in advance

        • 1. Re: To list VM, cluster, hosts, folders, vmdk file path
          julienvarela Expert

          Hi,

           

          You can try this :

           

          $AuditVM = @()

          ForEach ($vm in (Get-Cluster | Get-VM )) {

              foreach ($vmguest in @($vm | Get-VMguest)) {

                      foreach ($cluster in @($vm | Get-Cluster )) {

                          foreach ($datastore in @(Get-VM $vm | Get-Datastore)) {

                              foreach ($Network in @(Get-VM $vm | Get-NetworkAdapter)) {

              $objGuest= "" | Select State, Name,CPU, Memory, IPAddress, OSFullName, Cluster, Folder, UsedspaceGB, Datastore, Description, MacAddress, Path, NetworkName, NetworkAdapter

              $objGuest.State= $vmguest.state

              $objGuest.CPU = $vm.NumCPU

              $objGuest.Memory = $vm.MemoryMB

              $objGuest.Description= $vm.Notes

              $objGuest.Folder=$vm.Folder

              $objGuest.Name = $vm.Name

              $objGuest.IPAddress = [string]::Join(',',$vmguest.IPAddress)

              $objGuest.OSFullName = $vmguest.OSFullName

              $objGuest.UsedSpaceGb = $vm.UsedSpaceGB

              $objGuest.Cluster = $cluster.Name

              $objGuest.Datastore = $datastore.Name

              $objGuest.MacAddress = $Network.MacAddress

              $objGuest.Path = $vm.Extensiondata.Config.Files.VmPathName

              $objGuest.NetworkName = $Network.NetworkName

              $objGuest.NetworkAdapter = $Network.Name

              $AuditVM += $objGuest

              }

              }

              }

              }

          }

           

          $AuditVM

           

          Regards,

          Julien

          • 2. Re: To list VM, cluster, hosts, folders, vmdk file path
            fajarpri Hot Shot

            Thank you Julien it works

             

            However, for the Folder, it doesn't show the folder structure, only the Folder the VM located.

            Is it possible to show the Folder structure too, e.g. IT Infra > Fileserver > Accounting

            • 3. Re: To list VM, cluster, hosts, folders, vmdk file path
              julienvarela Expert

              Hi,

               

              I just integrated the folderpath function that LucD has created by the past and it should work.

               

              function Get-FolderPath{

              <#

              .SYNOPSIS

                  Returns the folderpath for a folder

              .DESCRIPTION

                  The function will return the complete folderpath for

                  a given folder, optionally with the "hidden" folders

                  included. The function also indicats if it is a "blue"

                  or "yellow" folder.

              .NOTES

                  Authors:    Luc Dekens

              .PARAMETER Folder

                  On or more folders

              .PARAMETER ShowHidden

                  Switch to specify if "hidden" folders should be included

                  in the returned path. The default is $false.

              .EXAMPLE

                  PS> Get-FolderPath -Folder (Get-Folder -Name "MyFolder")

              .EXAMPLE

                  PS> Get-Folder | Get-FolderPath -ShowHidden:$true

              #>

               

                  param(

                  [parameter(valuefrompipeline = $true,

                  position = 0,

                  HelpMessage = "Enter a folder")]

                  [VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl[]]$Folder,

                  [switch]$ShowHidden = $false

                  )

               

                  begin{

                      $excludedNames = "Datacenters","vm","host"

                  }

               

                  process{

                      $Folder | %{

                          $fld = $_.Extensiondata

                          $fldType = "yellow"

                          if($fld.ChildType -contains "VirtualMachine"){

                              $fldType = "blue"

                          }

                          $path = $fld.Name

                          while($fld.Parent){

                              $fld = Get-View $fld.Parent

                              if((!$ShowHidden -and $excludedNames -notcontains $fld.Name) -or $ShowHidden){

                                  $path = $fld.Name + "\" + $path

                              }

                          }

                          $row = "" | Select Path

                          $row.Path = $path

                          $row

                      }

                  }

              }

               

               

               

               

              $AuditVM = @()

              ForEach ($vm in (Get-Cluster Maquette_DEV | Get-VM )) {

                  foreach ($vmguest in @($vm | Get-VMguest)) {

                          foreach ($cluster in @($vm | Get-Cluster )) {

                              foreach ($datastore in @(Get-VM $vm | Get-Datastore)) {

                                  foreach ($Network in @(Get-VM $vm | Get-NetworkAdapter)) {

                  $objGuest= "" | Select State, Name,CPU, Memory, IPAddress, OSFullName, Cluster, Folder, UsedspaceGB, Datastore, Description, MacAddress, Path, NetworkName, NetworkAdapter

                  $objGuest.State= $vmguest.state

                  $objGuest.CPU = $vm.NumCPU

                  $objGuest.Memory = $vm.MemoryMB

                  $objGuest.Description= $vm.Notes

                  $objGuest.Folder= Get-folder $vm.Folder | Get-folderpath

                  $objGuest.Name = $vm.Name

                  $objGuest.IPAddress = [string]::Join(',',$vmguest.IPAddress)

                  $objGuest.OSFullName = $vmguest.OSFullName

                  $objGuest.UsedSpaceGb = $vm.UsedSpaceGB

                  $objGuest.Cluster = $cluster.Name

                  $objGuest.Datastore = $datastore.Name

                  $objGuest.MacAddress = $Network.MacAddress

                  $objGuest.Path = $vm.Extensiondata.Config.Files.VmPathName

                  $objGuest.NetworkName = $Network.NetworkName

                  $objGuest.NetworkAdapter = $Network.Name

                  $AuditVM += $objGuest

                  }

                  }

                  }

                  }

              }

               

              $AuditVM

               

              Regards,

              Julien

              1 person found this helpful
              • 4. Re: To list VM, cluster, hosts, folders, vmdk file path
                fajarpri Hot Shot

                Wow cool!
                Thank you very much Julien!

                • 5. Re: To list VM, cluster, hosts, folders, vmdk file path
                  CarlosDionizio Novice

                  Hi for all!

                   

                  This work with version 5.0 ... ?

                  Tks

                   

                  Hi,

                   

                  I just integrated the folderpath function that LucD has created by the past and it should work.

                   

                  function Get-FolderPath{

                  <#

                  .SYNOPSIS

                      Returns the folderpath for a folder

                  .DESCRIPTION

                      The function will return the complete folderpath for

                      a given folder, optionally with the "hidden" folders

                      included. The function also indicats if it is a "blue"

                      or "yellow" folder.

                  .NOTES

                      Authors:    Luc Dekens

                  .PARAMETER Folder

                      On or more folders

                  .PARAMETER ShowHidden

                      Switch to specify if "hidden" folders should be included

                      in the returned path. The default is $false.

                  .EXAMPLE

                      PS> Get-FolderPath -Folder (Get-Folder -Name "MyFolder")

                  .EXAMPLE

                      PS> Get-Folder | Get-FolderPath -ShowHidden:$true

                  #>

                   

                      param(

                      [parameter(valuefrompipeline = $true,

                      position = 0,

                      HelpMessage = "Enter a folder")]

                      [VMware.VimAutomation.ViCore.Impl.V1.Inventory.FolderImpl[]]$Folder,

                      [switch]$ShowHidden = $false

                      )

                   

                      begin{

                          $excludedNames = "Datacenters","vm","host"

                      }

                   

                      process{

                          $Folder | %{

                              $fld = $_.Extensiondata

                              $fldType = "yellow"

                              if($fld.ChildType -contains "VirtualMachine"){

                                  $fldType = "blue"

                              }

                              $path = $fld.Name

                              while($fld.Parent){

                                  $fld = Get-View $fld.Parent

                                  if((!$ShowHidden -and $excludedNames -notcontains $fld.Name) -or $ShowHidden){

                                      $path = $fld.Name + "\" + $path

                                  }

                              }

                              $row = "" | Select Path

                              $row.Path = $path

                              $row

                          }

                      }

                  }

                   

                   

                   

                   

                  $AuditVM = @()

                  ForEach ($vm in (Get-Cluster Maquette_DEV | Get-VM )) {

                      foreach ($vmguest in @($vm | Get-VMguest)) {

                              foreach ($cluster in @($vm | Get-Cluster )) {

                                  foreach ($datastore in @(Get-VM $vm | Get-Datastore)) {

                                      foreach ($Network in @(Get-VM $vm | Get-NetworkAdapter)) {

                      $objGuest= "" | Select State, Name,CPU, Memory, IPAddress, OSFullName, Cluster, Folder, UsedspaceGB, Datastore, Description, MacAddress, Path, NetworkName, NetworkAdapter

                      $objGuest.State= $vmguest.state

                      $objGuest.CPU = $vm.NumCPU

                      $objGuest.Memory = $vm.MemoryMB

                      $objGuest.Description= $vm.Notes

                      $objGuest.Folder= Get-folder $vm.Folder | Get-folderpath

                      $objGuest.Name = $vm.Name

                      $objGuest.IPAddress = [string]::Join(',',$vmguest.IPAddress)

                      $objGuest.OSFullName = $vmguest.OSFullName

                      $objGuest.UsedSpaceGb = $vm.UsedSpaceGB

                      $objGuest.Cluster = $cluster.Name

                      $objGuest.Datastore = $datastore.Name

                      $objGuest.MacAddress = $Network.MacAddress

                      $objGuest.Path = $vm.Extensiondata.Config.Files.VmPathName

                      $objGuest.NetworkName = $Network.NetworkName

                      $objGuest.NetworkAdapter = $Network.Name

                      $AuditVM += $objGuest

                      }

                      }

                      }

                      }

                  }

                   

                  $AuditVM

                  • 6. Re: To list VM, cluster, hosts, folders, vmdk file path
                    julienvarela Expert

                    Hi ,

                     

                    Yes its work with vsphere 5.

                     

                    regards,

                    Julien

                    • 7. Re: To list VM, cluster, hosts, folders, vmdk file path
                      CarlosDionizio Novice

                      Tks Julie!

                       

                      ITs only .\script.ps1 to execute?

                       

                      Sorry i m newbie in PowerCLI and powershell!

                      • 8. Re: To list VM, cluster, hosts, folders, vmdk file path
                        julienvarela Expert

                        Yes , but first you need to start powercli and start a connection to your VC.

                         

                        Regards,

                        julien

                        • 9. Re: To list VM, cluster, hosts, folders, vmdk file path
                          cmatheson Lurker

                          Sorry to bump an old thread but would it be possible to sort the results of this by the Folder?

                          We have different clients organized into folders I'd like to sort the result by the folder name if possible

                          I've tried a few different ways but I am still fairly new to power-cli and powershell.

                          • 10. Re: To list VM, cluster, hosts, folders, vmdk file path
                            vin01 Hot Shot

                            Hi Carlos

                             

                            Many Thanks for the script..Along with this output i need few more info .. like "Datacenter Name",Harddisk wise data with nna or along datastore with nna. and Lun ID.

                             

                            I'm attaching a script which i found in communities It is given by LucD.It may be help full with the current script.

                            Regards
                            Vineeth.K