Wednesday, December 30, 2015

Save complete virtual PortGroup information Settings - Powercli

This script I have written to pull information from all the Virtual PortGroup on esxi/vcenter and data is saved in a single CSV file, You can use it as a backup also, It is very helpful when modifying, analysing Portgroup configuration, If you take the portgroup backup with this script frequently you can even tell what are the changes done last time or compare those settings. This has saved my lots of time while troubleshooting my Virtual Standard Switch networking.

Earlier script for collecting networking information
PowerCLI esxi host Physical nic info in nice table format 

Find CDP or LLDP information on Windows Servers


Below are the settings captured and stored in CSV file.
  • Esxi server name
  • PortGroup Name, This will be per esxi server not the common one
  • Vlan Id configured on PortGroup
  • vSwitch name Portgroup resides
  • PortGroup Type (vmkernel or Virtual Machines)
  • Mac Address (If portgroup is virtual machine, Mac address of all virtual machines, else mac address of vmkernel portgroup)
  • PortGroup Security Settings  (Promiscuous mode, Mac address Changes, forged transmits)
  • NicTeaming Settings (Whether they are inherited from vSwitch)
  • Notify Switches
  • Failback, order
  • Active Nics, Standby Nics, Unused Nics
  • Virtual machines count on the portgroup and their name, Ip address,
  • PortGroup Binding compliance
  • MTU Settings
  • Management, vMotion, Ft, vSan enabled for vmkernel portgroup
