Hi Luc :smileyhappy:
Here is a PowerCli function I created based on one of your previous posts (thanks) in the community along other peoples posts.
Being new to PowerCli and Powershell, it was a lot of work but also a great learning experience. Hopefully it will be useful to others in the community.
The function does the following:
Takes input piped in from a Get-VM command (1 or multiple VM objects) and maps the path that given VM takes from it's virtual ethernet adapter(s)
and maps the following network path information:
VM Name, VM Virtual Nic, Portgroup, PortGroup VLAN, vSwitch, ESXi Physical NIC(s) attached to vSwitch, ESXi Host, Physical Switch Port(s) via CDP, Physical Switch Port Native VLAN via CDP,Physical Switch Name via CDP, & Observed IP traffic on each ESXi Physical Nic.
Still todo : Build-in some help functionality and error handling.
Command usage example:
Get-VM <VMName> | Get-VMNetPath
Because the Output of the function are Object(s) it can be piped into other formats, scripts or functions.
Example export to csv :
get-vm | Get-VMNetPath | Select VM,VM_Eth,PGVLAN,PortGroup,vSwitch,Host,pNic,pNic_Subs,Switch_NVLAN,Switch_Port,Switch,Switch_IP | export-csv c:\VM_Network_Path.csv -NoTypeInformation
## Begin Code ##
Function Get-VMNetPath {
#Ref: Luc D Posting,
#Configured as Function,Added Parameters,Pipeline,Output as Object, & Optimization
#Added VM vitural Nic , Added CDP info (Physical Switch info)
[CmdletBinding()]
param(
[Parameter(Mandatory=$False,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage="Output from Get-VM")]
[Alias('VM')]
[Object[]]$VMs
)
BEGIN
{
#Create Empty Collection for results of Get-View ESX
$ESX_Views = @()
}
PROCESS
{
ForEach($VM in $VMs)
{
#Define VM Vars
[String]$VM_Name =''
[String]$VM_pNic =''
[String]$VM_vSwitch =''
[String]$VM_PortGroup =''
[String]$VM_Host =''
[String]$VM_VLAN =''
[String]$VM_Eth =''
$VM | `
%{Get-View $_.ID} | `
%{
$VM_Name = $_.Name
$ESX_Name = $_.Runtime.Host.Value
#Colletion of VM Virtual Ethernet Cards inc. VM.Name,VM_Eth,PortGroup of VM_Eth
$VM_Eths = @()
$VM_Eths = ($_.config.hardware.device | where {$_ -is [VMware.Vim.VirtualEthernetCard]}).deviceInfo | Select @{n='VM';exp={$VM.Name} },@{n="VM_Eth";exp={$_.Label}},@{n="PortGroup";exp={$_.Summary}}
# Check if Get-View of Host is Cached in (Collection) $ESX_Views
# If Cached, use Cached, if not Get-View of Host and add to $ESX_Views Collection
if ( ($ESX_Views | Where {$_.Config.Host.Value -eq $ESX_Name}) -eq $null )
{
#Add View to $ESX_Views Collection
$ESX_Views += Get-View -ID $_.Runtime.Host
}
$esx = $ESX_Views | Where {$_.Config.Host.Value -eq $ESX_Name}
#Collect Physical Network CDP info from ESXi Host
$esx_Net_View = Get-View $esx.ConfigManager.NetworkSystem
$esx_CDP = @()
foreach($PhysNic in $esx_Net_View.NetworkInfo.Pnic)
{
$PnicInfo = $esx_Net_View.QueryNetworkHint($physnic.Device)
foreach($Hint in $PnicInfo)
{
$ObservIPs = @()
foreach($subnet in $hint.subnet)
{
$ObservIPs += $subnet.IPsubnet
}
if( ($hint.ConnectedSwitchPort) )
{
#Put CDP info into a Collection for matching against pNIC
#$hint.Subnet #test
$esx_CDP += ('' | Select @{n='HostName'; exp={$Esx.Name} }, @{n='pNic';exp = {$PhysNic.Device} } , `
@{n='Switch';exp={$Hint.ConnectedSwitchPort.Devid} } , @{n='Switch_IP' ;exp={$Hint.ConnectedSwitchPort.Address} } , `
@{n='Switch_Port';exp={$Hint.ConnectedSwitchPort.PortId} } , @{n='Switch_NVLAN';exp= {$Hint.ConnectedSwitchPort.Vlan} } , `
@{n='ObservIPs'; exp={$ObservIPs}} )
}
Else
{
$esx_CDP += ('' | Select @{n='HostName'; exp={$Esx.Name} }, @{n='pNic';exp = {$PhysNic.Device} } , `
@{n='Switch';exp={"N/A"} }, @{n='Switch_IP' ;exp={"N/A"} } , `
@{n='Switch_Port';exp={"N/A"} } , @{n='Switch_NVLAN';exp= {"N/A"} } , `
@{n='ObservIPs'; exp={$ObservIPs}} )
}
}
}
foreach($nicImpl in $_.Network)
{
$nic = Get-View $nicImpl
$VM_PortGroup=$nic.Name
$VM_Host = $esx.Name
foreach($hnet in $esx.Config.Network.Portgroup)
{
if($hnet.Spec.Name -eq $nic.Name)
{
$VM_VLAN = $hnet.Spec.VlanId
$VM_vSwitch = $hnet.Spec.VswitchName
if($hnet.ComputedPolicy.NicTeaming.NicOrder.ActiveNic -is [array])
{
foreach($pnic in $hnet.ComputedPolicy.NicTeaming.NicOrder.ActiveNic)
{
#Define additional Vars for Object.
$VM_pNic = $pnic
$VM_Eth = ($VM_Eths | Where {$_.PortGroup -eq $VM_PortGroup}).VM_Eth
$Switch = ($esx_CDP | Where {$_.pNic -eq $VM_pNic}).Switch
$Switch_IP = ($esx_CDP | Where {$_.pNic -eq $VM_pNic}).Switch_IP
$Switch_Port = ($esx_CDP | Where {$_.pNic -eq $VM_pNic}).Switch_Port
$Switch_NVLAN = ($esx_CDP | Where {$_.pNic -eq $VM_pNic}).Switch_NVLAN
$ObservIPs = ($esx_CDP | Where {$_.pNic -eq $VM_pNic}).ObservIPs
$props = @{'Host'= $VM_Host;
'pNic'= $VM_pNic;
'vSwitch' = $VM_vSwitch;
'PortGroup' = $VM_PortGroup;
'VM' = $VM_Name;
'PGVLAN' = $VM_VLAN;
'VM_Eth' = $VM_Eth;
'Switch' = $Switch;
'Switch_IP' = $Switch_IP;
'Switch_Port' = $Switch_Port;
'Switch_NVLAN'= $Switch_NVLAN;
'pNic_Subs'= $ObservIPs}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj
}
}
}
}
}
}
}
}
END{}
}
## End Code ##