PowerCLI

 View Only
Expand all | Collapse all

Get-VMHostHardware |

  • 1.  Get-VMHostHardware |

    Posted Mar 11, 2023 08:51 PM
    Hello,
     
    I'm using this command to collect HardwareInfo from ESXi
     
    $1 = $VMHost | Get-VMHostHardware -SkipAllSslCertificateChecks -ErrorAction SilentlyContinue
     
    Unfortunately, I got an error message :

    Cmdlet Get-VMHostHardware is not supported on PowerShell Core.
     
    Would you please assist me in solving the issue?


  • 2.  RE: Get-VMHostHardware |

    Posted Mar 11, 2023 09:17 PM

    What is there to solve?
    The message is rather clear, the cmdlet is not supported on PS Core.

    As alternatives, you could use PSv5.1 or get the HW info from the HostSystem object directly.
    What HW info specifically are you looking for?



  • 3.  RE: Get-VMHostHardware |

    Posted Mar 11, 2023 09:27 PM

    Ok, then it's more clear now, my Bad as I started creating the below loop in order to collect some details from ESXi

     

    Foreach ($VMHost in $ListOfVMHosts) {
        $1 = $null
        $1 = $VMHost | Get-VMHostHardware -SkipAllSslCertificateChecks -ErrorAction SilentlyContinue
        If ($1) {
            Write-Output -Message "Processing host $VMHost.Name [$Counter/$Count]"
            #Write-Host "Processing host $VMHost.Name [$Counter/$Count]"
     
            $HostHardware = $VMHost |  Get-VMHostHardware -SkipAllSslCertificateChecks
            $HostNetwork = $VMHost |  Get-VMHostNetwork
     
            $HostNetworkvMotionAdapter = $VMHost |  Get-VMHostNetworkAdapter -VMKernel |  Where-Object { $_.VMotionEnabled -eq $true }
            $HostNetworkManagementTrafficAdapter = $VMHost |  Get-VMHostNetworkAdapter -VMKernel |  Where-Object { $_.ManagementTrafficEnabled -eq $true }
            $HostNetworkvSANTrafficAdapter = $VMHost |  Get-VMHostNetworkAdapter -VMKernel |  Where-Object { $_.VsanTrafficEnabled -eq $true }
     
            $MemoryTotalGB = [math]::Round($VMHost.MemoryTotalGB)
     
            $Object = [PSCustomObject]@{
                SerialNumber     = $HostHardware.SerialNumber # Serialnumber needs to be first for VLOOKUP to work in Excel
                vCenter          = $VMHost.Uid.Substring($VMHost.Uid.IndexOf('@') + 1).Split(":")[0]
                DataCenter       = ($VMHost | Get-Datacenter)
                Cluster          = $VMHost.Parent
                HostName         = $VMHost.Name
                Manufacturer     = $VMHost.Manufacturer
                Model            = $VMHost.Model
                Bios             = $HostHardware.BiosVersion
                Processor        = $VMHost.ProcessorType
                CPU              = $HostHardware.CpuCount
                CPUCores         = $HostHardware.CpuCoreCountTotal
                MemoryGB         = $MemoryTotalGB
                ESXVersion       = $VMHost.ExtensionData.Summary.Config.Product.FullName
                ESXLicenseKey    = $VMHost.LicenseKey
                DNS1             = $HostNetwork.DnsAddress[0]
                DNS2             = $HostNetwork.DnsAddress[1]
                Gateway          = $HostNetwork.VMKernelGateway
                ManagementDevice = $HostNetworkManagementTrafficAdapter.name
                ManagementIP     = $HostNetworkManagementTrafficAdapter.ip
                ManagementMTU    = $HostNetworkManagementTrafficAdapter.mtu
                vMotionDevice    = $HostNetworkvMotionAdapter.name
                vMotionIP        = $HostNetworkvMotionAdapter.ip
                vMotionMTU       = $HostNetworkvMotionAdapter.mtu
                vSANDevice       = $HostNetworkvSANTrafficAdapter.name
                vSANIP           = $HostNetworkvSANTrafficAdapter.ip
                vSANMTU          = $HostNetworkvSANTrafficAdapter.mtu
            }
     
            $Report.add($Object) | Out-Null
        } Else {
            Write-Output -Message  "There was an error processing host $VMHost.Name [$Counter/$Count]"
        }
        $Counter++
    }


  • 4.  RE: Get-VMHostHardware |
    Best Answer

    Posted Mar 11, 2023 10:35 PM

    You can do

    Foreach ($VMHost in $ListOfVMHosts) {
      Write-Output -Message "Processing host $VMHost.Name [$Counter/$Count]"
    
      $HostNetwork = $VMHost |  Get-VMHostNetwork
    
      $HostNetworkvMotionAdapter = $VMHost |  Get-VMHostNetworkAdapter -VMKernel |  Where-Object { $_.VMotionEnabled -eq $true }
      $HostNetworkManagementTrafficAdapter = $VMHost |  Get-VMHostNetworkAdapter -VMKernel |  Where-Object { $_.ManagementTrafficEnabled -eq $true }
      $HostNetworkvSANTrafficAdapter = $VMHost |  Get-VMHostNetworkAdapter -VMKernel |  Where-Object { $_.VsanTrafficEnabled -eq $true }
    
      $MemoryTotalGB = [math]::Round($VMHost.MemoryTotalGB)
    
      $Object = [PSCustomObject]@{
        SerialNumber = $VMHost.ExtensionData.Hardware.SystemInfo.SerialNumber
        vCenter = $VMHost.Uid.Substring($VMHost.Uid.IndexOf('@') + 1).Split(":")[0]
        DataCenter = ($VMHost | Get-Datacenter)
        Cluster = $VMHost.Parent
        HostName = $VMHost.Name
        Manufacturer = $VMHost.Manufacturer
        Model = $VMHost.Model
        Bios = $VMHost.ExtensionData.Hardware.BiosInfo.BiosVersion
        Processor = $VMHost.ProcessorType
        CPU = $VMHost.ExtensionData.Hardware.CpuInfo.NumCpuPackages
        CPUCores = $VMHost.ExtensionData.Hardware.CpuInfo.NumCpuCores
        MemoryGB = $MemoryTotalGB
        ESXVersion = $VMHost.ExtensionData.Summary.Config.Product.FullName
        ESXLicenseKey = $VMHost.LicenseKey
        DNS1 = $HostNetwork.DnsAddress[0]
        DNS2 = $HostNetwork.DnsAddress[1]
        Gateway = $HostNetwork.VMKernelGateway
        ManagementDevice = $HostNetworkManagementTrafficAdapter.name
        ManagementIP = $HostNetworkManagementTrafficAdapter.ip
        ManagementMTU = $HostNetworkManagementTrafficAdapter.mtu
        vMotionDevice = $HostNetworkvMotionAdapter.name
        vMotionIP = $HostNetworkvMotionAdapter.ip
        vMotionMTU = $HostNetworkvMotionAdapter.mtu
        vSANDevice = $HostNetworkvSANTrafficAdapter.name
        vSANIP = $HostNetworkvSANTrafficAdapter.ip
        vSANMTU = $HostNetworkvSANTrafficAdapter.mtu
      }
    
      $Report.add($Object) | Out-Null
      $Counter++
    }
    


  • 5.  RE: Get-VMHostHardware |

    Posted Mar 13, 2023 07:48 PM

    Hi  

     

    Thank you it's working except that the Serial numbers aren't retrieved  

    I will do some other tests and keep you posted

     

    Thank you again



  • 6.  RE: Get-VMHostHardware |

    Posted Mar 13, 2023 08:02 PM

    The SerialNumber property in the HostSystemInfo object only has a valid value since vSphere 6.7.
    Is the ESXi node you are running against 6.7 or higher?



  • 7.  RE: Get-VMHostHardware |

    Posted Mar 14, 2023 02:44 PM

    Thanks for the detail

    I confirm that I have some servers running 6.7 version and not all of them show S/N, strange behavior, right?



  • 8.  RE: Get-VMHostHardware |
    Best Answer

    Posted Mar 14, 2023 04:14 PM

    Not really, some HW information is not passed correctly by some vendors and for some models.
    It would be interesting to check if the Get-VMHostHardware cmdlet, run in a PSv7 session, returns the correct serial.



  • 9.  RE: Get-VMHostHardware |

    Posted Jul 24, 2024 10:31 AM

    I ran into the same issue as Sabri. But..wait...the script he posts looks very much like the script I have posted on my website. Looks like someone actually uses my scripts :-)

    Anyways, I ran into the same issue with empty serial numbers. I fixed it by using this line:
    SerialNumber     = ($VMHost.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo | Where-Object { $_.IdentifierType.Key -eq "ServiceTag" }).IdentifierValue




  • 10.  RE: Get-VMHostHardware |

    Posted Jul 25, 2024 09:52 AM

    Maybe you can try this to see if it retrieves the SN:
    $vmhost.ExtensionData.Hardware.SystemInfo.SerialNumber

    Also I have used New-VIProperty to get host serials before like this.. 
    New-VIProperty -ObjectType VMHost -Name SerialNumber -Value { (Get-EsxCli -VMHost $Args[0]).hardware.platform.get().SerialNumber }

    Get-VMHost | Select Name,Manufacturer,Model,SerialNumber | FT




  • 11.  RE: Get-VMHostHardware |

    Posted Jul 25, 2024 11:19 AM

    This line worked perfect for me:

    SerialNumber = ($VMHost.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo | Where-Object { $_.IdentifierType.Key -eq "ServiceTag" }).IdentifierValue




  • 12.  RE: Get-VMHostHardware |

    Posted Aug 01, 2024 01:35 PM

    How can I define $ListOfVMHosts? I tried something like this but getting output only for one host. 

    # Initialize the counter and report array
    $Counter = 1
    $Report = @()
    $hostListFilePath = "path to esxihosts_hw.txt"
    $hostNames = Get-Content -Path $hostListFilePath
    $ListOfVMHosts = Get-VMHost $hostNames
    $Count = $ListOfVMHosts.Count
     
    Foreach ($VMHost in $ListOfVMHosts) {
        Write-Host "Processing host $($VMHost.Name) [$Counter/$Count]"
    .....
    ......
     $Report += $Object
        $Counter++
    }
     
    # Output the report or save it to a file
    $Report | Format-Table -AutoSize



  • 13.  RE: Get-VMHostHardware |

    Posted Aug 02, 2024 02:42 AM

    In my original script I define it like this:

    $Credentials = Get-Credential -Message "Please enter the username and password to use to connect to the vCenter(s)"
     
    # Connect to vCenter(s)
    $ListOfvCenters = (
        "<FQDN/IP address of vCenter #1",
        "vcenter01.yourdomain.local",
        "10.10.10.50"
    )
    Connect-VIServer $ListOfvCenters -Credential $Credentials -ErrorAction Continue
     
    # Get a list of all ESXi hosts running in the vCenter(s)
    $ListOfVMHosts = Get-VMHost | Sort-Object Name
     
    # Get required information for each host
    $Count = $ListOfVMHosts.Count
    $Counter = 1



  • 14.  RE: Get-VMHostHardware |

    Posted Aug 02, 2024 04:53 AM

    Try:

    $hostnames = [System.IO.File]::ReadAllLines( $hostListFilePath )




  • 15.  RE: Get-VMHostHardware |

    Posted Aug 02, 2024 09:29 AM

    Hi Dennis K, 

    Thank you. Can you please post your complete final script? I am having an issue in pulling array report.   

    Best,

    S




  • 16.  RE: Get-VMHostHardware |

    Posted Aug 02, 2024 09:39 AM

    Sure, here it is. Any improvements to make the script better, be sure to let me know.

    # Import VMware PowerCLI module
    If (-not (Get-Module -Name 'VMware.PowerCLI')) {
        Import-Module VMware.PowerCLI
    }
     
    # Variable declaration
    $Report = New-Object System.Collections.ArrayList
    $DateTime = $((Get-Date).ToString('yyyy-MM-dd_hh-mm-ss'))
     
    # Set report output path
    $OutputPath = "$env:USERPROFILE\Documents\Reports"
    If ( -Not (Test-Path -Path $OutputPath)) {
        New-Item -ItemType directory -Path $OutputPath
    }
    $Outputfile = "All-ESXi-hosts-$DateTime"
     
    Function MyLogger {
        Param(
            [Parameter(Mandatory = $True)] [String] $Message,
            [Parameter(Mandatory = $False)] [Boolean] $Log = $False
        )
        $TimeStamp = Get-Date -Format "dd-MMM-yyyy HH:mm:ss"
     
        Write-Host -NoNewline -ForegroundColor White "[$timestamp]"
        Write-Host -ForegroundColor Green " $message"
     
        $logMessage = "[$timeStamp] $message"
        If ($Log -eq $True) {
            $logMessage | Out-File -Append -LiteralPath $LogFile
        }
    } #Function MyLogger
     
    Function MyLoggerError {
        Param(
            [Parameter(Mandatory = $True)] [String] $Message,
            [Parameter(Mandatory = $False)] [Boolean] $Log = $False
        )
     
        $TimeStamp = Get-Date -Format "dd-MMM-yyyy HH:mm:ss"
     
        Write-Host -NoNewline -ForegroundColor White "[$timestamp]"
        Write-Host -ForegroundColor Red -BackgroundColor Yellow " $message"
     
        $logMessage = "[$timeStamp] $message"
        If ($Log -eq $True) {
            $logMessage | Out-File -Append -LiteralPath $LogFile
        }
    } #Function MyLoggerError
     
    # Get credentials to use to connect to the vCenter(s)
    Clear-Host
    $Credentials = Get-Credential -Message "Please enter the username and password to use to connect to the vCenter(s)"
     
    # Connect to vCenter(s)
    $ListOfvCenters = (
        "<FQDN/IP address of vCenter #1",
        "vcenter01.yourdomain.local",
        "10.10.10.50"
    )
    Connect-VIServer $ListOfvCenters -Credential $Credentials -ErrorAction Continue
     
    # Get a list of all ESXi hosts running in the vCenter(s)
    $ListOfVMHosts = Get-VMHost | Sort-Object Name
     
    # Get required information for each host
    $Count = $ListOfVMHosts.Count
    $Counter = 1
     
    Foreach ($VMHost in $ListOfVMHosts) {
     
        $TestVMHostConnection = $null
        $TestVMHostConnection = Get-VMHost -Name $VMHost
     
        If ($TestVMHostConnection) {
     
            MyLogger -Message "Processing host $VMHost [$Counter/$Count]"
            #Write-Host "Processing host $VMHost.Name [$Counter/$Count]"
     
            $HostNetwork = $VMHost Get-VMHostNetwork
     
            $HostNetworkvMotionAdapter = $VMHost Get-VMHostNetworkAdapter -VMKernel Where-Object { $_.VMotionEnabled -eq $true }
            $HostNetworkManagementTrafficAdapter = $VMHost Get-VMHostNetworkAdapter -VMKernel Where-Object { $_.ManagementTrafficEnabled -eq $true }
            $HostNetworkvSANTrafficAdapter = $VMHost Get-VMHostNetworkAdapter -VMKernel Where-Object { $_.VsanTrafficEnabled -eq $true }
     
            $MemoryTotalGB = [math]::Round($VMHost.MemoryTotalGB)
     
            $Object = [PSCustomObject]@{
                SerialNumber     = ($VMHost.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo | Where-Object { $_.IdentifierType.Key -eq "ServiceTag" }).IdentifierValue # Serialnumber needs to be first for VLOOKUP to work in Excel
                vCenter          = $VMHost.Uid.Substring($VMHost.Uid.IndexOf('@') + 1).Split(":")[0]
                DataCenter       = ($VMHost | Get-Datacenter)
                Cluster          = $VMHost.Parent
                HostName         = $VMHost.Name
                Manufacturer     = $VMHost.Manufacturer
                Model            = $VMHost.Model
                Bios             = $VMHost.ExtensionData.Hardware.BiosInfo.BiosVersion
                Processor        = $VMHost.ProcessorType
                CPU              = $VMHost.ExtensionData.Hardware.CpuInfo.NumCpuPackages
                CPUCores         = $VMHost.ExtensionData.Hardware.CpuInfo.NumCpuCores
                MemoryGB         = $MemoryTotalGB
                ESXVersion       = $VMHost.ExtensionData.Summary.Config.Product.FullName
                ESXLicenseKey    = $VMHost.LicenseKey
                DNS1             = $HostNetwork.DnsAddress[0]
                DNS2             = $HostNetwork.DnsAddress[1]
                Gateway          = $HostNetwork.VMKernelGateway
                ManagementDevice = $HostNetworkManagementTrafficAdapter.name
                ManagementIP     = $HostNetworkManagementTrafficAdapter.ip
                ManagementMTU    = $HostNetworkManagementTrafficAdapter.mtu
                vMotionDevice    = $HostNetworkvMotionAdapter.name
                vMotionIP        = $HostNetworkvMotionAdapter.ip
                vMotionMTU       = $HostNetworkvMotionAdapter.mtu
                vSANDevice       = $HostNetworkvSANTrafficAdapter.name
                vSANIP           = $HostNetworkvSANTrafficAdapter.ip
                vSANMTU          = $HostNetworkvSANTrafficAdapter.mtu
            }
     
            $Report.add($Object) | Out-Null
        } Else {
            MyLoggerError -Message  "There was an error processing host $VMHost.Name [$Counter/$Count]"
        }
        $Counter++
    }
     
    $Report | Export-Csv "$Outputfile.csv" -NoTypeInformation -UseCulture
     
    $DefaultVIServers | Disconnect-VIServer -Confirm:$false



  • 17.  RE: Get-VMHostHardware |

    Posted Aug 02, 2024 03:51 PM

    Perfect, it works. Thank you




  • 18.  RE: Get-VMHostHardware |

    Posted Aug 02, 2024 08:57 AM
    Edited by TX_Tundra Aug 02, 2024 09:02 AM

    Start the script with this:

    $esxHosts = Get-VMHost | Where { $_.PowerState -eq "PoweredOn" -and $_.ConnectionState -eq "Connected" } | Sort Name

    foreach($esx in $esxHosts) {

    This will give you an accurate list of what is running and connected.



    ~Eric Randall, Solution Architect, Virtual Infrastructure