Find file Copy path
df32c59 Oct 31, 2017
2 contributors

Users who have contributed to this file

@kmruddy @spartyzik
57 lines (50 sloc) 1.75 KB
Finds the local ESXi network Port-ID where a VM is assigned
Reports back a VM's Port-ID according to the local ESXi host. This correlates to the Port-ID which is displayed via ESXTop
Author: Kyle Ruddy, @kmruddy,
The name of the desired VM
PS> .\Get-VMNetworkPortId.ps1 -vm vmname
PS> Get-VM -Name vmname | .\Get-VMNetworkPortId.ps1
Begin {
#Create an array to store output prior to return
$output = @()
Process {
#Loop through each of the input values
foreach ($v in $vm) {
#Validate the input is a valid VM
$vmobj = Get-VM -Name $v -erroraction silentlycontinue
if (!$vmobj) {Write-Verbose "No VM found by the name $v."}
else {
#Create a temporary object to store individual ouput
$tempout = "" | select VM,PortId
#Start an ESXCLI session with the host where the VM resides
$esxcli = Get-EsxCli -VMHost $vmobj.VMHost -v2
#ESXCLI call to obtain information about the VM, specifically its WorldID
$vmNetInfo = $ | ?{$_.Name -eq $vmobj.Name}
#Create spec to poll the host for the network information of the VM
$portArgs = $
$portArgs.worldid = $vmNetInfo.WorldID
#Output the values to the temporary object
$tempout.VM = $vmobj.Name
$tempout.PortId = $$portArgs).PortId
$output += $tempout
End {
return $output