This is the first screenshot when I am running script, It will collect all the virtual portgroup on the esxi in vCenter and then will collect and store information from each PortGroup in CSV file.
This is the second screenshot I want to show how the data will look like on the console. Some of the information will be blank depending on if portgroup is VMKernel or virtual machines.
Copy paste below code in text file and rename extension to .ps1
 <#  
 .SYNOPSIS  
  Gets the complete virtual portgroup information  
 .DESCRIPTION  
  This script creates a report of complete virtual portGroup information  
  of the vm’s guest OS  
 .NOTES  
  Find more information on http://kunaludapi.blogspot.com  
  Make sure you have installed Powercli and can connect to vcenter.  
 .EXAMPLE  
  PS C:\Temp> .\Get-VirtualPortGroupInfo.ps1  
 .EXAMPLE  
  PS C:\Temp> .\Get-VirtualPortGroupInfo.ps1 | Export-Csv -NoTypeInformation -Path C:\Temp\NetworkInformation.csv  
 #>  
   
  #####################################    
  ## http://kunaludapi.blogspot.com    
  ## Version: 1    
  ##    
  ## Tested this script on    
  ## 1) Powershell v4    
  ## 2) VMware vSphere PowerCLI 6.0 Release 1 build 2548067   
  ## 3) Vsphere 5.5    
  #####################################    
 Begin {  
 if (-not(Get-PSSnapin vmware.vimautomation.core -ErrorAction SilentlyContinue)) {  
   Add-PSSnapin vmware.vimautomation.core -ErrorAction SilentlyContinue  
 }  
 $report = @()  
 $IPregex=‘(?<Address>((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))’  
 $VMHosts = Get-VMHost  
 } #begin  
 Process {  
   foreach ($esxi in $VMHosts) {  
   Write-Host "Collecting PortGroups from Esxi Server `"$($esxi.Name)`"" -BackgroundColor DarkGreen  
   
   #All PortGroups in esxi server  
   $PortGroup = $esxi | Get-VirtualPortGroup  
     
   #iSCSI software adapter on esxi  
   $iSCSIHBA = $esxi | Get-VMHostHba -Device $HBA -Type iSCSI | Where-Object {$_.Model -eq "iSCSI Software Adapter"}  
   
   #All VMKernel Ports on esxi Server  
   $VMkernelPorts = $esxi | Get-VMHostNetworkAdapter -Name vmk*  
   
   #Esxicli information of esxi server  
   $esxcli = $esxi | Get-EsxCli  
   
     foreach ($Group in $PortGroup) {  
       Write-Host "`tFetching Info from Portgroup `"$($Group.Name)`"" -ForegroundColor Yellow  
         
       $Obj = New-Object psobject  
       $Obj | Add-Member -Name EsxiName -MemberType NoteProperty -Value $esxi.Name  
       $Obj | Add-Member -Name PortGroup -MemberType NoteProperty -Value $Group.Name  
       $Obj | Add-Member -Name VLanId -MemberType NoteProperty -Value $Group.VLanId  
       $Obj | Add-Member -Name vSwitch -MemberType NoteProperty -Value $Group.VirtualSwitchName  
         
       #Find whether PortGroup is VMKernel or VirtualMachine  
       if ($VMkernelPorts.PortGroupName -contains $group.Name) {  
         $Portgrouptype = "VMKernel"  
       } #if ($VMkernelPorts.PortGroupName -contains $group.Name)  
       else {  
         $Portgrouptype = "VirtualMachine"  
       } #else #if ($VMkernelPorts.PortGroupName -contains $group.Name)  
         $Obj | Add-Member -Name PortGroupType -MemberType NoteProperty -Value $Portgrouptype  
       
       $Macs = $Group.ExtensionData.port.mac -join ", "  
         $Obj | Add-Member -Name Mac -MemberType NoteProperty -Value $Macs  
       
       $Obj | Add-Member -Name AllowPromiscuous -MemberType NoteProperty -Value $Group.ExtensionData.ComputedPolicy.security.AllowPromiscuous  
       $Obj | Add-Member -Name MacChanges -MemberType NoteProperty -Value $Group.ExtensionData.ComputedPolicy.security.MacChanges  
       $Obj | Add-Member -Name ForgedTransmits -MemberType NoteProperty -Value $Group.ExtensionData.ComputedPolicy.security.ForgedTransmits  
   
       #Teaming Policy  
       $NicTeamingPolicy = Get-NicTeamingPolicy -VirtualPortGroup $Group  
         $Obj | Add-Member -Name IsLoadBalancingInherited -MemberType NoteProperty -Value $NicTeamingPolicy.IsLoadBalancingInherited  
       
       $LoadBalancing = $Group.ExtensionData.ComputedPolicy.NicTeaming.Policy  
         switch ($LoadBalancing)   
           {   
             loadbalance_ip {$LoadBalancingPolicy = "Route based on IP hash"}   
             loadbalance_srcmac {$LoadBalancingPolicy = "Route based on source MAC hash"}   
             loadbalance_srcid {$LoadBalancingPolicy = "Route based on the source of the port ID"}   
             failover_explicit {$LoadBalancingPolicy = "use explicity failover order"}   
             default {$LoadBalancingPolicy = "Can't Determine"}  
           } #switch ($LoadBalancing)  
         $Obj | Add-Member -Name LoadBalancing -MemberType NoteProperty -Value $LoadBalancingPolicy  
       
       $Obj | Add-Member -Name IsNetworkFailoverDetectionInherited -MemberType NoteProperty -Value $NicTeamingPolicy.IsNetworkFailoverDetectionInherited  
       $Obj | Add-Member -Name NetworkFailoverDetection -MemberType NoteProperty -Value $NicTeamingPolicy.NetworkFailoverDetectionPolicy  
       $Obj | Add-Member -Name IsNotifySwitchesInherited -MemberType NoteProperty -Value $NicTeamingPolicy.IsNotifySwitchesInherited  
       $Obj | Add-Member -Name NotifySwitches -MemberType NoteProperty -Value $Group.ExtensionData.ComputedPolicy.NicTeaming.NotifySwitches  
       $Obj | Add-Member -Name IsFailbackInherited -MemberType NoteProperty -Value $NicTeamingPolicy.IsFailbackInherited  
       $Obj | Add-Member -Name Failback -MemberType NoteProperty -Value $Group.ExtensionData.ComputedPolicy.NicTeaming.RollingOrder  
       $Obj | Add-Member -Name IsFailoverOrderInherited -MemberType NoteProperty -Value $NicTeamingPolicy.IsFailoverOrderInherited  
   
       $ActiveNics = $Group.ExtensionData.ComputedPolicy.NicTeaming.NicOrder.ActiveNic -join ", "  
         $Obj | Add-Member -Name ActiveNics -MemberType NoteProperty -Value $ActiveNics  
       $StandbyNics = $Group.ExtensionData.ComputedPolicy.NicTeaming.NicOrder.StandbyNics -join ", "  
         $Obj | Add-Member -Name StandbyNics -MemberType NoteProperty -Value $StandbyNics  
       $UnusedNics = $NicTeamingPolicy.UnusedNic -join ", "  
         $Obj | Add-Member -Name UnusedNics -MemberType NoteProperty -Value $UnusedNics  
       
       #All VMs having assigned PortGroup  
       $IPv4Address = $null  
       if ($Portgrouptype -eq "VirtualMachine") {  
         $VMList = $null  
         $VMList = $Group | Get-VM  
         $VMs = $null  
         $VMs = $($VMList | Select-Object -ExpandProperty Name) -join ", "  
         $IPlist = $VMList.guest.Nics | Where-Object {$_.NetworkName -eq $Group.Name}  
         $IPv4List = $IPlist.IpAddress | Where-Object {$_ -match $IPregex}  
         $IPv4Address = $IPv4List -Join ", "  
         $IPv4SubnetMask = $null  
         $PortBindingCompliance = $null  
         $MTU = $null  
         $VmkernelNic = $null  
         $PathStatus = $null  
         $MGMT = $null  
         $vMotion = $null  
         $FT = $null  
         $vSAN = $null  
           
       } #if ($Portgrouptype -eq "VirtualMachine")  
       else {  
         $iSCSIPortGroup = $esxcli.iscsi.networkportal.list($iSCSIHBA.Name).PortGroup | Where-Object {$_ -eq $Group.Name}  
         $VmkernelNicinfo = $VMkernelPorts | Where-Object {$_.PortGroupName -eq $Group.Name}  
         if ($iSCSIPortGroup -ne $null) {  
           $VMList = $null  
           $VMs = $null  
           $PortBinding = "Configured"  
           $IPv4Address = $esxcli.iscsi.networkportal.list($iSCSIHBA.Name) | Where-Object {$_.PortGroup -eq $iSCSIPortGroup} | Select-Object -ExpandProperty IPv4  
           $IPv4SubnetMask = $esxcli.iscsi.networkportal.list($iSCSIHBA.Name) | Where-Object {$_.PortGroup -eq $iSCSIPortGroup} | Select-Object -ExpandProperty IPv4SubnetMask  
           $PortBindingCompliance = $esxcli.iscsi.networkportal.list($iSCSIHBA.Name) | Where-Object {$_.PortGroup -eq $iSCSIPortGroup} | Select-Object -ExpandProperty CompliantStatus  
           $MTU = $esxcli.iscsi.networkportal.list($iSCSIHBA.Name) | Where-Object {$_.PortGroup -eq $iSCSIPortGroup} | Select-Object -ExpandProperty MTU  
           $VmkernelNic = $esxcli.iscsi.networkportal.list($iSCSIHBA.Name) | Where-Object {$_.PortGroup -eq $iSCSIPortGroup} | Select-Object -ExpandProperty Vmknic  
           $PathStatus = $esxcli.iscsi.networkportal.list($iSCSIHBA.Name) | Where-Object {$_.PortGroup -eq $iSCSIPortGroup} | Select-Object -ExpandProperty PathStatus  
           $MGMT = $VmkernelNicinfo.ManagementTrafficEnabled  
           $vMotion = $VmkernelNicinfo.vMotionEnabled  
           $FT = $VmkernelNicinfo.FaultToleranceLoggingEnabled  
           $vSAN = $VmkernelNicinfo.VsanTrafficEnabled  
         } #if ($iSCSIPortGroup -ne $null)  
         else {  
           $VMList = $null  
           $VMs = $null  
           $IPv4Address = $VmkernelNicinfo.IP  
           $IPv4SubnetMask = $VmkernelNicinfo.SubnetMask  
           $PortBindingCompliance = "Not Configured"  
           $MTU = $VmkernelNicinfo.Mtu  
           $VmkernelNic = $VmkernelNicinfo.Name  
           $PathStatus = $null  
           $MGMT = $VmkernelNicinfo.ManagementTrafficEnabled  
           $vMotion = $VmkernelNicinfo.vMotionEnabled  
           $FT = $VmkernelNicinfo.FaultToleranceLoggingEnabled  
           $vSAN = $VmkernelNicinfo.VsanTrafficEnabled  
         } #else #if ($iSCSIPortGroup -ne $null)  
       } #if ($Portgrouptype -eq "VirtualMachine")  
       $Obj | Add-Member -Name VMCount -MemberType NoteProperty -Value $VMList.count  
       $Obj | Add-Member -Name VMs -MemberType NoteProperty -Value $VMs  
       $Obj | Add-Member -Name IPv4Address -MemberType NoteProperty -Value $IPv4Address  
       $Obj | Add-Member -Name IPv4SubnetMask -MemberType NoteProperty -Value $IPv4SubnetMask  
       $Obj | Add-Member -Name PortBindingCompliance -MemberType NoteProperty -Value $PortBindingCompliance  
       $Obj | Add-Member -Name MTU -MemberType NoteProperty -Value $MTU  
       $Obj | Add-Member -Name VmkernelNic -MemberType NoteProperty -Value $VmkernelNic  
       $Obj | Add-Member -Name PathStatus -MemberType NoteProperty $PathStatus  
       $Obj | Add-Member -Name MGMT-Enabled -MemberType NoteProperty $MGMT  
       $Obj | Add-Member -Name vMotion-Enabled -MemberType NoteProperty $vMotion  
       $Obj | Add-Member -Name FT-Enabled -MemberType NoteProperty $vMotion  
       $Obj | Add-Member -Name vSAN-Enabled -MemberType NoteProperty $vSan  
       $report += $obj  
       #check under $NicTeamingPolicy more settings   
     } #foreach ($Group in $PortGroup)  
   } #foreach ($esxi in $VMHosts)  
 } #Process  
 End {  
   $report #| Export-Csv C:\Temp\NetworkInfo.csv -NoTypeInformation  
 } #End  
This is how I connected to vcenter server and executed ps1 file. and exported results to csv file, if you don't export information to csv you can view results on csv.
And exported excel/csv file looks like this
Any suggestions and modifications are welcomed.



25 comments:

aaron said...

These information were very easy to understand. Keep up the good work.

Cloud Migration Services
AWS Cloud Migration Services
Azure Cloud Migration Services
VMware Cloud Migration Services
Cloud Migration tool
Database Migration Services
Cloud Migration Services



Anbarasan14 said...

Great info. Thanks for spending your valuable time to share this post.
Spoken English Classes in Chennai
Best Spoken English Classes in Chennai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Speaking Classes in Mumbai
English Speaking Course in Mumbai
IELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Coaching in Anna Nagar
Spoken English Class in Anna Nagar

Jagna Co Kalani said...

Great Article. Thank you for sharing! Really an awesome post for every one.
Project Centers in Chennai

JavaScript Training in Chennai

Final Year Project Domains for IT

JavaScript Training in Chennai

Tech Institute said...


Fantastic article with valuable information found very helpful waiting for next blog thank you.
Data Science Course in Hyderabad 360DigiTMG

Data Science Training said...

Really nice and interesting blog information shared was valuable and enjoyed reading this one. Keep posting. Thanks for sharing.
Data Science Training in Hyderabad

