VMware Cloud Community
Sureshadmin
Contributor
Contributor
Jump to solution

Need powershell script to collect ESX host Network Info

hi,

I need two powershell scripts for collecting two types of ESX host network information,

1. vNIC information --> the information about the vmnic(physical nic of ESX hosts) connected to vSwitches.

vNIC | Model | vSwitch | Portgroups | Speed | Status | PCI Location | Active/stand-by/unassigned

Expected output(Example):

vmnic0 | intel corporation 82XXXX gigabit ethernet controller | vSwitch0 | service console, vMotion | 1000mbps Full | up | 01:01.00 | Active

2. Portgroup information

Portgroup | vNIC(s) with active/standby | PCI location of vNIC(s) with active/standby | Physical switch with port number

Expected Output (Example):

Service console | vmnic0(a) : vmnic1(s) | 01:00.00(a) : 02:00.00(s) | abcd-123[GigabitEthernet10/10] : xyzf-7890[Gigabitethernet11/11]

Please note if CDP not enabled, then in values of physical switch field can contain "CDP not enabled"

Thanks in advance!

0 Kudos
71 Replies
Sureshadmin
Contributor
Contributor
Jump to solution

Thank you so much for your quick response. Smiley Happy You have been always so helpful.

0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Luc,

Need you help here. Can you please include 3 more fields in the output of the part 1 script.

1. Observed IP ranges

2. Physical switch Name --> The devID property which gives out the physical switch name where the physical NIC is connected.

3. Physical switch Port -


> The port in physical switch where the physical NIC is connected

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Give this a try

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Working perfect. Thank u so much.

0 Kudos
zxr45
Contributor
Contributor
Jump to solution

Luc

awesome scripts,

I have a little request, could you add a column that shows the VlanID that shows on the CDP to the pnic-report.ps1 script.

Thank you

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, here you go.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
maishsk
Expert
Expert
Jump to solution

Hi Luc,

This does not give any info for a NICS that are part of a dvSwitch

Is that possible?


Maish - VCP - vExpert 2010

VMware Communities User Moderator

Virtualization Architect & Systems Administrator

Twitter

Maish Saidel-Keesing • @maishsk • http://technodrone.blogspot.com • VMTN Moderator • vExpert • Co-author of VMware vSphere Design
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, that is possible.

I'll have to create some new dvSwitch functions first.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
zxr45
Contributor
Contributor
Jump to solution

Luc

Thank you, script worked perfect.

0 Kudos
bradley4681
Expert
Expert
Jump to solution

Luc,

How would you go about dumping this information into a csv file, or report?

Cheers,

Bradley Sessions

If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".

Cheers! If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Hi Bradley,

One way of doing this would be to collect all the entries in an array ($report) and then export that array to a CSV file.

Something like this

$report = @()
foreach($esxImpl in (Get-VMHost)){ 
	$esx = $esxImpl | Get-View
...
		$cdpInfo = $netSys.QueryNetworkHint($pnic.Device)
		$row = "" | Select ESXname,pNic,Model,vSwitch....,VLANID
		$row.ESXname = $esxImpl.Name
		$row.pNic = $pnic.Device
...
		$row.VLANID = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.Vlan}else{"CDP not configured"}}
		$report += $row
	}
}
$report | Export-Csv "C:\pnic-report.csv" -NoTypeInformation

Is the extract clear enough to show how it can be done ?

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
bradley4681
Expert
Expert
Jump to solution

This is what i've got so far but I'm bombing on the vSwitch line

#Here you can list multiple vCenter Servers to get the information from, just put a comma and then the next one in ""
$viservers = "vCenter.domain.net"


#Connects to each vCenter and collects the information below for a report
foreach ($singleViserver in $viservers)
{
	#Passwords with special characters require you to use single quotes, but you can just use them all the time.
	Connect-VIServer $singleViserver -user Username -password 'Password'

$report = @()
foreach($esxImpl in (Get-VMHost)){ 
	$esx = $esxImpl | Get-View
	$netSys = Get-View $esx.ConfigManager.NetworkSystem
	foreach($pnic in $esx.Config.Network.Pnic){
		$vSw = $esxImpl | Get-VirtualSwitch | where {$_.Nic -contains $pNic.Device}
		$pg = $esxImpl | Get-VirtualPortGroup | where {$_.VirtualSwitchName -eq $vSw.Name}
		$order = ($esx.Config.Network.Vswitch | where {$_.Name -eq $vSw.Name}).Spec.Policy.NicTeaming.NicOrder
		$cdpInfo = $netSys.QueryNetworkHint($pnic.Device)
		$row = "" | Select ESXname,pNic,Model,vSwitch,Portgroups,Speed,Status,PCI,ActiveStanByUnassigned,IPrange,pSwitch,PortID,VLANID
			$row.ESXname = $esxImpl.Name
			$row.pNic = $pnic.Device 
			$row.Model = {($esx.Hardware.PciDevice | where {$_.Id -eq $pnic.Pci}).DeviceName},
			$row.vSwitch = $vSw.Name
			$row.Portgroups = {$pg | %{$_.Name}},
			$row.Speed = $pnic.LinkSpeed.SpeedMb
			$row.Status = {if($pnic.LinkSpeed -ne $null){"up"}else{"down"}},
			$row.PCI = $pnic.Pci
			$row.ActiveStanByUnassigned = {if($order.ActiveNic -contains $pnic.Device){"active"}elseif($order.StandByNic -contains $pnic.Device){"standby"}else{"unused"}},
			$row.IPrange = {[string]::Join("/",@($cdpInfo[0].Subnet | %{$_.IpSubnet + "(" + $type + ")"}))},
			$row.pSwitch = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.devId}else{"CDP not configured"}},
			$row.PortID = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.portId}else{"CDP not configured"}},
			$row.VLANID = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.Vlan}else{"CDP not configured"}}
			$report += $row
	}
}

