VMware Cloud Community
G0nz0UK
Enthusiast
Enthusiast

Trying add the number of nics for each host on my Inventory script below

Hello,

I'm simply trying to add the number of nics each host has on this script below and I can't seem to figure it out, would someone kindly advise here?

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
    $esxcli = Get-EsxCli -VMHost $esx -V2
    $platform = $esxcli.hardware.platform.get.Invoke()
    $cpu = $esxcli.hardware.cpu.global.get.Invoke()
    [PSCustomObject]@{
        Cluster = (Get-Cluster -VMHost $esx).Name
        Name = $esx.Name
        Version = $esx.Version
        VendorName = $platform.VendorName
        ProductName = $platform.ProductName
        SerialNumber = $platform.SerialNumber
        PowerState = $esx.PowerState
        Uptime = ((Get-Date) - $esx.ExtensionData.Runtime.BootTime).ToString("dd' days 'hh' hours 'mm' minutes 'ss' seconds'")
        ConnectionState = $esx.ConnectionState
        Status = $esx.State
        HA = $esx.ExtensionData.Summary.Runtime.DasHostState.State
        NoVM = $esx.ExtensionData.VM.Count
        Processor = $esx.ExtensionData.Hardware.CpuPkg[0].Description
        NumCPU = $esx.NumCpu
        ProcSpeedGhz = [math]::Round($esx.ExtensionData.Hardware.CpuInfo.Hz/(1000*1000*1000),1)
        ProcSockets = $cpu.CPUPackages
        ProcCoresPerSocket = $cpu.CPUCores / $cpu.CPUPackages
        HTStatus = if($cpu.HyperthreadingActive){'Active'}else{'Not Active'}
        MemoryGB = $esx.MemoryTotalGB
        NoNic = $esxcli.network.nic.list.Invoke().Count
        BIOSVersion = $esx.ExtensionData.Hardware.BiosInfo.BiosVersion
        BIOSVendor = $esx.ExtensionData.Hardware.BiosInfo.Vendor
        BIOSReleaseDate = $esx.ExtensionData.Hardware.BiosInfo.ReleaseDate
        EVCMode = (Get-Cluster -VMHost $esx).EVCMode
    }
} | Export-Csv C:\vmware\reports\vmhostinfo.csv -NoTypeInformation -UseCulture

Thanks

0 Kudos
7 Replies
LucD
Leadership
Leadership

You mean this line?

$esxcli.network.nic.list.Invoke().Count

Seems to work ok for me


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

0 Kudos
G0nz0UK
Enthusiast
Enthusiast

I'll try and add that shortly thanks! Do you know the one for logical processors, I have the physical number using:

NumCPU = $esx.NumCpu
0 Kudos
G0nz0UK
Enthusiast
Enthusiast

I tried adding that to my script above btw:

 You cannot call a method on a null-valued expression.

At line:8 char:1

+ $esxcli.network.nic.list.Invoke().Count 

 

0 Kudos
LucD
Leadership
Leadership

That looks as if there is nothing in $esxcli.


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

0 Kudos
G0nz0UK
Enthusiast
Enthusiast

I think I'm confusing myself.  How should it look in my config.

I have this added at the top:

 

ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $esx -V2

0 Kudos
LucD
Leadership
Leadership

Provided your code starts with these lines, there should be an object in $esxcli

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
    $esxcli = Get-EsxCli -VMHost $esx -V2


Perhaps there is an ESXi node in a "funny" state in your environment that causes this error.
Perhaps explicitly test with just 1 ESXi node that you know is ok. 

Get-VMHost -Name <MyEsx> -PipelineVariable esx |
ForEach-Object -Process {
    $esxcli = Get-EsxCli -VMHost $esx -V2


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

0 Kudos
G0nz0UK
Enthusiast
Enthusiast

Thanks that worked.

0 Kudos