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
LucD
Leadership
Leadership
Jump to solution

The last script was for Suresh, the one before is for you.

Notice to who I replied.

It becomes difficult when we have 2 threads in 1 Smiley Wink

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
kwharrisit
Contributor
Contributor
Jump to solution

gotcha!! yeah tough thing when one like me threadjacks (sorry suresh) but i figured he had such a good idea to begin with, I just wanted a little tweak here or there. Smiley Wink

I see what you mean about the vlan clutter, Im not sure how Id like it represented . I know I want to have a list so that I can verify the ones that show up and those that dont, but im open to suggestions on how to clean up the display. !Untitled picture.png|thumbnail=true!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try this version.

At least the CSV is a bit more readable.

Not happy yet with the IP subnets though.

function Get-pNICInfo{
	param($name, $type)

	$cdpInfo = $netSys.QueryNetworkHint($name)
	$row = "" | Select "ESXname","Portgroup","VLANid","IP range + VLAN","pNIC","PCI location","Physical switch"
	$row."ESXname" = $esxImpl.Name
	$row."Portgroup" = $pg.Spec.Name
	$row."VLANid" = $pg.Spec.VlanId
	$row."IP range + VLAN" = [string]::Join("/",($cdpInfo[0].Subnet | %{$_.IpSubnet + ' (VLAN ' + $_.VlanId + ')(' + $type + ')'}))
	$row."pNIC" = $name + "(" + $type + ")"
	$row."PCI location"=$esx.Config.Network.Pnic | where {$_.Device -eq $name} | %{$_.Pci + "(" + $type + ")"}
	$row."Physical switch"=&{if($cdpInfo[0].connectedSwitchPort){$cdpInfo[0].connectedSwitchPort.devId + "(" + $type + ")"}else{"CDP not configured(" + $type + ")"}}
	$row
}

$report = @()
Get-VMHost | %{ 
	$esxImpl = $_
	$esx = $esxImpl | Get-View
	$netSys = Get-View $esx.ConfigManager.NetworkSystem
	foreach($pg in $esx.Config.Network.Portgroup){
		foreach($a in $pg.ComputedPolicy.NicTeaming.NicOrder.ActiveNic){
			if($a){
				$report += (Get-pNICInfo $a "a")
			}
		}
		foreach($s in $pg.ComputedPolicy.NicTeaming.NicOrder.StandbyNic){
			if($s){
				$report += (Get-pNICInfo $s "s")
			}
		}
	}
}
$report | Export-Csv "C:\Nic-Info.csv" -NoTypeInformation -UseCulture

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
kwharrisit
Contributor
Contributor
Jump to solution

OK, so I tried this one, and good thing I was running the command line out of VESI, as the errors on the CDP info lit up my screen. the resulting csv file is now completely blank in the observed IP column.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange, ran this from the PowerCLI prompt and no errors what so ever.

Could you perhaps show one or two of the errors ?

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
kwharrisit
Contributor
Contributor
Jump to solution

so Ive got what might be a silly question\suggestion - is there a way to stick the IP range and VLAN into seperate columns? If you coudl do that, then maybe we can mask the IP clumn if we want. in our environment almost everything is trunked, so its the VLAN tags that are most important, but being able to seperate and switch between the two might help clean up the display... what do you think?

0 Kudos
kwharrisit
Contributor
Contributor
Jump to solution

here you go

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There we go again Smiley Sad

The forum SW doesn't like square brackets.

I'll attach the script.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

And this one has "IP range" and "IP range VLAN" split out.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
kwharrisit
Contributor
Contributor
Jump to solution

Luc,

MUCH better!! in fact I just ran this across a big cluster and already see some missing VLANS Smiley Happy this has been the only way to check if all the networking is right aside from when you hook a mahcine up and nothing works.

Thank you thank you thank you

0 Kudos
kwharrisit
Contributor
Contributor
Jump to solution

Luc,

One more things, since CDP displays the switch it is connected to , is it possible to alos pull the port number for the nic? I think this script is turnign into a near perfect netowkr auditing script .

0 Kudos
kwharrisit
Contributor
Contributor
Jump to solution

Luc,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Here you go.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Luc,

Thanks for this awesome script. Now the script is perfect and very close to the expected ouput which i asked for when i opened this thread.

Just need port number to be added,

Presently the ouput of physical switch value is like this,

Physical switch : {abcd-1234.xxxx.com(a), zyzf-1234.xxxx.com(s)}

Expected output(example)

Physical switch : {abcd-1234.xxxx.com(a):GigabitEthernet1/1(a), zyzf-1234.xxxx.com(s):GigabitEthernet2/2(s)}

Given correct and helpful aswers

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks. The following will include the portId.