$report | export-csv C:\Scripts\GetNetworkInfo\networkinfo.csv -NoTypeInformation
}

Here's the error

Invalid assignment expression. The left hand side of an assignment operator needs to be something that can be assigned
to like a variable or a property.
At C:\Scripts\GetNetworkInfo\pnic-report.ps1:26 char:18
+             $row.vSwitch = <<<<  $vSw.Name
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : InvalidLeftHandSide

Cheers,

Bradley Sessions

If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".

Cheers! If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Remove the comma at the end of the previous line.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
bradley4681
Expert
Expert
Jump to solution

OK the script runs now but a few of the attributes don't query correctly... This is what gets output in the csv

Model
($esx.Hardware.PciDevice | where {$_.Id -eq $pnic.Pci}).DeviceName

Portgroups
$pg | %{$_.Name}

Status
if($pnic.LinkSpeed -ne $null){"up"}else{"down"}

ActiveStanByUnassigned
if($order.ActiveNic -contains $pnic.Device){"active"}elseif($order.StandByNic -contains $pnic.Device){"standby"}else{"unused"}

IPrange
[string]::Join("/",@($cdpInfo[0].Subnet | %{$_.IpSubnet + "(" + $type + ")"}))

Cheers,

Bradley Sessions

If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".

Cheers! If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are 2 solutions.

You leave out the outer curly braces.

$row.Model = ($esx.Hardware.PciDevice | where {$_.Id -eq $pnic.Pci}).DeviceName

Or you place a '&' before the curly braces.

That way you ask to execute the code block that follows.

Example:

$row.Model = &{($esx.Hardware.PciDevice | where {$_.Id -eq $pnic.Pci}).DeviceName}

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
bradley4681
Expert
Expert
Jump to solution

Well I've managed to get some of them working, still don't have Model, Portgroups, and IPrange working

$report = @()
foreach($esxImpl in (Get-VMHost)){ 
	$esx = $esxImpl | Get-View
	$netSys = Get-View $esx.ConfigManager.NetworkSystem
	foreach($pnic in $esx.Config.Network.Pnic){
		$vSw = $esxImpl | Get-VirtualSwitch | where {$_.Nic -contains $pNic.Device}
		$pg = $esxImpl | Get-VirtualPortGroup | where {$_.VirtualSwitchName -eq $vSw.Name}
		$order = ($esx.Config.Network.Vswitch | where {$_.Name -eq $vSw.Name}).Spec.Policy.NicTeaming.NicOrder
		$cdpInfo = $netSys.QueryNetworkHint($pnic.Device)
		$row = "" | Select ESXname,pNic,Model,vSwitch,Portgroups,Speed,Status,PCI,ActiveStanByUnassigned,IPrange,pSwitch,PortID,VLANID
			$row.ESXname = $esxImpl.Name
			$row.pNic = $pnic.Device 
			$row.Model = {($esx.Hardware.PciDevice | where {$_.Id -eq $pnic.Pci}).DeviceName}
			$row.vSwitch = $vSw.Name
			$row.Portgroups = $pg | %{$_.Name}
			$row.Speed = $pnic.LinkSpeed.SpeedMb
			$row.Status = &{if($pnic.LinkSpeed -ne $null){"up"}else{"down"}}
			$row.PCI = $pnic.Pci
			$row.ActiveStanByUnassigned = &{if($order.ActiveNic -contains $pnic.Device){"active"}elseif($order.StandByNic -contains $pnic.Device){"standby"}else{"unused"}}
			$row.IPrange = {[string]::Join("/",@($cdpInfo[0].Subnet | %{$_.IpSubnet + "(" + $type + ")"}))}
			$row.pSwitch = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.devId}else{"CDP not configured"}}
			$row.PortID = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.portId}else{"CDP not configured"}}
			$row.VLANID = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.Vlan}else{"CDP not configured"}}
			$report += $row
	}
}

$report | export-csv C:\Scripts\GetNetworkInfo\networkinfo.csv -NoTypeInformation
}

Cheers,

Bradley Sessions

If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".

Cheers! If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
bradley4681
Expert
Expert
Jump to solution

Ok I have everything but the portgroups working now.

