Skip to content

Finding a VM’s Port-ID with PowerCLI

A question was recently brought up over finding a VM’s Port-ID with PowerCLI.

Right off the bat, I assumed the ask was for the distributed switch port. That wasn’t the case though. In this situation, the ask was to identify the Port-ID that was being displayed in ESXTOP via the Network view.

ESXTOP Network View Example

The Port-ID that’s being displayed is local to the host. That information isn’t available through the VM object, at least as far as I could find. In order to get the Port-ID through PowerCLI, we have to get a little crafty. This is going to require some ESXCLI usage.

Since we’re dealing with networking, we can automatically jump to the ESXCLI Network documentation on VMware Code. Luckily, there’s really only two commands that deal with VMs and we’re going to need both of them:

  • network vm list
  • network vm port list

We’ll use the network vm list command to obtain the VM’s World-ID and then use that as an input to the network vm port list command to receive back the local Port-ID for the VM!

Since the goal was to do this in PowerCLI, I created a script and added it to the PowerCLI Community Repo. The script is titled: Get-VMNetworkPortId.ps1

It can be used in a couple different ways, so use it however makes sense to you.

Example Usage:
Get-VMNetworkPortId Usage

After feeding in a VM by pipeline, making use of the VM parameter, and even passing multiple VMs to the script, we can see how the Port-ID does in fact match up with what’s displayed in the ESXTOP example from above.

The script is available on GitHub and looks like the following:

Published inPowershellVMware

Be First to Comment

Leave a Reply