Cyber Security said...

Nice Information Your first-class knowledge of this great job can become a suitable foundation for these people. I did some research on the subject and found that almost everyone will agree with your blog.
Cyber Security Course in Bangalore

Cyber Security Course said...

Writing in style and getting good compliments on the article is hard enough, to be honest, but you did it so calmly and with such a great feeling and got the job done. This item is owned with style and I give it a nice compliment. Better!
Cyber Security Training in Bangalore

PMP Certification said...

Hello! I just want to give a big thank you for the great information you have here in this post. I will probably come back to your blog soon for more information!. PMP Certification in Hyderabad

Data Science said...

Great article with valuable information found very resourceful and enjoyed reading it waiting for next blog updated thanks for sharing.
typeerror nonetype object is not subscriptable

Data Science Training in Bangalore said...

It's good to visit your blog again, it's been months for me. Well, this article that I have been waiting for so long. I will need this post to complete my college homework, and it has the exact same topic with your article. Thanks, have a good game.

Business Analytics Course in Bangalore

Data Analytics Course said...

It's like you understand the topic well, but forgot to include your readers. Maybe you should think about it from several angles.

Data Analytics Course in Bangalore

Excelr Tuhin said...

I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it!
data science courses

Priyanka said...

Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
Data Analyst Course

DataScience Specialist said...

