PowerCLI

 View Only
Expand all | Collapse all

PowerCLI Report :Getting inventory VMs from ESX cluster hosts

Vineeth_K

Vineeth_KOct 21, 2016 05:38 PM

LucD

LucDOct 24, 2016 10:31 AM

  • 1.  PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 21, 2016 07:28 AM

    I have been using powercli script like below. I am able to fetch VMs informations. But some VMs can have more than one IP such as backup interface , internal interface. I want to display its within VM IP Addresses column. How can I reconstruct my code?

    I'm looking to run a PowerCLI script to return a table with the headings as follows:

    vCenter | Cluster Name | VMHost | VM | VM IP Addresses | Guest OS | Notes | State | CPU | RAM | Datastores | Disk Space | VMware Tools version

    & { foreach ($vCenterServer in $DefaultVIServers)
        {
         
    $VMHostTable= @{}
         
    foreach ($VMHostView in (Get-View -Server $vCenterServer -ViewType HostSystem -Property Name))
          {
           
    $VMHostTable["$($VMHostView.MoRef.Value)"] = $VMHostView.Name
          }
         
    $DatastoreTable = @{}
         
    foreach ($DatastoreView in (Get-View -Server $vCenterServer -ViewType Datastore -Property Name))
          {
           
    $DatastoreTable["$($DatastoreView.MoRef.Value)"] = $DatastoreView.Name
          }
         
    Get-View -Server $vCenterServer -ViewType VirtualMachine -Filter @{"Config.Template"="False"} -Property Name,
           
    Runtime.Host,
           
    Guest.GuestFullName,
           
    Config.Annotation,
           
    Datastore,
           
    Config.Tools.ToolsVersion |
         
    Select-Object -Property @{N="vCenter";E={$vCenterServer.Name}},
           
    @{N="VMHost";E={$VMHostTable["$($_.Runtime.Host.Value)"]}},
           
    @{N="VM";E={$_.Name}},
           
    @{N="Guest OS";E={$_.Guest.GuestFullName}},
           
    @{N="Notes";E={$_.Config.Annotation}},
           
    @{N="Datastores";E={
               
    $DatastoreNames = foreach ($Datastore in ($_.Datastore))
                  {
    $DatastoreTable["$($Datastore.Value)"]}         
                [
    string]::Join(',',($DatastoreNames))
              }
            }
    ,
           
    @{N="VMware Tools version";E={$_.Config.Tools.ToolsVersion}}
        }
      }
    |
    Export-Csv -Path "VMsInfo-$(Get-Date -UFormat '%Y%m%d-%H.%M.%S').csv" -NoTypeInformation -UseCulture -Encoding UTF8


  • 2.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 21, 2016 09:01 AM

    If you want to have one rowper IP address I guess you'll have to do a foreach on "guest.net.ipaddress" but that might look messy.

    Or you can just add a row with "guest.net.ipaddress" that should give you something like {10.20.0.1, 192.168.0.2, ...}.



  • 3.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 21, 2016 05:38 PM

    You can try this

    Complete VM information



  • 4.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 21, 2016 06:05 PM

    First of all many thanks for your help. How can I export output of this script to CSV format? Also I have been using DefaultVIServers variable.  I can not see it your powercli script. How about this?



  • 5.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 23, 2016 05:54 AM

    The $global:defaultviservers variable is empty as long as no successful Connect-VIServer is executed.

    When I have a property that can be an array (like IP addresses), I join the values together with a '|' separator.

    Something like this

          Select-Object -Property @{N="vCenter";E={$vCenterServer.Name}},

            @{N="VMHost";E={$VMHostTable["$($_.Runtime.Host.Value)"]}},

            @{N="VM";E={$_.Name}},

            @{N='VM IP Adrressess';E={$_.guest.IPAddress -join '|'}},

            @{N="Guest OS";E={$_.Guest.GuestFullName}},

            @{N="Notes";E={$_.Config.Annotation}},

            @{N="Datastores";E={

                $DatastoreNames = foreach ($Datastore in ($_.Datastore))

                  {$DatastoreTable["$($Datastore.Value)"]}         

                [string]::Join(',',($DatastoreNames))

              }

            },

            @{N="VMware Tools version";E={$_.Config.Tools.ToolsVersion}}



  • 6.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 23, 2016 07:52 AM

    many thanks for your answer. Using the following PowerCLI script I was able to see all VMs on the ESX Cluster hosts. Actually I was asking question for below script. So How can I export output of this script to CSV format?  Also how can I adapted DefaultVIServers variable this script ?

    Secondly  , I want to to find out if the Tools were updated correctly is to check the build number of the Tool. is there is another variable that would do the trick in powercli ? So  it would be something like  9.4.10.38250 (Build-2092844).



    foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,runtime.powerState,Guest.net,Config.Hardware.numCPU,Config.Hardware.MemoryMB,Runtime.Host,Guest.GuestFullName,
    Config.GuestFullName,Parent,ResourcePool,Config.Hardware.Device,Config.version,Config.Tools.ToolsVersion,guest.toolsversionstatus,
    Config.Files.VMPathName)){
       
    $t = Get-View $vm.ResourcePool -Property Name,Parent
       
    while($t.getType().Name -eq "ResourcePool"){
         
    $t = Get-View $t.Parent -Property Name,Parent
        }
           
    if($t.GetType().Name -eq "ClusterComputeResource"){
           
    $cluster = $t.Name
            }
           
    else{
               
    $cluster = "Stand Alone Host"
            }
       
    while($t.getType().Name -ne "Datacenter"){
           
    $t = Get-View $t.Parent -Property Name,Parent
        }
       
    $datacenter = $t.Name
     
       
    $vm.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} |
       
    Select @{N="VM";E={$vm.Name}},
       
    @{N='powerState';E={$vm.runtime.powerState}},
       
    @{N='IP';E={[string]::Join(',',($vm.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}},
       
    @{N='NumCPU';E={$vm.config.Hardware.NumCpu}},
       
    @{N='Memory GB';E={$vm.Config.Hardware.MemoryMB| %{[math]::Round($_/1kb,2)}}},
       
    @{N='VMHost';E={$script:esx = Get-View -Id $vm.Runtime.Host; $script:esx.name}},
       
    @{N='GuestOS';E={$vm.Guest.GuestFullName}},
       
    @{N='ConfiguredOS';E={$vm.Config.GuestFullName}},
       
    #@{N="Folder";E={$path}},
        @{N="Cluster";E={$cluster}},
       
    @{N="Datacenter";E={$datacenter}},
       
    @{N="Scsi";E={$_.UnitNumber}},
       
    @{N="Hard Disk";E={$_.DeviceInfo.Label}},
       
    @{N="Disk datastore";E={$_.Backing.Filename.Split(']')[0].TrimStart('[')}},
       
    @{N="Disk capacity GB";E={$_.CapacityInKB| %{[math]::Round($_/1MB,2)}}},
       
    @{N="Disk type";E={
               
    if($_.Backing.GetType().Name -match "flat"){
                   
    "Flat"
                }
               
    else{
                   
    $_.Backing.CompatibilityMode
                }}}
    ,
     
    @{N='DeviceName';E={
       
    if($_.Backing.GetType().Name -match 'raw'){
         
    $_.Backing.DeviceName
        }
       
    else{
         
    $script:lunnaa = (Get-View -Id $_.Backing.Datastore).Info.Vmfs.Extent[0].DiskName
         
    $script:lun = $script:esx.Config.StorageDevice.ScsiLun | where{$_.CanonicalName -eq $script:lunnaa}
         
    $script:lun.Descriptor | where{$_.Id -match 'vml.'} | Select -ExpandProperty Id
        }}}
    ,
     
    @{N='LUN NAA';E={
       
    if($_.Backing.GetType().Name -match 'raw'){
         
    $lunUuid = $_.Backing.LunUuid
         
    $script:lun = $script:esx.Config.StorageDevice.ScsiLun | where{$_.Uuid -eq $lunUuid}
         
    $script:lun.CanonicalName
        }
       
    else{
         
    $script:lunnaa
        }}}
    ,
     
    @{N='LUN ID';E={
         
    $dev = $script:esx.Config.StorageDevice.PlugStoreTopology.Device | where {$_.Lun -eq $script:lun.Key}
         
    $script:esx.Config.StorageDevice.PlugStoreTopology.Path | where {$_.Device -eq $dev.Key} |
         
    Select -First 1 -ExpandProperty LunNumber
        }}
    ,
     
    @{N='VMConfigFile';E={$VM.config.files.VMpathname}},
     
    @{N='VMDKPath';E={$_.Backing.FileName}},
     
    @{N="HW Version";E={$vm.Config.version}},
     
    @{N="VMware Tools version";E={$vm.Config.Tools.ToolsVersion}}   
     
    @{N="Tools Status";E={$vm.guest.toolsversionstatus}},
     
    @{N="NIC Name";E={($vm.config.hardware.device | where {($_.DeviceInfo.Label -like "Network*")}).DeviceInfo.Label}},
     
    @{N="Mac"; E={($vm.Config.Hardware.Device | where{$_.DeviceInfo.Label -like "Network*"}).MacAddress}},
     
    @{N="Portgroup"; E={
       
    $nic = $vm.Config.Hardware.Device | where{$_.DeviceInfo.Label -like "Network*"}
        [
    string]::Join(',',(
         
    $nic | %{
         
    if($_.DeviceInfo.Summary -notmatch 'DVSwitch'){
           
    $_.DeviceInfo.Summary
          }
         
    else{
           
    Get-View -ViewType DistributedVirtualPortgroup -Property Name -Filter @{'Key'=$_.Backing.Port.PortgroupKey} |
           
    Select -ExpandProperty Name
          }}))}}
    }


  • 7.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 23, 2016 08:31 AM

    You can use the 'VMware Tools Version' property, that is already in the report, to check if the latest VMware Tools are installed.

    Try the attached script, I added a loop through all the connected vCenters and I added the export to a CSV.



  • 8.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 23, 2016 12:23 PM

    when attempting to open ZIP file then I got the following the error like the archive is either in unknown format or damaged. Addionataly ,  I need to retrieve Virtual machine Consolidation Needed status from VMs how can we add into the powercli script? 

    Thanks in advance for your all help.



  • 9.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 23, 2016 01:33 PM

    That zip file was created by the forum SW, not sure what happened there.

    In any case I'll include the script inline.

    $report = foreach($vc in $global:defaultviservers){

        foreach($vm in (Get-View-ViewType VirtualMachine -Property Name,runtime.powerState,Guest.net,Config.Hardware.numCPU,Config.Hardware.MemoryMB,Runtime.Host,Guest.GuestFullName,

        Config.GuestFullName,Parent,ResourcePool,Config.Hardware.Device,Config.version,Config.Tools.ToolsVersion,guest.toolsversionstatus,

        Config.Files.VMPathName -Server $vc )){

            $t = Get-View $vm.ResourcePool -Property Name,Parent -Server $vc

            while($t.getType().Name -eq "ResourcePool"){

              $t = Get-View $t.Parent -Property Name,Parent -Server $vc

            }

                if($t.GetType().Name -eq "ClusterComputeResource"){

                $cluster = $t.Name

                }

                else{

                    $cluster = "Stand Alone Host"

                }

            while($t.getType().Name -ne "Datacenter"){

                $t = Get-View $t.Parent -Property Name,Parent -Server $vc

            }

            $datacenter = $t.Name

         

            $vm.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} |

            Select @{N="VM";E={$vm.Name}},

            @{N='powerState';E={$vm.runtime.powerState}},

            @{N='IP';E={[string]::Join(',',($vm.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}},

            @{N='NumCPU';E={$vm.config.Hardware.NumCpu}},

            @{N='Memory GB';E={$vm.Config.Hardware.MemoryMB| %{[math]::Round($_/1kb,2)}}},

            @{N='vCenter';E={$vc.Name}},

            @{N='VMHost';E={$script:esx = Get-View -Id $vm.Runtime.Host -Server $vc ; $script:esx.name}},

            @{N='GuestOS';E={$vm.Guest.GuestFullName}},

            @{N='ConfiguredOS';E={$vm.Config.GuestFullName}},

            #@{N="Folder";E={$path}},

            @{N="Cluster";E={$cluster}},

            @{N="Datacenter";E={$datacenter}},

            @{N="Scsi";E={$_.UnitNumber}},

            @{N="Hard Disk";E={$_.DeviceInfo.Label}},

            @{N="Disk datastore";E={$_.Backing.Filename.Split(']')[0].TrimStart('[')}},

            @{N="Disk capacity GB";E={$_.CapacityInKB| %{[math]::Round($_/1MB,2)}}},

            @{N="Disk type";E={

                    if($_.Backing.GetType().Name -match "flat"){

                        "Flat"

                    }

                    else{

                        $_.Backing.CompatibilityMode

                    }}},

          @{N='DeviceName';E={

            if($_.Backing.GetType().Name -match 'raw'){

              $_.Backing.DeviceName

            }

            else{

              $script:lunnaa = (Get-View -Id $_.Backing.Datastore -Server $vc ).Info.Vmfs.Extent[0].DiskName

              $script:lun = $script:esx.Config.StorageDevice.ScsiLun | where{$_.CanonicalName -eq $script:lunnaa}

              $script:lun.Descriptor | where{$_.Id -match 'vml.'} | Select -ExpandProperty Id

            }}},

          @{N='LUN NAA';E={

            if($_.Backing.GetType().Name -match 'raw'){

              $lunUuid = $_.Backing.LunUuid

              $script:lun = $script:esx.Config.StorageDevice.ScsiLun | where{$_.Uuid -eq $lunUuid}

              $script:lun.CanonicalName

            }

            else{

              $script:lunnaa

            }}},

          @{N='LUN ID';E={

              $dev = $script:esx.Config.StorageDevice.PlugStoreTopology.Device | where {$_.Lun -eq $script:lun.Key}

              $script:esx.Config.StorageDevice.PlugStoreTopology.Path | where {$_.Device -eq $dev.Key} |

              Select -First 1 -ExpandProperty LunNumber

            }},

          @{N='VMConfigFile';E={$VM.config.files.VMpathname}},

          @{N='VMDKPath';E={$_.Backing.FileName}},

          @{N="HW Version";E={$vm.Config.version}},

          @{N="VMware Tools version";E={$vm.Config.Tools.ToolsVersion}}   

          @{N="Tools Status";E={$vm.guest.toolsversionstatus}},

          @{N="NIC Name";E={($vm.config.hardware.device | where {($_.DeviceInfo.Label -like "Network*")}).DeviceInfo.Label}},

          @{N="Mac"; E={($vm.Config.Hardware.Device | where{$_.DeviceInfo.Label -like "Network*"}).MacAddress}},

          @{N="Portgroup"; E={

            $nic = $vm.Config.Hardware.Device | where{$_.DeviceInfo.Label -like "Network*"}

            [string]::Join(',',(

              $nic | %{

              if($_.DeviceInfo.Summary -notmatch 'DVSwitch'){

                $_.DeviceInfo.Summary

              }

              else{

                Get-View -ViewType DistributedVirtualPortgroup -Property Name -Filter @{'Key'=$_.Backing.Port.PortgroupKey} -Server $vc  |

                Select -ExpandProperty Name

              }}))}}

        }

    }

    $report | Export-Csv report.csv -NoTypeInformation -UseCulture



  • 10.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 23, 2016 02:02 PM

    Lastly I need to retrieve Virtual machine Consolidation Needed status from VMs how can we add into the powercli script?



  • 11.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 05:27 AM

    Try like this

    $report = foreach($vc in $global:defaultviservers){

        foreach($vm in (Get-View-ViewType VirtualMachine -Property Name,runtime.powerState,runtime.consolidationNeeded,Guest.net,Config.Hardware.numCPU,Config.Hardware.MemoryMB,

        Runtime.Host,Guest.GuestFullName,    Config.GuestFullName,Parent,ResourcePool,Config.Hardware.Device,Config.version,Config.Tools.ToolsVersion,guest.toolsversionstatus,

        Config.Files.VMPathName -Server $vc )){

            $t = Get-View $vm.ResourcePool -Property Name,Parent -Server $vc

            while($t.getType().Name -eq "ResourcePool"){

              $t = Get-View $t.Parent -Property Name,Parent -Server $vc

            }

                if($t.GetType().Name -eq "ClusterComputeResource"){

                $cluster = $t.Name

                }

                else{

                    $cluster = "Stand Alone Host"

                }

            while($t.getType().Name -ne "Datacenter"){

                $t = Get-View $t.Parent -Property Name,Parent -Server $vc

            }

            $datacenter = $t.Name

         

            $vm.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} |

            Select @{N="VM";E={$vm.Name}},

            @{N='powerState';E={$vm.runtime.powerState}},

            @{N='IP';E={[string]::Join(',',($vm.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}},

            @{N='NumCPU';E={$vm.config.Hardware.NumCpu}},

            @{N='Memory GB';E={$vm.Config.Hardware.MemoryMB| %{[math]::Round($_/1kb,2)}}},

            @{N='vCenter';E={$vc.Name}},

            @{N='VMHost';E={$script:esx = Get-View -Id $vm.Runtime.Host -Server $vc ; $script:esx.name}},

            @{N='GuestOS';E={$vm.Guest.GuestFullName}},

            @{N='ConfiguredOS';E={$vm.Config.GuestFullName}},

            #@{N="Folder";E={$path}},

            @{N="Cluster";E={$cluster}},

            @{N="Datacenter";E={$datacenter}},

            @{N="Scsi";E={$_.UnitNumber}},

            @{N="Hard Disk";E={$_.DeviceInfo.Label}},

            @{N="Disk datastore";E={$_.Backing.Filename.Split(']')[0].TrimStart('[')}},

            @{N='Needs Consolidation';E={$vm.Runtime.consolidationNeeded}},

            @{N="Disk capacity GB";E={$_.CapacityInKB| %{[math]::Round($_/1MB,2)}}},

            @{N="Disk type";E={

                    if($_.Backing.GetType().Name -match "flat"){

                        "Flat"

                    }

                    else{

                        $_.Backing.CompatibilityMode

                    }}},

          @{N='DeviceName';E={

            if($_.Backing.GetType().Name -match 'raw'){

              $_.Backing.DeviceName

            }

            else{

              $script:lunnaa = (Get-View -Id $_.Backing.Datastore -Server $vc ).Info.Vmfs.Extent[0].DiskName

              $script:lun = $script:esx.Config.StorageDevice.ScsiLun | where{$_.CanonicalName -eq $script:lunnaa}

              $script:lun.Descriptor | where{$_.Id -match 'vml.'} | Select -ExpandProperty Id

            }}},

          @{N='LUN NAA';E={

            if($_.Backing.GetType().Name -match 'raw'){

              $lunUuid = $_.Backing.LunUuid

              $script:lun = $script:esx.Config.StorageDevice.ScsiLun | where{$_.Uuid -eq $lunUuid}

              $script:lun.CanonicalName

            }

            else{

              $script:lunnaa

            }}},

          @{N='LUN ID';E={

              $dev = $script:esx.Config.StorageDevice.PlugStoreTopology.Device | where {$_.Lun -eq $script:lun.Key}

              $script:esx.Config.StorageDevice.PlugStoreTopology.Path | where {$_.Device -eq $dev.Key} |

              Select -First 1 -ExpandProperty LunNumber

            }},

          @{N='VMConfigFile';E={$VM.config.files.VMpathname}},

          @{N='VMDKPath';E={$_.Backing.FileName}},

          @{N="HW Version";E={$vm.Config.version}},

          @{N="VMware Tools version";E={$vm.Config.Tools.ToolsVersion}}   

          @{N="Tools Status";E={$vm.guest.toolsversionstatus}},

          @{N="NIC Name";E={($vm.config.hardware.device | where {($_.DeviceInfo.Label -like "Network*")}).DeviceInfo.Label}},

          @{N="Mac"; E={($vm.Config.Hardware.Device | where{$_.DeviceInfo.Label -like "Network*"}).MacAddress}},

          @{N="Portgroup"; E={

            $nic = $vm.Config.Hardware.Device | where{$_.DeviceInfo.Label -like "Network*"}

            [string]::Join(',',(

              $nic | %{

              if($_.DeviceInfo.Summary -notmatch 'DVSwitch'){

                $_.DeviceInfo.Summary

              }

              else{

                Get-View -ViewType DistributedVirtualPortgroup -Property Name -Filter @{'Key'=$_.Backing.Port.PortgroupKey} -Server $vc  |

                Select -ExpandProperty Name

              }}))}}

        }

    }

    $report | Export-Csv report.csv -NoTypeInformation -UseCulture



  • 12.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 10:21 AM

    I am getting an error  like below  :

    Get-View        Invalid property: runtimeGuest.net   

    At line:2 char:21

    + ... ach($vm in (Get-View -ViewType VirtualMachine -Property Name,runtime. ...

    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidArgument: (:) [Get-View], InvalidArgument

        + FullyQualifiedErrorId : Client20_MoServiceImpl_GetNetInteropView_InvalidProperty,VMware.VimAutomation.ViCore.Cmdlets.

       Commands.DotNetInterop.GetVIView



  • 13.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 10:31 AM

    Oops, typo.

    Corrected it.



  • 14.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 10:46 AM

    Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not null o

    r empty, and then try the command again.

    At line:5 char:23

    +         $t = Get-View $vm.ResourcePool -Property Name,Parent -Server  ...

    +                       ~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException

        + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.G

       etVIView



  • 15.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 01:18 PM

    Didn't you get that error when you ran the script originally?



  • 16.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 01:44 PM

    Normally I am using this powercli script the first time.



  • 17.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts
    Best Answer

    Posted Oct 24, 2016 03:54 PM

    I suspect that error was caused by templates in the environment.

    I added logic to distinguish between Templates and VMs.

    Try this one

    $report = foreach($vc in $global:defaultviservers){

        foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,runtime.powerState,runtime.consolidationNeeded,Guest.net,Config.Hardware.numCPU,Config.Hardware.MemoryMB,

        Runtime.Host,Guest.GuestFullName, Config.GuestFullName,Parent,ResourcePool,Config.Hardware.Device,Config.version,Config.Tools.ToolsVersion,guest.toolsversionstatus,

        Config.Files.VMPathName,Config.Template -Server $vc )){

            if($vm.Config.Template){

                $t = Get-View -Id (Get-View $vm.Runtime.Host).Parent

            }

            else{

                $t = Get-View $vm.ResourcePool -Property Name,Parent -Server $vc

                while($t.getType().Name -eq "ResourcePool"){

                  $t = Get-View $t.Parent -Property Name,Parent -Server $vc

                }

            }

            if($t.GetType().Name -eq "ClusterComputeResource"){

                $cluster = $t.Name

            }

            else{

                $cluster = "Stand Alone Host"

            }

            while($t.getType().Name -ne "Datacenter"){

                $t = Get-View $t.Parent -Property Name,Parent -Server $vc

            }

            $datacenter = $t.Name

        

            $vm.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualDisk]} |

            Select @{N="VM";E={$vm.Name}},

            @{N='powerState';E={$vm.runtime.powerState}},

            @{N='IP';E={[string]::Join(',',($vm.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}},

            @{N='NumCPU';E={$vm.config.Hardware.NumCpu}},

            @{N='Memory GB';E={$vm.Config.Hardware.MemoryMB| %{[math]::Round($_/1kb,2)}}},

            @{N='vCenter';E={$vc.Name}},

            @{N='VMHost';E={$script:esx = Get-View -Id $vm.Runtime.Host -Server $vc ; $script:esx.name}},

            @{N='GuestOS';E={$vm.Guest.GuestFullName}},

            @{N='ConfiguredOS';E={$vm.Config.GuestFullName}},

            #@{N="Folder";E={$path}},

            @{N="Cluster";E={$cluster}},

            @{N="Datacenter";E={$datacenter}},

            @{N="Scsi";E={$_.UnitNumber}},

            @{N="Hard Disk";E={$_.DeviceInfo.Label}},

            @{N="Disk datastore";E={$_.Backing.Filename.Split(']')[0].TrimStart('[')}},

            @{N='Needs Consolidation';E={$vm.Runtime.consolidationNeeded}},

            @{N="Disk capacity GB";E={$_.CapacityInKB| %{[math]::Round($_/1MB,2)}}},

            @{N="Disk type";E={

                    if($_.Backing.GetType().Name -match "flat"){

                        "Flat"

                    }

                    else{

                        $_.Backing.CompatibilityMode

                    }}},

          @{N='DeviceName';E={

            if($_.Backing.GetType().Name -match 'raw'){

              $_.Backing.DeviceName

            }

            else{

              $script:lunnaa = (Get-View -Id $_.Backing.Datastore -Server $vc ).Info.Vmfs.Extent[0].DiskName

              $script:lun = $script:esx.Config.StorageDevice.ScsiLun | where{$_.CanonicalName -eq $script:lunnaa}

              $script:lun.Descriptor | where{$_.Id -match 'vml.'} | Select -ExpandProperty Id

            }}},

          @{N='LUN NAA';E={

            if($_.Backing.GetType().Name -match 'raw'){

              $lunUuid = $_.Backing.LunUuid

              $script:lun = $script:esx.Config.StorageDevice.ScsiLun | where{$_.Uuid -eq $lunUuid}

              $script:lun.CanonicalName

            }

            else{

              $script:lunnaa

            }}},

          @{N='LUN ID';E={

              $dev = $script:esx.Config.StorageDevice.PlugStoreTopology.Device | where {$_.Lun -eq $script:lun.Key}

              $script:esx.Config.StorageDevice.PlugStoreTopology.Path | where {$_.Device -eq $dev.Key} |

              Select -First 1 -ExpandProperty LunNumber

            }},

          @{N='VMConfigFile';E={$VM.config.files.VMpathname}},

          @{N='VMDKPath';E={$_.Backing.FileName}},

          @{N="HW Version";E={$vm.Config.version}},

          @{N="VMware Tools version";E={$vm.Config.Tools.ToolsVersion}},  

          @{N="Tools Status";E={$vm.guest.toolsversionstatus}},

          @{N="NIC Name";E={($vm.config.hardware.device | where {($_.DeviceInfo.Label -like "Network*")}).DeviceInfo.Label}},

          @{N="Mac"; E={($vm.Config.Hardware.Device | where{$_.DeviceInfo.Label -like "Network*"}).MacAddress}},

          @{N="Portgroup"; E={

            $nic = $vm.Config.Hardware.Device | where{$_.DeviceInfo.Label -like "Network*"}

            [string]::Join(',',(

              $nic | %{

              if($_.DeviceInfo.Summary -notmatch 'DVSwitch'){

                $_.DeviceInfo.Summary

              }

              else{

                Get-View -ViewType DistributedVirtualPortgroup -Property Name -Filter @{'Key'=$_.Backing.Port.PortgroupKey} -Server $vc  |

                Select -ExpandProperty Name

              }}))}}

        }

    }

    $report | Export-Csv report.csv -NoTypeInformation -UseCulture



  • 18.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 05:49 PM

    looks like its works good. But I am able to get the ip address, but its null for all the virtual machines. How come its null ?

    @{N='IP';E={[string]::Join(',',($vm.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}},



    Thanks in advance.



  • 19.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 06:11 PM

    If you don't have the VMware Tools installed on the VM, the IPAddress property might be empty.



  • 20.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 06:22 PM

    This is very weird Because I can not see guest IP addresses for all Vms. Already installed VMware tools on all VMs. But I can retrieve IP addresses via RvTools.



  • 21.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 06:32 PM

    Did you restart the guest OS of the VM after the VMware Tools installation?

    Does this return IP address(es) for all VMs?

    Get-VM |

    Select Name,@{N='IP';E={$_.ExtensionData.Guest.IPAddress -join '|'}}



  • 22.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 07:09 PM

    No luck  not returning any value. This is very frustrating.  How am i going to troubleshoot this?



  • 23.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 07:40 PM

    Do you see the IP address in the vSphere client or the Web client?



  • 24.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 08:19 PM

    ‌I can see IP address via vsphere client no issue



  • 25.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 24, 2016 08:31 PM

    Are you using IPv6 by any chance?



  • 26.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 25, 2016 03:30 AM

    ‌We are not using IPv6 address



  • 27.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 25, 2016 11:39 AM

    Then I'm at my wits end I'm afraid :smileysad:

    So you see the IP address(es) in the Web Client, but the following doesn't show the IP address(es).

    Get-VM |

    Select Name,@{N='IP';E={$_.ExtensionData.Guest.IPAddress -join '|'}}


    I have no explanation for that behaviour, nor can I recreate that I'm afraid.



  • 28.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 25, 2016 12:25 PM

    Hi Luc,

    I have identified very strange something. I assuming this issue is smells like a BUG regarding VMware vSphere PowerCLI 6.3 Release 1. Because I am running same command on VMware vSphere PowerCLI 5.5 Release 2 Patch 1  And I am able to see IPs for all of those VMs. What do you think about this? Or  is there any different syntax for PowerCLI 6.3 ?

    Thanks and

    Kind Regards,



  • 29.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 25, 2016 03:51 PM

    Well, I ran the same script with PowerCLI 6.3R1 against vSphere 5.5 and 6.0, and in both cases I had the IP addresses in the report.

    The syntax should the same, we are accessing a vSphere object, nothing to do with PowerCLI.

    Are you using the same PowerShell version in both environments?

    What does $psversiontable say?



  • 30.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 25, 2016 06:10 PM

    PowerCLI 6.3R1  :

    PowerCLI C:\> $psversiontable

    Name                           Value

    ----                           -----

    PSVersion                      5.0.10586.117

    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}

    BuildVersion                   10.0.10586.117

    CLRVersion                     4.0.30319.34209

    WSManStackVersion              3.0

    PSRemotingProtocolVersion      2.3

    SerializationVersion           1.1.0.1

    PowerCLI  5.5 R2 patch 1 on another vCenter Server  :

    PowerCLI C:\> $psversiontable

    Name                           Value

    ----                           -----

    PSVersion                      4.0

    WSManStackVersion              3.0

    SerializationVersion           1.1.0.1

    CLRVersion                     4.0.30319.34209

    BuildVersion                   6.3.9600.17400

    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}

    PSRemotingProtocolVersion      2.2



  • 31.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 25, 2016 09:57 PM

    A wild guess, can you try the following script in both environments?

    Get-VM |

    Select Name,

        @{N='IP1';E={$_.ExtensionData.Guest.IPAddress -join '|'}},

        @{N='IP2';E={$_.ExtensionData.Guest.Net.IpAddress -join '|'}},

        @{N='IP3';E={($_.ExtensionData.Guest.Net | %{$_.IPAddress}) -join '|'}}



  • 32.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 26, 2016 05:52 AM

    VMware vSphere PowerCLI 5.5 R2

    VMware vSphere PowerCLI 6.3





  • 33.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 26, 2016 06:07 AM

    I just noticed this.

    Are these environments pre-5.0?

    Can you run this one as well in both environments?

    Get-VM |

    Select Name,

        @{N='IP1';E={$_.ExtensionData.Guest.IPAddress -join '|'}},

        @{N='IP2';E={$_.ExtensionData.Guest.Net.IpAddress -join '|'}},

        @{N='IP3';E={($_.ExtensionData.Guest.Net | %{$_.IPConfig | %{$_.IPAddress | %{$_.IPAddress}}}) -join '|'}}



  • 34.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 26, 2016 07:59 AM

    No luck I am getting same results. I can continue with VMware PowercLI 5.5 Also  I want to add script which reports on any datastores over 90% utilisation I have got script like below. How can I adapted my script ?

    foreach ($datastore in (get-datastore | where {$_.name -notlike "*local*" -and [math]::Round(100-($_.freespacegb/$_.capacitygb)*100) -gt 90}))
    {
    $row = '' | select Name,FreeSpaceGB,CapacityGB,vCenter,PercentUsed
    $row.Name = $datastore.name
    $row.FreeSpaceGB = $datastore.freespacegb
    $row.CapacityGB = $datastore.capacitygb
    $row.vCenter = $vcenter.name
    $row.PercentUsed = [math]::Round(100-($datastore.freespacegb/$datastore.capacitygb)*100)
    $report += $row


  • 35.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 26, 2016 08:22 AM

    I would suggest you create a new thread for that last question.
    This thread is already way beyond the thread's title.



  • 36.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Mar 22, 2019 06:38 PM

    LucD​ not sure what I'm doing wrong here with this code, each time I run it seems it does the data fetching however when it tries to build the report it starts spitting out errors.

    Get-View : Cannot validate argument on parameter 'Id'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a

    collection that does not contain any null values and then try the command again.At

    C:\Users\Edward\Documents\WindowsPowerShell\Scripts\VM_InventoryLucD2.ps1:41 char:27

    +             $t = Get-View $t.Parent -Property Name,Parent -Server $vc

    +                           ~~~~~~~~~

        + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException

        + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

    Line 41 of code is:            $t = Get-View $t.Parent -Property Name,Parent -Server $vc

    Only change I made to the code was the path to the output.



  • 37.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Mar 23, 2019 05:31 AM

    Can you attach the code you are running?
    There are a few variations on the script in this thread.



  • 38.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted May 06, 2021 10:29 AM

    Thanks for this awesome script Lucd but how i can see nic connection state for VMs, like is it connected or not ?



  • 39.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted May 06, 2021 10:57 AM

    Add a line like this

    @{N="NIC Connected";E={($vm.config.hardware.device | where {($_.DeviceInfo.Label -like "Network*")}).Connectable.Connected}},
    


  • 40.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted May 06, 2021 11:33 AM

    Thanks Lucd, it worked but its not showing if nic status is set to connected at power on, how i can get that ?



  • 41.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted May 06, 2021 11:46 AM

    In the same way, but use the StartConnected property in the VirtualDeviceConnectInfo object.



  • 42.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted May 06, 2021 02:08 PM

    Thanks Lucd, that helped. It actually create a very big csv file based on environment size, is there any way i can exclude all disk and storage info so that file could be shorten. I can see it give all disks for VM and that VM appears three and four time in report.



  • 43.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted May 06, 2021 03:38 PM

    This script is indeed per vDisk.
    Changing that would make it a rather different script, and out of the scope of this thread.



  • 44.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Sep 16, 2022 08:50 AM

    HI There

    I just found this script, and found it very useful

    How easy would it be to point to just a folder in vsphere, rather than take the whole inventory?

    Thanks in advance



  • 45.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Sep 16, 2022 09:08 AM

    You could use the SearchRoot parameter on the Get-View cmdlets.
    Use the desired Folder's MoRef as a value.

    -SearchRoot (Get-Folder -Name MyFolder).ExtensionData.MoRef


  • 46.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Sep 16, 2022 09:12 AM

    Thanks LucD



  • 47.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Sep 16, 2022 12:21 PM

    I've got this working fine, but just for my knowledge, what is '.ExtensionData.moref' called. So if I was to google it, I could read up on how this works?

    Thanks



  • 48.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Sep 16, 2022 03:45 PM

    The objects returned by PowerCLI cmdlets are so-called .NET objects.
    Their content is composed by the PowerCLI developers.
    They added what most users will need most of the time.

    The ExtensionData property maps to the corresponding vSphere object.
    There you see the internal objects as used in vSphere.
    Their content is described in the vSphere Web Services API.

    A MoRef is short for a Managed Object Reference.
    In short a pointer to a vSphere object



  • 49.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Sep 20, 2022 08:15 AM

    Thank you LucD



  • 50.  RE: PowerCLI Report :Getting inventory VMs from ESX cluster hosts

    Posted Oct 04, 2022 09:39 PM

    I would first like to note that this is my favorite report script, I use it constantly. I have been trying to add a DNS column to the report, however I can't seem to get the syntax right as the DnsName column is just ends up blank.   Here are the two different lines I have added to this script, neither of which have worked. 

    @{N='DnsName';E={$vm.ExtensionData.Guest.Hostname}},

    and

    @{N="DnsName";E={[string]::Join(',',($_.Guest.ExtensionData.Net[0].DnsConfig.IpAddress))}},

    Both of which I've found from other threads, and they work when I only pull that one request in a smaller script. However I can't get either of them to work with this script. In the end I just want the DNS Name line that shows in vSphere.