foreach($esxImpl in (Get-VMHost)){ 
	$esx = $esxImpl | Get-View
	$netSys = Get-View $esx.ConfigManager.NetworkSystem
	foreach($pg in $esx.Config.Network.Portgroup){
		$pNICStr = @()
		$pciStr = @()
		$cdpStr = @()
		foreach($a in $pg.ComputedPolicy.NicTeaming.NicOrder.ActiveNic){
			if($a){
				$pNICStr += ($a + "(a)")
				$pciStr += ($esx.Config.Network.Pnic | where {$_.Device -eq $a} | %{$_.Pci + "(a)"})
				$cdpInfo = $netSys.QueryNetworkHint($a)
				$cdpStr += &{if($cdpInfo[0].connectedSwitchPort){
								$cdpInfo[0].connectedSwitchPort.devId + "(a):" + $cdpInfo[0].connectedSwitchPort.PortId + "(a)"
							}
							else{"CDP not configured(a)"}}
			}
		}
		foreach($s in $pg.ComputedPolicy.NicTeaming.NicOrder.StandbyNic){
			if($s){
				$pNICStr += ($s + "(s)")
				$pciStr += ($esx.Config.Network.Pnic | where {$_.Device -eq $s} | %{$_.Pci + "(s)"})
				$cdpInfo = $netSys.QueryNetworkHint($s)
				$cdpStr += &{if($cdpInfo[0].connectedSwitchPort){
								$cdpInfo[0].connectedSwitchPort.devId + "(s):" + $cdpInfo[0].connectedSwitchPort.PortId + "(s)"
							}
							else{"CDP not configured(s)"}}
			}
		}

		$pg | Select @{N="ESXname";E={$esxImpl.Name}},
		@{N="Portgroup";E={$pg.Spec.Name}},
		@{N="VLANid";E={$pg.Spec.VlanId}},
		@{N="pNIC";E={$pNICStr}},
		@{N="PCI location";E={$pciStr}},
		@{N="Physical switch";E={$cdpStr}}
	}
}

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Perfect. That's it. I got what i want. Thanks a lot for your patience in hearing all my queries and providing this wonderful script.

Have two more script requirement Smiley Wink one for storage and other for VM inventory, which i will post soon, need your script for those.

0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Luc,

I have posted a new ESX Storage powershell script requirement in this link http://communities.vmware.com/thread/263312 Please check whether you have a script for this.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'll have a look.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Luc,

Sorry to ask you a addtion to this script again. Can you please add a vSwitch field to the output. At present from the output i can't know in which vSwitch the portgroup exist. So the expected output looks like this,

ESXname :

Portgroup :

vSwitch :

VLANid :

pNIC :

PCI location :

Physical switch :

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Here you go

foreach($esxImpl in (Get-VMHost)){ 
	$esx = $esxImpl | Get-View
	$netSys = Get-View $esx.ConfigManager.NetworkSystem
	foreach($pg in $esx.Config.Network.Portgroup){
		$pNICStr = @()
		$pciStr = @()
		$cdpStr = @()
		foreach($a in $pg.ComputedPolicy.NicTeaming.NicOrder.ActiveNic){
			if($a){
				$pNICStr += ($a + "(a)")
				$pciStr += ($esx.Config.Network.Pnic | where {$_.Device -eq $a} | %{$_.Pci + "(a)"})
				$cdpInfo = $netSys.QueryNetworkHint($a)
				$cdpStr += &{if($cdpInfo[0].connectedSwitchPort){
								$cdpInfo[0].connectedSwitchPort.devId + "(a):" + $cdpInfo[0].connectedSwitchPort.PortId + "(a)"
							}
							else{"CDP not configured(a)"}}
			}
		}
		foreach($s in $pg.ComputedPolicy.NicTeaming.NicOrder.StandbyNic){
			if($s){
				$pNICStr += ($s + "(s)")
				$pciStr += ($esx.Config.Network.Pnic | where {$_.Device -eq $s} | %{$_.Pci + "(s)"})
				$cdpInfo = $netSys.QueryNetworkHint($s)
				$cdpStr += &{if($cdpInfo[0].connectedSwitchPort){
								$cdpInfo[0].connectedSwitchPort.devId + "(s):" + $cdpInfo[0].connectedSwitchPort.PortId + "(s)"
							}
							else{"CDP not configured(s)"}}
			}
		}

		$pg | Select @{N="ESXname";E={$esxImpl.Name}},
		@{N="vSwitch";E={($esx.Config.Network.Vswitch | where {$_.Key -eq $pg.Vswitch}).Name}},
		@{N="Portgroup";E={$pg.Spec.Name}},
		@{N="VLANid";E={$pg.Spec.VlanId}},
		@{N="pNIC";E={$pNICStr}},
		@{N="PCI location";E={$pciStr}},
		@{N="Physical switch";E={$cdpStr}}
	}
}

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos