Expert
Expert

Complete VM information

Jump to solution

Hi

I'm pretty new to get-view with the help of the old threads form the Master LucD I written the below Code but i need to add few more information in the code .

LucD can you please help me out.

Information need to fetch :

Lun NAA of each hardisk in output

Lun ID of that NAA

VMConfigFile

VMDKpath for each harddisk

Size of that .vmdk

-------------------------------------

Some thing went wrong in the code unable to get below information

IP address (Some vms contains multiple IPs )

network adapter (like.. network adapter 1 etc..)

need port group information for DVS b/c some vms are on DVS

=============

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,guest.toolsversionstatus)){

   # $t = Get-View $vm.Parent -Property Name,Parent

    #$path = $t.Name

    #while($t.GetType().Name -eq "Folder"){

     #   $t = Get-View $t.Parent

      #  if($t.Name -ne "vm"){

       #     $path = $t.Name + "/" + $path

        #}

    #}

    $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={$add=($vm.Guest.Net | %{$_.IpAddress})}.(([string]::Join(',',$add)))},

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

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

    @{N='VMHost';E={(Get-View -Id $vm.Runtime.Host).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={($_.DeviceInfo.Label).BusNumber}},

    @{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="HW Version";E={$vm.Config.version}},

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

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

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

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

}

Sample Output :

output.JPG

Regards Vineeth.K
0 Kudos
1 Solution

Accepted Solutions
Leadership
Leadership

Try like this

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,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="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

       }}))}}

}


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

View solution in original post

0 Kudos
24 Replies
Leadership
Leadership

Some questions, how do you want to display information for items that have multiple instances, i.e. IP addresses, hard disk info ?

If you want to export this information to a CSV file, the rows ideally should have the same number of properties.

What exactly do you mean with the LUN for a harddisk ?

Is that the LUN behind the datastore on which the harddisk VMDK file is located ?

Or do you need the LUN only for RDM harddisks ?


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

0 Kudos
Expert
Expert

Hi LucD

Thanks, I need details somewhat like below ..I will export info to .csv like this vminfo.ps1 | export-csv

Lun for hardisk -- Lun(datastore) NAA for each harddisk

If RDM is present i need NAA of of that RDM Disk if possible.

VMIP addressDisk TypeHard DiskDatastoreDeviceNameLUN NAALUNIDLUNWWNVMConfigFileVMDKpathVMDK SizeDrive Size
TestVM10.50.45.125,10.50.46.72FlatHard disk 1Hitachi_USPVM_Lun01vml.020011000060060e8010144f30058b44c30000012c444636303046naa.60060e8010144f30058b44c30000012c050060e80058e8a01,50060e80058e8a11[Hitachi_USPVM_Lun01] test/test.vmx[Hitachi_USPVM_Lun01] test/test.vmdk4040
TestVM10.50.45.125,10.50.46.72FlatHard disk 2SAN_CX4_480_(406)_LUN_2vml.02000d000060060e8010144f30058b44c30000011a444636303046naa.60060e8010144f30058b44c30000011a150060160c46001a7[Hitachi_USPVM_Lun01] test/test.vmx[SAN_CX4_480_(406)_LUN_2] test/test.vmdk131131
TestVM10.50.45.125,10.50.46.72RawPhysicalHard disk 3Hitachi_USPVM_Lun01vml.02000e000060060e8010144f30058b44c30000011b444636303046naa.60060e8010144f30058b44c30000011b25005076802058f5d,5005076802058f5e[Hitachi_USPVM_Lun01] test/test.vmx[Hitachi_USPVM_Lun01] test/test.vmdk100100
TestVM10.50.45.125,10.50.46.72RawPhysicalHard disk 4Hitachi_USPVM_Lun01vml.02000f000060060e8010144f30058b44c30000011c444636303046naa.60060e8010144f30058b44c30000011c95005076802058f5d,5005076802058f5e[Hitachi_USPVM_Lun01] test/test.vmx[Hitachi_USPVM_Lun01] test/test.vmdk100100
Regards Vineeth.K
0 Kudos
Expert
Expert

Hi Luc

Is the information given below post is OK or need any more info.

Regards Vineeth.K
0 Kudos
Expert
Expert

Get-VM | ForEach-Object {

    $vm = $_

    $_ | Get-HardDisk | foreach {

        $HDD = $_

        $VMName = $vm.Name

        $IPAddress =  $vm.guest.ipaddress -join ', '

        $VMConfigFile = $HDD.Filename

        $Datastore = $($VMConfigFile -split ']')[0].Substring(1)

        $DeviceName = $HDD.DeviceName

        $NAA = $(Get-Datastore $Datastore).extensiondata.Info.vmfs.extent.Diskname

        $Esxi = $VM.VMHost

        $LunID =  $esxi.Extensiondata.config.storagedevice.ScsiTopology.Adapter.target.lun.lun[0]

        $VMConfigFile = $VM.ExtensionData.config.files.VMpathname

        $VMDKSize = $HDD.CapacityGB

        $hash = @{           

        VM = $VMName

        Host = $Esxi

        IPAddress = $IPAddress

        Disktype = $HDD.DiskType

        HardDisk = $HDD.Name

        Datastore = $Datastore

        DeviceName = $DeviceName

        LunNAA = $NAA

        LUNID = $LunID

        VMDKPath = $VMConfigFile

        VMDKSize = $VMDKSize

        }                          

        $Object = New-Object PSObject -Property $hash

        $Object           

    }

}

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
Expert
Expert

