Highlighted
Enthusiast
Enthusiast

PowerCLI equivalent of net-stats -l in ESXi shell

Is there a way to get the "PortNum" value using PowerCLI? I'm trying to run $esxcli.network.port.stats.get.Invoke() to get the stats, using the "PortNum" value only for vmnic for multiple hosts.

Snag_1a62914f.png

Snag_1a676015.png

0 Kudos
6 Replies
Highlighted
Enthusiast
Enthusiast

I've just managed to get the port stats by manually entering the PortID, using $esxcli.network.port.stats.get.Invoke(@{portid = '33554434'}).

Snag_1ab74610.png

But as I mentioned, I'd like to be able to focus only on PortIDs of vmnics.

0 Kudos
Highlighted
User Moderator
User Moderator

It is not a full replication of the net-stats command, but besides the fact that you don't have the  PortNum, there is no statistical data for the vmnic afaik.

Try with one of the PortNum returned by net-stats for one of the vmnic.

You can get the statistics for the VMKernel and the VM connected.

That can be done something like this

$esxName = 'MyEsx'

$esxcli = Get-EsxCli -VMHost $esxName -V2

$esxcli.network.ip.interface.list.Invoke() |

ForEach-Object -Process {

    $vmk = $_

    $esxcli.network.port.stats.get.Invoke(@{portid=$vmk.PortID}) |

    Add-Member -MemberType NoteProperty -Name ClientName -Value $vmk.Name -PassThru |

    Add-Member -MemberType NoteProperty -Name SwitchName -Value $vmk.VDSName -PassThru |

    Add-Member -MemberType NoteProperty -Name PortNum -Value $vmk.PortID -PassThru

}

$esxcli.network.vm.list.Invoke() |

ForEach-Object -Process {

    $vm = $_

    $filter = $esxcli.network.vm.port.list.Invoke(@{worldid=$vm.WorldID})

    $esxcli.network.port.stats.get.Invoke(@{portid=$filter.PortID}) |

    Add-Member -MemberType NoteProperty -Name ClientName -Value $vm.Name -PassThru |

    Add-Member -MemberType NoteProperty -Name SwitchName -Value $filter.vSwitch -PassThru |

    Add-Member -MemberType NoteProperty -Name PortNum -Value $filter.PortID -PassThru

}


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

Highlighted
Enthusiast
Enthusiast

@LucD 

With one of your scripts using Posh-SSH, I've been able to get the PortID of vmnics

 

$esxName = 'MyEsx'
$esx = Get-VMHost -Name $esxName
$esxcli = Get-EsxCli -VMHost $esx -V2

$cmdsub = @'
net-stats  -l | grep -i vmnic | awk '{print $1}';
'@
$secPswd = ConvertTo-SecureString 'S@curePass!' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('root', $secPswd)

$session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey
$PortIDs = Invoke-SSHCommand -SSHSession $session -Command $cmdSub | Select -ExpandProperty Output
Foreach ($portid in $PortIDs){
    $esxcli.network.port.stats.get.Invoke(@{portid = "$PortID"})
}
Remove-SSHSession -SSHSession $session | Out-Null

 

Snag_1431144d.png

It still lacks a little bit of info such as the hostname and the VMNIC. Would you be able to help me optimize the above snippet to run against multiple hosts, therefore a group of hosts in a cluster? It'd be useful to have the script report the vmnic stats that are online/UP from all the hosts in the cluster.

0 Kudos
Highlighted
User Moderator
User Moderator

I'm not sure how you got that result, for me that is not working.
I'm on an ESXi v7 node.

port-stats.png


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

0 Kudos
Highlighted
Enthusiast
Enthusiast

May be it makes a difference with the ESXi version. But does this make sense to you?

Snag_14902b71.png

0 Kudos
Highlighted
User Moderator
User Moderator

I can only do that with the portId of a VM.


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

0 Kudos