VMware Cloud Community
bugeyemonster
Enthusiast
Enthusiast

powershell script help

I am working with the script from Ivo Beerens. He has the following lines to collect info about the esx server hardware and build levels.

What i would like to do is combine these two together so that in the output you see the hostname esx version then the hardware info.

I am not much of a programmer and am new to powershell.

Is anyone able to provide advice on this issue?

#######################

  1. VMware ESX hardware #

#######################

Get-VMHost | Get-View | ForEach-Object { $_.Summary.Hardware } | Select-object Vendor, Model, MemorySize, CpuModel, CpuMhz, NumCpuPkgs, NumCpuCores, NumCpuThreads, NumNics, NumHBAs | ConvertTo-Html –title "VMware ESX server Hardware configuration" –body "<H2>VMware ESX server Hardware configuration.</H2>" -head "<link rel='stylesheet' href='style.css' type='text/css' />" | Out-File -Append $filelocation

#######################

  1. VMware ESX versions #

#######################

get-vmhost | % { $server = $_ |get-view; $server.Config.Product | select { $server.Name }, Version, Build, FullName }| ConvertTo-Html –title "VMware ESX server versions" –body "<H2>VMware ESX server versions and builds.</H2>" -head "<link rel='stylesheet' href='style.css' type='text/css' />" | Out-File -Append $filelocation

0 Kudos
21 Replies
LucD
Leadership
Leadership

A quick and dirty method to combine those two.

$filelocation = "C:\report.html"
$report = @()
Get-VMHost | Get-View | %{
  $row = "" | select Name,Vendor, Model, MemorySize, CpuModel, CpuMhz, NumCpuPkgs, NumCpuCores, NumCpuThreads, NumNics, NumHBAs, Version, Build, Fullname
  $row.Name = $_.Name
  $row.Vendor = $_.Summary.Hardware.Vendor
  $row.Model = $_.Summary.Hardware.Model
  $row.MemorySize = $_.Summary.Hardware.MemorySize
  $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.NumNics = $_.Summary.Hardware.NumNics
  $row.NumHBAs = $_.Summary.Hardware.NumHBAs
  $row.Version = $_.Config.Product.Version
  $row.Build = $_.Config.Product.Build
  $row.Fullname = $_.Config.Product.Fullname
  $report += $row 
}
$report | `
  ConvertTo-Html –title "VMware ESX server" –body "<H2>VMware ESX server hardware, versions and builds.</H2>" -head "<link rel='stylesheet' href='style.css' type='text/css'/>" | `
  Out-File -Append $filelocation


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

0 Kudos
bugeyemonster
Enthusiast
Enthusiast

thank you very much that is exactly what i wanted to do. and now i know how to do it.

thanks

0 Kudos
kaizenwerks
Contributor
Contributor

Hi All

I'm having trouble with a similar script.  Could you take a look at it?  Can't seem to get the Version and IP to show up.  FYI I believe some of these are ESXi 3.5 servers.  Thanks

#Setting Variables
Write-Host "Setting Variables...Please wait"
$VMs = Get-VM
$VMHs = Get-VMHost
# Send VM Host Information to Document #
$report = @()
ForEach ($vmh in $vmhs)
{
  $hosts = Get-VMHost $vmh.Name | %{Get-View $_.ID}
  $row = "" | select-Object Hostname, NumCpuCores, CPU_MHz, TotalRAM_MB, Version, IP
  $row.Hostname = $hosts.Name
  $row.NumCpuCores = $hosts.Hardware.CpuInfo.NumCpuCores
  $row.CPU_MHz = [math]::round(($hosts.Hardware.CpuInfo.Hz)/1000000, 0)
  $row.TotalRAM_MB = [math]::round(($hosts.Hardware.MemorySize)/1048576, 0)
  $row.Version = $_.Config.Product.Version
  $row.IP = $_.Config.Network.ConsoleVnic[0].Spec.ip.ipaddress
  $report += $row
   
}
$report | Export-Csv "c:\temp\VMwareExport_HostReport.csv" -noTypeInformation
# Disconnect session from VC #
disconnect-viserver -confirm:$false
exit
0 Kudos
LucD
Leadership
Leadership

