PowerCLI

 View Only
Expand all | Collapse all

Get-Hardware details

  • 1.  Get-Hardware details

    Posted Oct 29, 2024 10:03 AM

    Hello, I am having trouble with the function described below. Some details are not being returned correctly. Additionally, when I use -V2, the serial number information is no longer retrieved. Can you please assist me?

    function Get-ESXiHostInventory {
        param (
            [Parameter(Mandatory = $true)]
            [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$vmHost
        )

        # Initialisation des collections de données
        $hardwareInfo = @{}
        $serialNumber = "Not Retrieved"

        # Récupération des informations matérielles de base via Get-View
        try {
            $hostView = $vmHost | Get-View
            $hardwareInfo = @{
                Hostname         = $vmHost.Name
                ConnectionState  = $vmHost.ConnectionState
                PowerState       = $vmHost.PowerState
                CpuTotalGHz      = [math]::round($vmHost.CpuTotalMhz / 1000, 2)
                CpuUsageMHz      = $vmHost.CpuUsageMhz
                CpuCount         = $vmHost.NumCpu
                TotalCPUCores    = $hostView.Hardware.CpuInfo.NumCpuCores
                MemoryTotalGB    = [math]::round($vmHost.MemoryTotalGB, 2)
                MemoryUsageGB    = [math]::round($vmHost.MemoryUsageGB, 2)
                Manufacturer     = $vmHost.Manufacturer
                Model            = $vmHost.Model
                LicenseKey       = $vmHost.LicenseKey
                ProcessorType    = $vmHost.ProcessorType
                IsStandalone     = $vmHost.IsStandalone
                DatacenterName   = ($vmHost | Get-Datacenter).Name
                ClusterName      = ($vmHost | Get-Cluster).Name
            }

            # Tentative de récupération du numéro de série via Get-View
            $serialNumber = $hostView.Hardware.SystemInfo.SerialNumber
            if (-not [string]::IsNullOrEmpty($serialNumber)) {
                $hardwareInfo["SerialNumber"] = $serialNumber
            } else {
                $hardwareInfo["SerialNumber"] = "Not Retrieved"
            }
        }
        catch {
            Write-Log -Message "Failed to retrieve data via Get-View for host $($vmHost.Name): $($_.Exception.Message)" -Level "Error"
        }

        # Si le numéro de série n'a pas été récupéré, utiliser ESXCLI sans -V2
        if ($hardwareInfo["SerialNumber"] -eq "Not Retrieved") {
            try {
                $esxcli = Get-EsxCli -VMHost $vmHost
                $serialNumberCLI = $esxcli.hardware.platform.get().SerialNumber
                if (-not [string]::IsNullOrEmpty($serialNumberCLI)) {
                    $hardwareInfo["SerialNumber"] = $serialNumberCLI
                }
            }
            catch {
                Write-Log -Message "Failed to retrieve Serial Number via ESXCLI for host $($vmHost.Name): $($_.Exception.Message)" -Level "Warning"
            }
        }

        return [PSCustomObject]$hardwareInfo
    }


  • 2.  RE: Get-Hardware details

    Posted Oct 29, 2024 11:06 AM

    Which properties are not returned correctly?

    Whether the serial number is returned depends on the HW on which the ESXi node is running.
    Some manufacturers/models do not provide that info in the correct location.



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


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


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



  • 3.  RE: Get-Hardware details

    Posted Oct 29, 2024 11:17 AM

    Without the -V2 option, all serial numbers are retrieved without any issues. Once I add the -V2, I am no longer able to retrieve all serial numbers, except for a few, even though they are all from the same manufacturer and have the same ESXi version.




  • 4.  RE: Get-Hardware details

    Posted Oct 30, 2024 05:30 AM

    @LucD

    Shall I change the approach to get the needed inventory?




  • 5.  RE: Get-Hardware details

    Posted Oct 30, 2024 05:36 AM

    If your approach to getting the serial number without using the V2 switch works, I would stick with that solution.
    Your code by trying both methods is a good idea



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


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


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



  • 6.  RE: Get-Hardware details

    Posted Oct 30, 2024 05:47 AM

    Thank you very much. Indeed, with this function, the series numbers are well-recovered. However, I am experiencing an issue with the number of CPU counts. The numbers "CPUCount" and the CPUTotalGHz are not being recovered. I cannot understand why.




  • 7.  RE: Get-Hardware details
    Best Answer

    Posted Oct 30, 2024 06:21 AM

    Can you try with

                CpuTotalGHz      = [math]::round(($hostView.Hardware.CpuInfo.NumCpuCores * $hostView.Hardware.CpuInfo.Hz / 1e9), 2)
                CpuUsageMHz      = [math]::round(($hostView.Hardware.CpuInfo.NumCpuCores * $hostView.Hardware.CpuInfo.Hz / 1e6), 2)
                CpuCount         = $hostView.Hardware.CpuInfo.NumCpuPackages
    


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


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


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



  • 8.  RE: Get-Hardware details

    Posted Oct 30, 2024 06:40 AM

    @LucD

    Thank you again, with your help I'm able to get all the details.
    Just a quick one, shall also I update the function that exports the data?

    function Get-ESXiHostInventory {
        param (
            [Parameter(Mandatory = $true)]
            [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$vmHost
        )

        # Initialize hardware information variables and set default values if not retrieved successfully from the host system or ESXCLI commands (if -V2 is not available)
        $hardwareInfo = @{ }
        $serialNumber = "Not Retrieved"

        # Get basic hardware information via Get-View (vSphere API) for ESXi hosts (vCenter Server required)
        try {
            $hostView = $vmHost | Get-View
            $totalCpuCores = $hostView.Hardware.CpuInfo.NumCpuCores

            $hardwareInfo = @{
                Hostname         = $vmHost.Name
                ConnectionState  = $vmHost.ConnectionState
                PowerState       = $vmHost.PowerState
                CpuTotalGHz      = [math]::round(($hostView.Hardware.CpuInfo.NumCpuCores * $hostView.Hardware.CpuInfo.Hz / 1e9), 2)
                CpuUsageGHz      = [math]::round(($hostView.Hardware.CpuInfo.NumCpuCores * $hostView.Hardware.CpuInfo.Hz / 1e6), 2)
                CpuCount         = $hostView.Hardware.CpuInfo.NumCpuPackages
                TotalCPUCores    = $totalCpuCores
                MemoryTotalGB    = [math]::round($vmHost.MemoryTotalGB, 2)
                MemoryUsageGB    = [math]::round($vmHost.MemoryUsageGB, 2)
                Manufacturer     = $vmHost.Manufacturer
                Model            = $vmHost.Model
                LicenseKey       = $vmHost.LicenseKey
                ProcessorType    = $vmHost.ProcessorType
                IsStandalone     = $vmHost.IsStandalone
                DatacenterName   = ($vmHost | Get-Datacenter).Name
                ClusterName      = ($vmHost | Get-Cluster).Name
            }

            # Try to retrieve the serial number via Get-View
            $serialNumber = $hostView.Hardware.SystemInfo.SerialNumber
            if (-not [string]::IsNullOrEmpty($serialNumber)) {
                $hardwareInfo["SerialNumber"] = $serialNumber
            } else {
                $hardwareInfo["SerialNumber"] = "Not Retrieved"
            }
        }
        catch {
            Write-Log -Message "Failed to retrieve data via Get-View for host $($vmHost.Name): $($_.Exception.Message)" -Level "Error"
        }

        # If the serial number was not retrieved, try to get it via ESXCLI without -V2
        if ($hardwareInfo["SerialNumber"] -eq "Not Retrieved") {
            try {
                $esxcli = Get-EsxCli -VMHost $vmHost
                $serialNumberCLI = $esxcli.hardware.platform.get().SerialNumber
                if (-not [string]::IsNullOrEmpty($serialNumberCLI)) {
                    $hardwareInfo["SerialNumber"] = $serialNumberCLI
                }
            }
            catch {
                Write-Log -Message "Failed to retrieve Serial Number via ESXCLI for host $($vmHost.Name): $($_.Exception.Message)" -Level "Warning"
            }
        }

        return [PSCustomObject]$hardwareInfo
    }
    # Function to gather and store ESXi host data
    function Get-ESXiHostData {
        param (
            [Parameter(Mandatory = $true)]
            [string]$vCenterName,
            [Parameter(Mandatory = $true)]
            [string]$vCenterVersion,
            [Parameter(Mandatory = $true)]
            [string]$vCenterBuild
        )

        # Initialize data collection array
        $data = @()

        # Get list of connected hosts
        $hosts = Get-VMHost | Where-Object { $_.ConnectionState -eq "Connected" }

        foreach ($vmHost in $hosts) {
            # Retrieve hardware and configuration information
            $hardwareInfo = Get-ESXiHostInventory -vmHost $vmHost

            # Append data to the collection
            $data += [PSCustomObject]@{
                vCenterName      = $vCenterName
                Version          = $vCenterVersion
                Build            = $vCenterBuild
                DatacenterName   = $hardwareInfo.DatacenterName
                ClusterName      = $hardwareInfo.ClusterName
                ESXiHostname     = $hardwareInfo.Hostname
                ConnectionState  = $hardwareInfo.ConnectionState
                Manufacturer     = $hardwareInfo.Manufacturer
                Model            = $hardwareInfo.Model
                SerialNumber     = $hardwareInfo.SerialNumber
                CPUCount         = $hardwareInfo.CPUCount
                CpuTotalGHz      = $hardwareInfo.CpuTotalGHz
                CpuUsageGHz      = $hardwareInfo.CpuUsageGHz
                MemoryTotalGB    = $hardwareInfo.MemoryTotalGB
                MemoryUsageGB    = $hardwareInfo.MemoryUsageGB
                PowerState       = $hardwareInfo.PowerState
                ProcessorType    = $hardwareInfo.ProcessorType
                LicenseKey       = $hardwareInfo.LicenseKey
                IsStandalone     = $hardwareInfo.IsStandalone
            }
        }

        return $data
    }



  • 9.  RE: Get-Hardware details

    Posted Oct 30, 2024 06:50 AM

    Since that function only uses, not calculates, the data returned by the Get-ESXiHostInventory function, there should be no need to change it.



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


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


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



  • 10.  RE: Get-Hardware details

    Posted Oct 30, 2024 06:56 AM

    You are correct; I have tested it and it's fine. Thank you very much for your assistance.




  • 11.  RE: Get-Hardware details

    Posted Oct 31, 2024 08:02 AM

    Maybe you get an array of info back? So you can investigate by using $a.type()




  • 12.  RE: Get-Hardware details

    Posted Oct 31, 2024 08:20 AM

    I will test this option thank you ;)