VMware Cloud Community
A_S
Enthusiast
Enthusiast

Need some VMhost properties

Hi,

trying to make a small fast inventory script for VMhosts. I need some properties that í don't know how to retrieve.

Any hints?

thanks in advance

Info: (running with powercli 4.1 against vCenters 25/esx 3.5)


Connect-viserver -Server <vc1> <vc2>

$currDir=Split-path -parent $MyInvocation.MyCommand.Definition
$outputFile=$currDir + "\VMHost_Inventory.csv"

$report = @()
Get-View -ViewType HostSystem | % {
$row = "" | Select `
      FQDN, HostName, Cluster, Datacenter, Vendor, Model, uuid, Powerstate, ConnectionState, InMaintenanceMode, `
      bootTime, overallStatus, CPUUsageMhz, CpuUsage%, MemoryUsageMB, MemoryUsage%, `
             cluster,vmotion_ip,version,build,ip_address,Vc, FQDN,Hostname, `
                           fullName, apiVersion,  `
                           BIOSversion, BIOSrelease, memoryGB, `
                           CpuModel, CpuMhz, NumCpuPkgs, NumCpuCores, NumCpuThreads, NumNics, NumHBAs
$row.Vc= $_.Client.ServiceUrl.Split('/')[2]
$row.HostName = $_.name.Split('.')[0]
$row.FQDN=$_.name

## Q1: How to retrieve cluster name (parent) where this server is running?
$row.Cluster=$_.Parent.Name

## Q2: How to retrieve datacenter name (parent of cluster)
  $row.Datacenter=

## Q3: Anyway to retrieve datastore names as array connected to this host?
$row.datastores=
$row.datastoreCount=

## Q4: Anyway to retrieve VM names as array running currently on this VMhost?
$row.VMs=
$row.VMCount=

## Q5: how to retrieve .value of advancedsettings entries?
$row.Disk_SchedNumReqOutstanding_value= $_.Config.option["Disk.SchedNumReqOutstanding"]
$row.QueueDepth_value =?????


$row.vendor = $_.Summary.Hardware.vendor
$row.model = $_.Summary.Hardware.model
$row.uuid = $_.Summary.Hardware.uuid
$row.powerstate = $_.Summary.runtime.powerstate
$row.connectionState = $_.Summary.runtime.connectionState
$row.inMaintenanceMode = $_.Summary.runtime.inMaintenanceMode
$row.bootTime = $_.Summary.runtime.bootTime
$row.overallStatus = $_.Summary.overallStatus

$row.version = $_.Config.Product.version
$row.build = $_.Config.Product.build
$row.apiVersion = $_.Config.Product.apiVersion
$row.fullName = $_.Config.Product.fullName
$row.BIOSversion = $_.Hardware.Biosinfo.biosversion
$row.BIOSrelease = $_.Hardware.Biosinfo.releasedate

$row.CpuModel = $_.Summary.Hardware.CpuModel
$row.cpuMhz = $_.Summary.Hardware.CPUMhz
$row.NumCpuPkgs = $_.Summary.Hardware.NumCpuPkgs
$row.NumCpuCores = $_.Summary.Hardware.NumCpuCores
$row.NumCpuThreads = $_.Summary.Hardware.NumCpuThreads
$row.CPUUsageMhz = $_.Summary.Quickstats.overallCPUUsage
$CPUTotalMhz = $_.Summary.hardware.CPUMhz * $_.Summary.Hardware.NumCpuCores
$row."CpuUsage%" = [math]::round( ($row.CpuUsageMhz / $CPUTotalMhz), 2) * 100

$row.memoryGB = ($_.Summary.Hardware.MemorySize/1GB)
$row.MemoryUsageMB= $_.Summary.Quickstats.OverallMemoryUsage
$MemoryTotalBytes=$_.Summary.Hardware.MemorySize
$row."MemoryUsage%"= [math]::round( ($row.MemoryUsageMB * 1MB / $MemoryTotalBytes), 2) * 100

$row.NumNics = $_.Summary.Hardware.NumNics
$row.ip_address= $_.Config.Network.ConsoleVnic[0].spec.ip.ipaddress
$row.vmotion_ip=$_.Config.Vmotion.ipConfig.ipaddress

$row.NumHBAs = $_.Summary.Hardware.NumHBAs
$report += $row
}
$report | export-csv $outputFile -noTypeInformation