Try the following version.

You can use the Extensiondata property instead of doing a Get-View for the host.

I removed the Get-VM because that doesn't seem to be used anywhere.

#Setting Variables 
Write-Host "Setting Variables...Please wait"
$VMHs
= Get-VMHost
#
Send VM Host Information to Document #
$report = @() foreach ($vmh in $vmhs) {     $row = "" | select-Object Hostname, NumCpuCores, CPU_MHz, TotalRAM_MB, Version, IP
   
$row.Hostname = $vmh.Name     $row.NumCpuCores = $vmh.Extensiondata.Hardware.CpuInfo.NumCpuCores     $row.CPU_MHz = [math]::round(($vmh.Extensiondata.Hardware.CpuInfo.Hz)/1000000, 0)     $row.TotalRAM_MB = [math]::round(($vmh.Extensiondata.Hardware.MemorySize)/1MB, 0)     $row.Version = $vmh.Extensiondata.Config.Product.Version     $row.IP = $vmh.Extensiondata.Config.Network.ConsoleVnic[0].Spec.ip.ipaddress     $report += $row } $report | Export-Csv "c:\temp\VMwareExport_HostReport.csv" -noTypeInformation
#
Disconnect session from VC #
disconnect-viserver
-confirm:$falseexit


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

0 Kudos
kaizenwerks
Contributor
Contributor

Thanks for the reply Lucd.

I'm receiving the following error:  Thoughts?

Cannot index into a null array.

At M:\Scripts\VMware\VMware_Host1.ps1:20 char:59

+   $row.IP = $vmh.Extensiondata.Config.Network.ConsoleVnic[ <<<< 0].Spec.ip.ip

address

    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException

    + FullyQualifiedErrorId : NullArray

0 Kudos
LucD
Leadership
Leadership

I suspect you ran this against 1 or more ESXi servers ?

For those the ConsoleVnic property is $null.

This version supports ESX and ESXi

#Setting Variables  
Write-Host
"Setting Variables...Please wait" $VMHs = Get-VMHost
#
Send VM Host Information to Document #
$report
= @()
foreach
($vmh in $vmhs) {     $row = "" | select-Object Hostname, NumCpuCores, CPU_MHz, TotalRAM_MB, Version, IP
   
$row.Hostname = $vmh.Name     $row.NumCpuCores = $vmh.Extensiondata.Hardware.CpuInfo.NumCpuCores     $row.CPU_MHz = [math]::round(($vmh.Extensiondata.Hardware.CpuInfo.Hz)/1000000, 0)     $row.TotalRAM_MB = [math]::round(($vmh.Extensiondata.Hardware.MemorySize)/1MB, 0)     $row.Version = $vmh.Extensiondata.Config.Product.Version     if($vmh.Extensiondata.Config.Product.ProductLineId -eq "embeddedEsx"){        $row.IP = $vmh.Extensiondata.Config.Network.Vnic[0].Spec.ip.ipaddress     }     else{        $row.IP = $vmh.Extensiondata.Config.Network.ConsoleVnic[0].Spec.ip.ipaddress     }     $report += $row
}
$report | Export-Csv "c:\temp\VMwareExport_HostReport.csv" -noTypeInformation
#
Disconnect session from VC #
disconnect-viserver
-confirm:$falseexit


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

0 Kudos
kaizenwerks
Contributor
Contributor

How can this be modified to report back the version and IP for ESXi hosts?  Thanks in advance.

I tried using something like this as well..

if($row.version -like "3.5.*"){
          $row.IP =$_.Config.Network.ConsoleVnic.Spec.ip.ipaddress
  }
  else {$row.IP =$_.Config.Network.ConsoleVnic[0].Spec.ip.ipaddress}
0 Kudos
LucD
Leadership
Leadership