Thanks Kunal for taking time to write this codeSmiley Happy This is OK but I'm looking to write using  Get-View as i need to run on 5000+vms  you go through the above code there I only missing datastore info like Harddisk,naa,LunID...using Get-View

Is it possible to rewrite the code given in original post using get-view for getting required storage details..

Regards Vineeth.K
0 Kudos
Contributor
Contributor

Have you looked at RVTOOLs it will give you a complete inventory.

0 Kudos
Expert
Expert

Hi Saki

Yes I aware of RVTools but not all information will be available which i mentioned on the post.

Regards Vineeth.K
0 Kudos
Expert
Expert

Hi Luc

Is I'm clear with the questions you raised in the post or any additional information required.

Regards Vineeth.K
0 Kudos
Leadership
Leadership

When other people answer a thread, I normally don't barge in anymore.

Try something like this

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,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={$add=($vm.Guest.Net | %{$_.IpAddress})}.(([string]::Join(',',$add)))}, 

    @{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

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

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

    }}},

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

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

      $lunUuid = $_.Backing.LunUuid

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

      $lun.CanonicalName

    }

    else{

      $script:lunnaa

    }}},

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

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

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

   @{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={($vm.Config.Hardware.Device | where{$_.DeviceInfo.Label -like "Network*"}).DeviceInfo.Summary}} 

}  


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

0 Kudos
Expert
Expert

Thanks a lot LucD:smileyhappy:Smiley Happy for spending your valuable time to write the complete script.

This works perfectly as per my requirement.

Only three steps missing..

Correct me in IP,Portgroup & Lun ID missing

Not able to retrieve IP address

@{N="IP";E={[string]::Join(',',$VM.Guest.net)|where ForEach-Object {$_.IPAddress} | Where-Object {$_.split(".").length -eq 4}}},

====================

Unable to retrieve Port group ID for vds b/c this environment is mixed with  vds/vss.

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

output:

Portgroup        : DVSwitch: f8 28 0e 50 06 6d a0 fa-4f 30 5b 17 e6 af 94 40

====================

LUN ID

LUN ID for each naa is missing

For eg in output:

LUN NAA          : naa.60060e8010144f30058b44c30000013d

LUN ID             :  1

Regards Vineeth.K
0 Kudos
Leadership
Leadership

Try like this

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,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 | %{$_}}))}},

    @{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="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*"}

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

      $nic.DeviceInfo.Summary

     }

     else{

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

      Select -ExpandProperty Name

     }}}

}

   


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

0 Kudos
Expert
Expert

Perfect Guru:smileycool:

Two steps missing

Need to filter and exclude IPV6 while writing output.

@{N='IP';E={[string]::Join(',',($vm.Guest.Net | %{$_.IpAddress | %{$_}})) |where ForEach-Object {$_} | Where-Object {$_.split(".").length -eq 4}}},

===========

If a vm contains two network adapters it is not writing portgroup name.

@{N="Portgroup"; E={

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

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

      $nic.DeviceInfo.Summary

     }

     else{

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

      Select -ExpandProperty Name

     }}}

--

Sample output:

NIC Name         : {Network adapter 1, Network adapter 2}

Mac              : {00:50:56:99:58:10, 00:50:56:99:58:11}

Portgroup        :

Regards Vineeth.K
0 Kudos
Leadership
Leadership

Try like this

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,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="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

       }}))}}

}


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

View solution in original post

0 Kudos
Expert
Expert

Thanks LucSmiley Happy.Now the code is retrieving information as per my requirement.

Regards Vineeth.K
0 Kudos
Enthusiast
Enthusiast

Hi LucD,

How can I export output of this script to CSV format.

Also, I am not able to get the Folder details of the VM. Please help LucD Guru Smiley Happy

0 Kudos
Contributor
Contributor

Hi Luc,

Please provide me the complete script to run in Multiple vcenter servers

0 Kudos
Leadership
Leadership

Try like this

foreach ($vc in $global:DefaultVIServers)

{

   foreach ($vm in (Get-View -Server $vc -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, guest.toolsversionstatus,

  Config.Files.VMPathName))

   {

   $t = Get-View -Id $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 = '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 = "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 -Server $vc -ViewType DistributedVirtualPortgroup -Property Name -Filter @{'Key' = $_.Backing.Port.PortgroupKey } |

  Select -ExpandProperty Name

   } })) }

   }

   }

}


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

0 Kudos
Contributor
Contributor

Hi LuCD,

Script is working fine but if the server has 2 disks then it displayed 2 times. Can we export into CSV format in which multiple hard disk are added in new row.

0 Kudos
Leadership
Leadership

Are those entries really duplicates?
Aren't properties like Unit, Filename... different?


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

0 Kudos