Wednesday, August 27, 2014

Extra Extended VM inventory using powercli - Part 2

In Decemeber 2013 i had published Extended VM inventory using powercli. I observed that script is taking longer than usual, and taking 4-5 hours to pull record from around 900 VMs. 

I tuned up this script and now it is taking around 1.18 hrs to pull the same data. (Faster one)

And also this time I have added many more things than the earlier script.
  • below are the new addition in the scripts
  • Multiple IP address,
  • Name of the hostname inside VM
  • CPUsocket and core count
  • HDD type info
  • Cluster info
  • Annotation attributes information (you will need to fillup these information in notes>custom attributes)
  • vm resource allocation.

  #####################################   
  ## http://kunaludapi.blogspot.com   
  ## Version: 2  
  ## Date: 27 August 2014    
  ## Script tested on below platform   
  ## 1) Powershell v3   
  ## 2) Powercli v5.1   
  ## 3) Vsphere 5.1   
  ####################################  
 Add-PSSnapin vmware.vimautomation.core  
 Connect-Viserver #vcenterserver    
     
   foreach ($vm in Get-VM) {  
     Write-Host $vm.Name  
     #All global info here  
     $GlobalHDDinfo = $vm | Get-HardDisk  
     $vNicInfo = $vm | Get-NetworkAdapter  
     $Snapshotinfo = $vm | Get-Snapshot  
     $Resourceinfo = $vm | Get-VMResourceConfiguration  
       
     #IPinfo  
     $IPs = $vm.Guest.IPAddress -join "," #$vm.Guest.IPAddres[0] <#it will take first ip#>  
   
     #FQDN - AD domain name  
     $OriginalHostName = $($vm.ExtensionData.Guest.Hostname -split '\.')[0]  
     $Domainname = $($vm.ExtensionData.Guest.Hostname -split '\.')[1,2] -join '.'  
     
     #All hardisk individual capacity  
     $TotalHDDs = $vm.ProvisionedSpaceGB -as [int]  
     
     #All hardisk individual capacity  
       
     $HDDsGB = $($GlobalHDDinfo | select-object -ExpandProperty CapacityGB) -join " + "  
   
     #All HDD disk type,($vdisk.Capacity /1GB -as [int])}  
     $HDDtype = foreach ($HDDtype in $GlobalHDDinfo) {"{0}={1}GB"-f ($HDDtype.Name), ($HDDtype.StorageFormat)}  
     $HDDtypeResult = $HDDtype -join (", ")  
   
     #Associated Datastores  
     $datastore = $(Get-Datastore -vm $vm) -split ", " -join ", "  
     
     #Guest OS Internal HDD info  
     $internalHDDinfo = ($vm | get-VMGuest).ExtensionData.disk  
     $internalHDD = foreach ($vdisk in $internalHDDinfo) {"{0}={1}GB/{2}GB"-f ($vdisk.DiskPath), ($vdisk.FreeSpace /1GB -as [int]),($vdisk.Capacity /1GB -as [int])}  
     $internalHDDResult = $internalHDD -join (", ")  
   
     #vCenter Server  
     $vCenter = $vm.ExtensionData.Client.ServiceUrl.Split('/')[2].trimend(":443")   
   
     #VM Macaddress  
     $Macaddress = $vNicInfo.MacAddress -join ", "  
   
     #Vmdks and its location  
     $vmdk = $GlobalHDDinfo.filename -join ", "  
   
     #Snapshot info  
     $snapshot = $Snapshotinfo.count  
     
     #Datacenter info  
     $datacenter = $vm | Get-Datacenter | Select-Object -ExpandProperty name  
   
     #Cluster info  
     $cluster = $vm | Get-Cluster | Select-Object -ExpandProperty name  
   
     #vNic Info  
     $vNics = foreach ($vNic in $VnicInfo) {"{0}={1}"-f ($vnic.Name.split("")[2]), ($vNic.Type)}  
     $vnic = $vNics -join (", ")  
   
     #Virtual Port group Info  
     $portgroup = $vNicInfo.NetworkName -join ", "  
   
     #RDM Disk Info  
     $RDMInfo = $GlobalHDDinfo | Where-Object {$_.DiskType -eq "RawPhysical"-or $_.DiskType -eq "RawVirtual"}   
     $RDMHDDs = foreach ($RDM in $RDMInfo) {"{0}/{1}/{2}/{3}"-f ($RDM.Name), ($RDM.DiskType),($RDM.Filename), ($RDM.ScsiCanonicalName)}  
     $RDMs = $RDMHDDs -join (", ")  
   
     #Custom Attributes  
     $Annotation = $vm | Get-Annotation  
     $Project = $Annotation | Where-Object {$_.Name -eq "Project"} | Select-Object -ExpandProperty value  
     $SubProject = $Annotation | Where-Object {$_.Name -eq "SubProject"} | Select-Object -ExpandProperty value  
     $Environment = $Annotation | Where-Object {$_.Name -eq "Environment"} | Select-Object -ExpandProperty value  
     $Application = $Annotation | Where-Object {$_.Name -eq "Application"} | Select-Object -ExpandProperty value  
     $Owner = $Annotation | Where-Object {$_.Name -eq "Owner"} | Select-Object -ExpandProperty value  
     $Creationdate = $Annotation | Where-Object {$_.Name -eq "CreationDate"} | Select-Object -ExpandProperty value  
     $Email = $Annotation | Where-Object {$_.Name -eq "Email"} | Select-Object -ExpandProperty value  
       
     $Vmresult = New-Object PSObject   
     $Vmresult | add-member -MemberType NoteProperty -Name "VMName" -Value $vm.Name  
     $Vmresult | add-member -MemberType NoteProperty -Name "IP Address" -Value $IPs  
     $Vmresult | add-member -MemberType NoteProperty -Name "PowerState" -Value $vm.PowerState  
     $Vmresult | add-member -MemberType NoteProperty -Name "Hostname" -Value $OriginalHostName  
     $Vmresult | add-member -MemberType NoteProperty -Name "Domain Name" -Value $Domainname  
     $Vmresult | add-member -MemberType NoteProperty -Name "vCPU" -Value $vm.NumCpu  
     $Vmresult | Add-Member -MemberType NoteProperty -Name CPUSocket -Value $vm.ExtensionData.config.hardware.NumCPU  
     $Vmresult | Add-Member -MemberType NoteProperty -Name Corepersocket -Value $vm.ExtensionData.config.hardware.NumCoresPerSocket  
     $Vmresult | add-member -MemberType NoteProperty -Name "RAM(GB)" -Value $vm.MemoryGB  
     $Vmresult | add-member -MemberType NoteProperty -Name "Total-HDD(GB)" -Value $TotalHDDs  
     $Vmresult | add-member -MemberType NoteProperty -Name "HDDs(GB)" -Value $HDDsGB  
     $Vmresult | add-member -MemberType NoteProperty -Name "HDDsType" -Value $HDDtypeResult  
     $Vmresult | add-member -MemberType NoteProperty -Name "Datastore" -Value $datastore  
     $Vmresult | add-member -MemberType NoteProperty -Name "Partition/Size" -Value $internalHDDResult  
     $Vmresult | add-member -MemberType NoteProperty -Name "Installed-OS" -Value $vm.guest.OSFullName  
     $Vmresult | add-member -MemberType NoteProperty -Name "Setting-OS" -Value $VM.ExtensionData.summary.config.guestfullname  
     $Vmresult | add-member -MemberType NoteProperty -Name "EsxiHost" -Value $VM.VMHost  
     $Vmresult | add-member -MemberType NoteProperty -Name "vCenter Server" -Value $vCenter  
     $Vmresult | add-member -MemberType NoteProperty -Name "Hardware Version" -Value $vm.Version  
     $Vmresult | add-member -MemberType NoteProperty -Name "Folder" -Value $vm.folder  
     $Vmresult | add-member -MemberType NoteProperty -Name "MacAddress" -Value $macaddress  
     $Vmresult | add-member -MemberType NoteProperty -Name "VMX" -Value $vm.ExtensionData.config.files.VMpathname  
     $Vmresult | add-member -MemberType NoteProperty -Name "VMDK" -Value $vmdk  
     $Vmresult | add-member -MemberType NoteProperty -Name "VMTools Status" -Value $vm.ExtensionData.Guest.ToolsStatus  
     $Vmresult | add-member -MemberType NoteProperty -Name "VMTools Version" -Value $vm.ExtensionData.Guest.ToolsVersion  
     $Vmresult | add-member -MemberType NoteProperty -Name "VMTools Version Status" -Value $vm.ExtensionData.Guest.ToolsVersionStatus  
     $Vmresult | add-member -MemberType NoteProperty -Name "VMTools Running Status" -Value $vm.ExtensionData.Guest.ToolsRunningStatus  
     $Vmresult | add-member -MemberType NoteProperty -Name "SnapShots" -Value $snapshot  
     $Vmresult | add-member -MemberType NoteProperty -Name "datacenter" -Value $datacenter  
     $Vmresult | add-member -MemberType NoteProperty -Name "Cluster" -Value $cluster  
     $Vmresult | add-member -MemberType NoteProperty -Name "vNic" -Value $vNic  
     $Vmresult | add-member -MemberType NoteProperty -Name "Portgroup" -Value $portgroup  
     $Vmresult | add-member -MemberType NoteProperty -Name "RDM" -Value $RDMs  
     $Vmresult | add-member -MemberType NoteProperty -Name "Project" -Value $Project  
     $Vmresult | add-member -MemberType NoteProperty -Name "SubProject" -Value $SubProject  
     $Vmresult | add-member -MemberType NoteProperty -Name "Environment" -Value $Environment  
     $Vmresult | add-member -MemberType NoteProperty -Name "Application" -Value $Application  
     $Vmresult | add-member -MemberType NoteProperty -Name "Email" -Value $Email  
     $Vmresult | add-member -MemberType NoteProperty -Name "Owner" -Value $Owner  
     $Vmresult | add-member -MemberType NoteProperty -Name "CreationDate" -Value $Creationdate  
     $Vmresult | add-member -MemberType NoteProperty -Name "NumCpuShares" -Value $Resourceinfo.NumCpuShares  
     $Vmresult | add-member -MemberType NoteProperty -Name "CpuReservationMhz" -Value $Resourceinfo.CpuReservationMhz  
     $Vmresult | add-member -MemberType NoteProperty -Name "CpuLimitMhz" -Value $Resourceinfo.CpuLimitMhz  
     $Vmresult | add-member -MemberType NoteProperty -Name "CpuSharesLevel" -Value $Resourceinfo.CpuSharesLevel  
     $Vmresult | add-member -MemberType NoteProperty -Name "NumMemShares" -Value $Resourceinfo.NumMemShares  
     $Vmresult | add-member -MemberType NoteProperty -Name "MemReservationGB" -Value $Resourceinfo.MemReservationGB  
     $Vmresult | add-member -MemberType NoteProperty -Name "MemLimitGB" -Value $Resourceinfo.MemLimitGB  
     $Vmresult | add-member -MemberType NoteProperty -Name "MemSharesLevel" -Value $Resourceinfo.MemSharesLevel  
     $Vmresult | add-member -MemberType NoteProperty -Name "NumDiskShares" -Value $Resourceinfo.DiskResourceConfiguration.NumDiskShares  
     $Vmresult | add-member -MemberType NoteProperty -Name "DiskSharesLevel" -Value $Resourceinfo.DiskResourceConfiguration.DiskSharesLevel  
     $Vmresult | add-member -MemberType NoteProperty -Name "CpuAffinityList" -Value $Resourceinfo.CpuAffinityList  
     $Vmresult  
   }  
Result look like below
Stay tuned for Extended VMHost inventory........
Powercli VMHost esxi server inventory
VMware Networking 101: VMware Network Load Balancing policies – Part 1
VMware Networking 101: VLAN handling in vSwitches – Part 2
VMware Networking 101: Network Failure Detection – Part 3

Disclaimer:

This is a personal weblog. The opinions expressed here represent my own. If you find any correction need to be done, Feel free to comment on the post.

3 comments:

JB said...

Hello,
your script is excellent. I am using this script to pull my weekly report.

I am having 1 request, is it possible to add Folder name where VM is residing like VM FOLDER


thanks for sharing your script

Anonymous said...

first up...a big thanks for the wonderful work shared.

when i am trying to run this script as below
PS C:\temp>.\vm-inventory.ps1 | export-csv c:\temp\vmlist.csv -notypeinformation

the csv file generated doesnt have any details, while if i run without exporting I can see the information on screen per VM.

would you be able to advise what will I have to add to export the details to csv.

thanks again.

RAJ said...

I have added this below line before the last closing flower bracket
Out-File -FilePath .\vmlist.txt -InputObject $Vmresult -append -Encoding ASCII -Width 50

Report is in txt format.

Check whether this text format helps you.