Just added a new version of the script to my previous post.


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

0 Kudos
kaizenwerks
Contributor
Contributor

Hi Lucd,

I tried the updated syntax and it came back with this.  Thoughts?

Cannot index into a null array.
At M:\Scripts\VMware\VMware_Host1.ps1:22 char:64
+        $row.IP = $vmh.Extensiondata.Config.Network.ConsoleVnic[ <<<< 0].Spec.
ip.ipaddress
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
0 Kudos
LucD
Leadership
Leadership

Against which ESX(i) version are you running the script ? 3.x, 4.x ?

Just tested again and it works without a problem against ESX 4.1 and ESXi 4.1


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

0 Kudos
kaizenwerks
Contributor
Contributor

Hi Lucd,

Our environment is currently on 3.5i and 3.5.  Thanks.

0 Kudos
LucD
Leadership
Leadership

I'm sorry but I have no ESX(I) 3.5 left to test against and update the script.

Perhaps someone else can chip in ?


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

0 Kudos
kaizenwerks
Contributor
Contributor

I should mention that our environment has both classic ESX 3.5 and embedded 3.5i hosts.  Shouldn't the script still return something for the classic ESX hosts?

0 Kudos
Chamon
Commander
Commander

I don't have a 3.5 box either but can you try insted of using the ProductLineId try  config.Product.Name

From:

$vmh.Extensiondata.Config.Product.ProductLineId -eq "embeddedEsx"){

To:

$vmh.Config.Product.Name -like "Esxi"){

If that doesn't work can you post the results from

$info=Get-VMHost | Get-View

$info.config.Product

Then we can see what options are available in that object.

0 Kudos
kaizenwerks
Contributor
Contributor

Thanks for the replies..

I haven't had a chance to try your suggestion but I wanted to propose a possible option.

I dug up a script that someone created which provides the version to ESX embedded servers as well. (See attached)  I tried incorporating that syntax into the script I was working on but need some help.  I think I'm closer but still need to put the pieces together.  Here is the output that I'm looking for: "VMware ESX Server 3i 3.5.0 build-163429"  The second step would be obtaining the IP, but I'll take it one step at a time. 😃

Thoughts?

#Setting Variables
Write-Host "Setting Variables...Please wait"
$VMs = Get-VM
$VMHs = Get-VMHost
# Send VM Host Information to Document #
$report = @()
ForEach ($vmh in $vmhs)
{
  $hosts = Get-VMHost $vmh.Name | %{Get-View $_.ID}
  $row = "" | select-Object Hostname, NumCpuCores, CPU_MHz, TotalRAM_MB, Version
  $row.Hostname = $hosts.Name
  $row.NumCpuCores = $hosts.Hardware.CpuInfo.NumCpuCores
  $row.CPU_MHz = [math]::round(($hosts.Hardware.CpuInfo.Hz)/1000000, 0)
  $row.TotalRAM_MB = [math]::round(($hosts.Hardware.MemorySize)/1048576, 0)
  $row.Version = get-vmhost | % { $server = $_ |get-view; $server.Config.Product | select { FullName }}
  $report += $row
   
}
$report | Export-Csv "c:\temp\VMwareExport_HostReport1.csv" -noTypeInformation
0 Kudos
RvdNieuwendijk
Leadership
Leadership

Hi Luc,

I have tried the latest version of your script against a vCenter 4.1 Server and it works for ESX 3.5U5 and ESXi 4.1 hosts.

Regards, Robert

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

Thanks Robert.


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

0 Kudos
kaizenwerks
Contributor
Contributor

I'm kinda at a loss here with my limited scripting abilities and hoping I could get some additional pointers.  I wish we were on vSphere already but unfortunately we're still on ESXi 3.5 and ESX 3.5.

I stumbled my way to obtain the ESXi version but am having trouble with getting the IP address.  It's ugly but the ESXi version output is "@{FullName=VMware ESX Server 3i 3.5.0 build-163429}"  I'm not quite sure if and how to modify the IP address lines.  Thanks in advance.

#Setting Variables
Write-Host "Setting Variables...Please wait"
$VMs = Get-VM
$VMHs = Get-VMHost
# Send VM Host Information to Document #
$report = @()
ForEach ($vmh in $vmhs)
{
  $hosts = Get-VMHost $vmh.Name | %{Get-View $_.ID}
  $row = "" | select-Object Hostname, NumCpuCores, CPU_MHz, TotalRAM_MB, Version, IP
# $row.Hostname = $hosts.Name
  #$row.NumCpuCores = $hosts.Hardware.CpuInfo.NumCpuCores
  #$row.CPU_MHz = [math]::round(($hosts.Hardware.CpuInfo.Hz)/1000000, 0)
  #$row.TotalRAM_MB = [math]::round(($hosts.Hardware.MemorySize)/1048576, 0)
  $row.Version = get-vmhost | % { $server = $_ |get-view; $server.Config.Product | select Fullname }
  if($vmh.Extensiondata.Config.Product.ProductLineId -eq "embeddedEsx"){
     $row.IP = $vmh.Extensiondata.Config.Network.Vnic[0].Spec.ip.ipaddress
   }
  else{
     $row.IP = $vmh.Extensiondata.Config.Network.ConsoleVnic[0].Spec.ip.ipaddress
    }
     $report += $row
   
}
$report | Export-Csv "c:\temp\VMwareExport_HostReport3.csv" -noTypeInformation
0 Kudos
timmp
Enthusiast
Enthusiast

Here is my script and I just can't seem to get the console IP for the ESXi hosts to report.  I have tried every iteration of syntaxes I could drum up but here is what I am using.  I am running this against ESX AND ESXi 4.0 and 4.1 hosts.  Everything outputs correctly EXCEPT the service console IP for the ESXi host.  Maybe somebody can shed some light.

  # ---- ESX Host Hardware Summary Info ----
 
  if ($ShowESXSum){
  Write-CustomOut "..Checking ESX Information"
 
  $HostReport = @()

    Get-VMHost |Sort Name |Get-View |%{
    $Report = "" | select Hostname, Version, Build, Manufacturer, ServerModel, CPU_Model, CPU_Cores, GBMemory, IP_Address, VMotion_IP, HBA_Num, Phy_Nic, OverallStatus
    $Report.Hostname = $_.Name
    $Report.Version =$_.Config.Product.Version
    $Report.Build =$_.Config.Product.Build
    $Report.Manufacturer =$_.Hardware.SystemInfo.Vendor
    $Report.ServerModel =$_.Hardware.SystemInfo.Model
    $Report.CPU_model =$_.Summary.Hardware.CpuModel
    $Report.CPU_Cores =$_.Hardware.CpuInfo.NumCpuCores
    $Report.GBMemory = [math]::round(($_.Hardware.MemorySize)/1024000000, 0)

    if($_.Config.Product.ProductLineId -eq "embeddedEsx"){
       $Report.IP_Address = $_.Extensiondata.Config.Network.Vnic[0].Spec.ip.ipaddress
    }
    else{
       $Report.IP_Address = $_.Config.Network.ConsoleVnic[0].Spec.ip.ipaddress
    }
    $Report.vmotion_ip =$_.Config.Vmotion.IpConfig.IpAddress
    $Report.HBA_num =$_.Summary.Hardware.NumHBAs
    $Report.Phy_nic =$_.Config.Network.Pnic.count
    $Report.OverallStatus = $_.Summary.OverallStatus
    $HostReport += $Report
}
   If (($HostReport | Measure-Object).count -ge 0 -or $ShowAllHeaders) {
    $MyReport += Get-CustomHeader "VMware vSphere Host OS Information" "This Provides the Host FQDN, OS Version and Build, Basic Hardware Information and Relevent IP Addresses"
    $MyReport += Get-HTMLTable $HostReport
    $MyReport += Get-CustomHeaderClose
   }
  }

0 Kudos