$report = @()
foreach($esxImpl in (Get-VMHost)){ 
	$esx = $esxImpl | Get-View
	$netSys = Get-View $esx.ConfigManager.NetworkSystem
	foreach($pnic in $esx.Config.Network.Pnic){
		$vSw = $esxImpl | Get-VirtualSwitch | where {$_.Nic -contains $pNic.Device}
		$pg = $esxImpl | Get-VirtualPortGroup | where {$_.VirtualSwitchName -eq $vSw.Name}
		$order = ($esx.Config.Network.Vswitch | where {$_.Name -eq $vSw.Name}).Spec.Policy.NicTeaming.NicOrder
		$cdpInfo = $netSys.QueryNetworkHint($pnic.Device)
		$row = "" | Select ESXname,pNic,Model,vSwitch,Portgroups,Speed,Status,PCI,ActiveStanByUnassigned,IPrange,pSwitch,PortID,VLANID
			$row.ESXname = $esxImpl.Name
			$row.pNic = $pnic.Device 
			$row.Model = &{($esx.Hardware.PciDevice | where {$_.Id -eq $pnic.Pci}).DeviceName}
			$row.vSwitch = $vSw.Name
			$row.Portgroups = $pg | %{$_.Name}
			$row.Speed = $pnic.LinkSpeed.SpeedMb
			$row.Status = &{if($pnic.LinkSpeed -ne $null){"up"}else{"down"}}
			$row.PCI = $pnic.Pci
			$row.ActiveStanByUnassigned = &{if($order.ActiveNic -contains $pnic.Device){"active"}elseif($order.StandByNic -contains $pnic.Device){"standby"}else{"unused"}}
			$row.IPrange = &{[string]::Join("/",@($cdpInfo[0].Subnet | %{$_.IpSubnet + "(" + $type + ")"}))}
			$row.pSwitch = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.devId}else{"CDP not configured"}}
			$row.PortID = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.portId}else{"CDP not configured"}}
			$row.VLANID = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.Vlan}else{"CDP not configured"}}
			$report += $row
	}
}

$report | export-csv C:\Scripts\GetNetworkInfo\networkinfo.csv -NoTypeInformation
}

Also would there be a way for the IPrange to also show the associated VLAN name? How could I also grab the vmkernel and Service console IPs?

Cheers,

Bradley Sessions

If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".

Cheers! If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try this one, note that you need to have CDP enabled for several of the properties.

$report = @()
foreach($esxImpl in (Get-VMHost)){ 
	$esx = $esxImpl | Get-View
	$netSys = Get-View $esx.ConfigManager.NetworkSystem
	foreach($pnic in $esx.Config.Network.Pnic){
		$vSw = $esxImpl | Get-VirtualSwitch | where {$_.Nic -contains $pNic.Device}
		$pg = $esxImpl | Get-VirtualPortGroup | where {$_.VirtualSwitchName -eq $vSw.Name}
		$order = ($esx.Config.Network.Vswitch | where {$_.Name -eq $vSw.Name}).Spec.Policy.NicTeaming.NicOrder
		$cdpInfo = $netSys.QueryNetworkHint($pnic.Device)
		$row = "" | Select ESXname,pNic,Model,vSwitch,Portgroups,Speed,Status,PCI,ActiveStanByUnassigned,IPrange,pSwitch,PortID,VLANID
		$row.ESXname = $esxImpl.Name
		$row.pNic = $pnic.Device 
		$row.Model = ($esx.Hardware.PciDevice | where {$_.Id -eq $pnic.Pci}).DeviceName
		$row.vSwitch = $vSw.Name
		$row.Portgroups = [string]::Join(",", ($pg | %{$_.Name}))
		$row.Speed = $pnic.LinkSpeed.SpeedMb
		$row.Status = &{if($pnic.LinkSpeed -ne $null){"up"}else{"down"}}
		$row.PCI = $pnic.Pci
		$row.ActiveStanByUnassigned = &{if($order.ActiveNic -contains $pnic.Device){"active"}elseif($order.StandByNic -contains $pnic.Device){"standby"}else{"unused"}}
		$row.IPrange = [string]::Join("/",@($cdpInfo[0].Subnet | %{$_.IpSubnet + "(" + $type + ")"}))
		$row.pSwitch = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.devId}else{"CDP not configured"}}
		$row.PortID = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.portId}else{"CDP not configured"}}
		$row.VLANID = &{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.Vlan}else{"CDP not configured"}}
		$report += $row
	}
}
$report | export-csv C:\Scripts\GetNetworkInfo\networkinfo.csv -NoTypeInformation

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
kwharrisit
Contributor
Contributor
Jump to solution

Gents,

Would you mind sharing exactly how youre running these scripts? I have tried copying and pasting the code into VESI and chaging the viserver to my local one and for each of these scripts Im gretted with all kinds of errors.

the first screenshot is my setup and the second are the errors. Im not sure what Im doing wrong here.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like the square bracket monster struck again Smiley Wink

Seriously, the forum SW doesn't like square brackets.

Attached the script. Try it with that one.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos