Highlighted
Contributor
Contributor

PowerCLI Report :Getting inventory VMs from ESX cluster hosts

Jump to solution

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
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
36 Replies
Highlighted
Expert
Expert

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, ...}.

http://www.vxav.fr
0 Kudos
Highlighted
Expert
Expert

You can try this

Complete VM information

Regards Vineeth.K
0 Kudos
Highlighted
Contributor
Contributor

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?

0 Kudos
Highlighted
User Moderator
User Moderator

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}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Highlighted
Contributor
Contributor

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
      }}))}}
}
0 Kudos
Highlighted
User Moderator
User Moderator

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Highlighted
Contributor
Contributor

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.

0 Kudos
Highlighted
User Moderator
User Moderator

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Highlighted
Contributor
Contributor

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

0 Kudos
Highlighted
User Moderator
User Moderator

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Highlighted
Contributor
Contributor

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

0 Kudos
Highlighted
User Moderator
User Moderator

Oops, typo.

Corrected it.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Highlighted
Contributor
Contributor

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

0 Kudos
Highlighted
User Moderator
User Moderator

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Highlighted
Contributor
Contributor

Normally I am using this powercli script the first time.

0 Kudos
Highlighted
User Moderator
User Moderator

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
Highlighted
Contributor
Contributor

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.

0 Kudos
Highlighted
User Moderator
User Moderator

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Highlighted
Contributor
Contributor

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.

0 Kudos