I have to search sites with relevant information ,This is a
wonderful blog,These type of blog keeps the users interest in
the website, i am impressed. thank you.
Data Science Training in Bangalore

Data Science training said...

Fantastic blog extremely good well enjoyed with the incredible informative content which surely activates the learners to gain the enough knowledge. Which in turn makes the readers to explore themselves and involve deeply in to the subject. Wish you to dispatch the similar content successively in future as well.

Data Science Course in Raipur

Data Science Training in Bangalore said...

I bookmarked your website because this site contains valuable information. I am very satisfied with the quality and the presentation of the articles. Thank you so much for saving great things. I am very grateful for this site.

Data Science Training in Bangalore

Digital Marketing Training in Bangalore said...

Wonderful blog found to be very impressive to come across such an awesome blog. I should really appreciate the blogger for the efforts they have put in to develop such amazing content for all the curious readers who are very keen on being updated across every corner. Ultimately, this is an awesome experience for the readers. Anyways, thanks a lot and keep sharing the content in the future too.

Digital Marketing Training in Bangalore

Artificial Intelligence Training in Bangalore said...

I wanted to leave a little comment to support you and wish you the best of luck. We wish you the best of luck in all of your blogging endeavors.

Artificial Intelligence Training in Bangalore

Machine Learning Course in Bangalore said...

The Extraordinary blog went amazed by the content that they have developed in a very descriptive manner. This type of content surely ensures the participants explore themselves. Hope you deliver the same near the future as well. Gratitude to the blogger for the efforts.

Machine Learning Course in Bangalore

fueldigital said...

Nice Blog, keep it up for more updates about this type of blog
Hii, This is Great Post..!
Best Digital Marketing Agency in Chennai
Best Content Marketing companies in Chennai
Best SEO Services in Chennai
digital marketing company in chennai
website designing company in chennai. .
best digital marketing company in chennai

trainingcourses said...


Excellent effort to make this blog more wonderful and attractive.
business analytics course

cloud computing course in bangalore said...

Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing. data scientist course in delhi

data science said...


I was basically inspecting through the web filtering for certain data and ran over your blog. I am flabbergasted by the data that you have on this blog. It shows how well you welcome this subject. Bookmarked this page, will return for extra. data science course in jaipur

Artificial Intelligence Training in BLR said...

A good blog always contains new and exciting information, and reading it I feel like this blog really has all of these qualities that make it a blog.

Artificial Intelligence Training in Bangalore

Digital Marketing Training in BLR said...

It is late to find this act. At least one should be familiar with the fact that such events exist. I agree with your blog and will come back to inspect it further in the future, so keep your performance going.

Digital Marketing Training in Bangalore