0 Kudos
7 Replies
LucD
Leadership
Leadership

Try the following script.

Note1: you don't have to use the Get-View anymore. The latest Get-VMHost is a lot faster and gives you, through the Extensiondata property, access to the vSphere object.

Note2: The QueueDepth value can be retrieved with Get-EsxCli afaik, but that requires a connection to the ESX server instead of the vCenter server.

# Connect-viserver -Server <vc1> <vc2>  
$currDir
= Split-path -parent $MyInvocation.MyCommand.Definition $outputFile = $currDir + "\VMHost_Inventory.csv" $report = @() foreach($esx in Get-VMHost){     $row = "" | Select FQDN, HostName, Cluster, Datacenter, Vendor, Model, uuid, Powerstate, ConnectionState, InMaintenanceMode,     datastores,datastoreCount,VMs,VMcount,Disk_SchedNumReqOutstanding_value,     bootTime, overallStatus, CPUUsageMhz, CpuUsage%, MemoryUsageMB, MemoryUsage%,     vmotion_ip,version,build,ip_address,Vc,     fullName, apiVersion,     BIOSversion, BIOSrelease, memoryGB,     CpuModel, CpuMhz, NumCpuPkgs, NumCpuCores, NumCpuThreads, NumNics, NumHBAs     $row.Vc= $esx.Extensiondata.Client.ServiceUrl.Split('/')[2]     $row.HostName = $esx.extensiondata.name.Split('.')[0]     $row.FQDN=$esx.name     ## Q1: How to retrieve cluster name (parent) where this server is running?
    $row.Cluster= (Get-Cluster -VMHost $esx).Name     ## Q2: How to retrieve datacenter name (parent of cluster)
   
$row.Datacenter = &{         $parent = Get-View $esx.extensiondata.Parent         while($parent.GetType().Name -ne "Datacenter"){             $parent = Get-View $parent.Parent         }         $parent.Name     }     ## Q3: Anyway to retrieve datastore names as array connected to this host?
    $row.datastores= Get-Datastore -VMHost $esx | %{$_.Name}     $row.datastoreCount= $row.datastores.Count     ## Q4: Anyway to retrieve VM names as array running currently on this VMhost?
    $row.VMs= Get-VM -Location $esx | %{$_.Name}     $row.VMCount= $row.VMs.Count     ## Q5: how to retrieve .value of advancedsettings entries?
    $row.Disk_SchedNumReqOutstanding_value= $esx.extensiondata.Config.option["Disk.SchedNumReqOutstanding"] #     $row.QueueDepth_value =?????     $row.vendor = $esx.extensiondata.Summary.Hardware.vendor     $row.model = $esx.extensiondata.Summary.Hardware.model     $row.uuid = $esx.extensiondata.Summary.Hardware.uuid     $row.powerstate = $esx.extensiondata.Summary.runtime.powerstate     $row.connectionState = $esx.extensiondata.Summary.runtime.connectionState     $row.inMaintenanceMode = $esx.extensiondata.Summary.runtime.inMaintenanceMode     $row.bootTime = $esx.extensiondata.Summary.runtime.bootTime     $row.overallStatus = $esx.extensiondata.Summary.overallStatus     $row.version = $esx.extensiondata.Config.Product.version     $row.build = $esx.extensiondata.Config.Product.build     $row.apiVersion = $esx.extensiondata.Config.Product.apiVersion     $row.fullName = $esx.extensiondata.Config.Product.fullName     $row.BIOSversion = $esx.extensiondata.Hardware.Biosinfo.biosversion     $row.BIOSrelease = $esx.extensiondata.Hardware.Biosinfo.releasedate     $row.CpuModel = $esx.extensiondata.Summary.Hardware.CpuModel     $row.cpuMhz = $esx.extensiondata.Summary.Hardware.CPUMhz     $row.NumCpuPkgs = $esx.extensiondataSummary.Hardware.NumCpuPkgs     $row.NumCpuCores = $esx.extensiondata.Summary.Hardware.NumCpuCores     $row.NumCpuThreads = $esx.extensiondata.Summary.Hardware.NumCpuThreads     $row.CPUUsageMhz = $esx.extensiondata.Summary.Quickstats.overallCPUUsage     $CPUTotalMhz = $esx.extensiondata.Summary.hardware.CPUMhz * $esx.Extensiondata.Summary.Hardware.NumCpuCores     $row."CpuUsage%" = [math]::round( ($row.CpuUsageMhz / $CPUTotalMhz), 2) * 100     $row.memoryGB = ($esx.extensiondata.Summary.Hardware.MemorySize/1GB)     $row.MemoryUsageMB= $esx.extensiondata.Summary.Quickstats.OverallMemoryUsage     $MemoryTotalBytes = $esx.extensiondata.Summary.Hardware.MemorySize     $row."MemoryUsage%"= [math]::round( ($row.MemoryUsageMB * 1MB / $MemoryTotalBytes), 2) * 100     $row.NumNics = $esx.extensiondata.Summary.Hardware.NumNics     $row.ip_address= $esx.extensiondata.Config.Network.ConsoleVnic[0].spec.ip.ipaddress     $row.vmotion_ip=$esx.extensiondata.Config.Vmotion.ipConfig.ipaddress     $row.NumHBAs = $esx.extensiondata.Summary.Hardware.NumHBAs     $report += $row}
$report | export-csv $outputFile -noTypeInformation


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

0 Kudos
RvdNieuwendijk
Leadership
Leadership

Although Luc already gave you an answer I will also give you the one I made. Mostly because I spend quit some time to get the a working answer. And also because I know how to get the Disk.SchedNumReqOutstanding value. I made the same change Luc did to use Get-VMHost instead of Get-View. The Get-VMHost will give you the cluster name in the Parent property. And with the ExtensionData property you can easily go to all the properties you get with Get-View. I removed the part that tried to find the current directory because it didn't work and you don't need it. And I changed the property list because some properties were missing. The script gives and counts only the virtual machines and not the templates on the host.

Connect-viserver -Server <vc1> <vc2>

$outputFile = "VMHost_Inventory.csv"

$report = @()
Get-VMHost | ForEach-Object {
  $VMHost = $_
  $row = "" | Select-Object -Property `
      Vc, HostName, FQDN, Cluster, Datacenter, Datastores, DatastoreCount, `
      VMs, VMCount, Disk_SchedNumReqOutstanding_value, Vendor, Model, uuid, `
      PowerState, ConnectionState, InMaintenanceMode, BootTime, OverallStatus, `
      Version, Build, ApiVersion, FullName, BIOSVersion, BIOSRelease, CpuModel, `
      CpuMhz, NumCpuPkgs, NumCpuCores, NumCpuThreads, CpuUsageMhz, CpuTotalMhz, `
      CpuUsage% ,MemoryGb, MemoryUsageMB, MemoryTotalBytes, MemoryUsage%, `
      NumNics, Ip_Address, vMotion_ip, NumHBAs

  $row.Vc = $VMHost.ExtensionData.Client.ServiceUrl.Split('/')[2]
  $row.HostName = $VMHost.ExtensionData.name.Split('.')[0]
  $row.FQDN = $VMHost.ExtensionData.name

  ## Q1: How to retrieve cluster name (parent) where this server is running?
  $row.Cluster = $VMHost.Parent

  ## Q2: How to retrieve datacenter name (parent of cluster)
  $row.Datacenter= (Get-Datacenter -Cluster $VMHost.Parent).Name

  ## Q3: Anyway to retrieve datastore names as array connected to this host?
  $Datastores = ""
  $VMHost | Get-Datastore | ForEach-Object {$Datastores += "$($_.Name),"}
  $row.Datastores = $Datastores.TrimEnd(",")
  $row.DatastoreCount = $VMHost.ExtensionData.Datastore.Count
 
  ## Q4: Anyway to retrieve VM names as array running currently on this VMhost?
  $VMs = ""
  $VMHost | Get-VM | ForEach-Object {$VMs += "$($_.Name),"}
  $row.VMs = $VMs.TrimEnd(",")
  $row.VMCount = @($VMHost | Get-VM).Count

  ## Q5: how to retrieve .value of advancedsettings entries?
  $row.Disk_SchedNumReqOutstanding_value = `
    ($VMHost | Get-VMHostAdvancedConfiguration)["Disk.SchedNumReqOutstanding"]

  $row.Vendor = $VMHost.ExtensionData.Summary.Hardware.vendor
  $row.Model = $VMHost.ExtensionData.Summary.Hardware.model
  $row.uuid = $VMHost.ExtensionData.Summary.Hardware.uuid
  $row.PowerState = $VMHost.ExtensionData.Summary.runtime.powerstate
  $row.ConnectionState = $VMHost.ExtensionData.Summary.runtime.connectionState
  $row.InMaintenanceMode = $VMHost.ExtensionData.Summary.runtime.inMaintenanceMode
  $row.BootTime = $VMHost.ExtensionData.Summary.runtime.bootTime
  $row.OverallStatus = $VMHost.ExtensionData.Summary.overallStatus

  $row.Version = $VMHost.ExtensionData.Config.Product.version
  $row.Build = $VMHost.ExtensionData.Config.Product.build
  $row.ApiVersion = $VMHost.ExtensionData.Config.Product.apiVersion
  $row.FullName = $VMHost.ExtensionData.Config.Product.fullName
  $row.BIOSversion = $VMHost.ExtensionData.Hardware.Biosinfo.biosversion
  $row.BIOSrelease = $VMHost.ExtensionData.Hardware.Biosinfo.releasedate

  $row.CpuModel = $VMHost.ExtensionData.Summary.Hardware.CpuModel
  $row.CpuMhz = $VMHost.ExtensionData.Summary.Hardware.CPUMhz
  $row.NumCpuPkgs = $VMHost.ExtensionData.Summary.Hardware.NumCpuPkgs
  $row.NumCpuCores = $VMHost.ExtensionData.Summary.Hardware.NumCpuCores
  $row.NumCpuThreads = $VMHost.ExtensionData.Summary.Hardware.NumCpuThreads
  $row.CpuUsageMhz = $VMHost.ExtensionData.Summary.Quickstats.overallCPUUsage
  $row.CpuTotalMhz = $VMHost.ExtensionData.Summary.hardware.CPUMhz * `
    $VMHost.ExtensionData.Summary.Hardware.NumCpuCores
  $row."CpuUsage%" = [math]::round( ($row.CpuUsageMhz / $CPUTotalMhz), 2) * 100

  $row.MemoryGB = ($VMHost.ExtensionData.Summary.Hardware.MemorySize/1GB)
  $row.MemoryUsageMB= $VMHost.ExtensionData.Summary.Quickstats.OverallMemoryUsage
  $row.MemoryTotalBytes=$VMHost.ExtensionData.Summary.Hardware.MemorySize
  $row."MemoryUsage%"= [math]::round( ($row.MemoryUsageMB * 1MB / $MemoryTotalBytes), 2) * 100

  $row.NumNics = $VMHost.ExtensionData.Summary.Hardware.NumNics
  $row.Ip_Address= $VMHost.ExtensionData.Config.Network.ConsoleVnic[0].spec.ip.ipaddress
  $row.vMotion_ip=$VMHost.ExtensionData.Config.Vmotion.ipConfig.ipaddress

  $row.NumHBAs = $VMHost.ExtensionData.Summary.Hardware.NumHBAs
  $report += $row
}
$report | export-csv $outputFile -noTypeInformation -UseCulture

Regards, Robert

Message was edited by: RvdNieuwendijk

I now see that I misunderstood question 5. At the moment I also don't know how to retrieve the QueueDepth value using PowerCLI. I will try to find a slution.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership

You can't retrieve the queuedepth with the Get-EsxCli cmdlet (sorry I put you on the wrong foot there).

You can do something like this.

This is for a QLogic HBA. If you use another HBA you will have to change the module name.

(Get-VMhostModule -Name qla2xxx).Options.ql2xmaxqdepth

But you need to be connected to the ESX server instead of the vCenter Server.


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

0 Kudos
A_S
Enthusiast
Enthusiast

Gents,

I was happy to make it working. There were some typo's in both solutions. I made a mix (Q1,Q2,Q3=lucd and Q4,Q5=Robert. But, in the other side i didn't expect that these 5 extra properties would make my script run slower.

I executed this against 2 vCenters (40esx hosts behind)  and it takes 4 minutes to finish. It's 3 min 10 seconds longer than my first script. I'm going to test further and try to optimize it somehow.

Concerning Queuedepth value. Not allowed to run directly against Esx hosts (no root permissions, only vCenter rights). I will match the values with output generated directly from esx cron jobs.

the final script is posted back.

Thank you for your help and Marry Christmas

0 Kudos
wiggy66
Contributor
Contributor

I've been using this script and it works well for me.

There is one little addon which i think would compliment it but im unsure on how to get it to work.

## Q3:
    $Datastores = ""
    $serv | Get-Datastore | ForEach-Object {$Datastores += "$($_.Name)|"}
    $row.Datastores = $Datastores.TrimEnd("|")
    $row.DatastoreCount = $serv.ExtensionData.Datastore.Count

How would I add total Datastore FreeSpaceMB and CapacityMB into this area.

Thanks!

0 Kudos
RvdNieuwendijk
Leadership
Leadership

You can add total Datastore FreeSpaceMB and CapacityMB into the script by making the following changes. I took my script to make them.

  $row = "" | Select-Object -Property `
      Vc, HostName, FQDN, Cluster, Datacenter, Datastores, DatastoreCount, `
      DatastoresFreeSpaceMB, DatastoresCapacityMB, `
      VMs, VMCount, Disk_SchedNumReqOutstanding_value, Vendor, Model, uuid, `
      PowerState, ConnectionState, InMaintenanceMode, BootTime, OverallStatus, `
      Version, Build, ApiVersion, FullName, BIOSVersion, BIOSRelease, CpuModel, `
      CpuMhz, NumCpuPkgs, NumCpuCores, NumCpuThreads, CpuUsageMhz, CpuTotalMhz, `
      CpuUsage% ,MemoryGb, MemoryUsageMB, MemoryTotalBytes, MemoryUsage%, `
      NumNics, Ip_Address, vMotion_ip, NumHBAs

And:

## Q3:
$Datastores = ""
$DatastoresFreeSpaceMB = 0
$DatastoresCapacityMB = 0
    $serv | Get-Datastore | ForEach-Object {
      $Datastores += "$($_.Name)|"
      $DatastoresFreeSpaceMB += $_.FreeSpaceMB
      $DatastoresCapacityMB += $_.CapacityMB
    }
    $row.Datastores = $Datastores.TrimEnd("|")
    $row.DatastoreCount = $serv.ExtensionData.Datastore.Count
    $row.DatastoresFreeSpaceMB = $DatastoresFreeSpaceMB
    $row.DatastoresCapacityMB = $DatastoresCapacityMB

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
wiggy66
Contributor
Contributor

Works a treat, Thanks

0 Kudos