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.



15 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



Tech Institute said...


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

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

traininginstitute said...


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

Tamil novels said...

Very interesting blog. Keep sharing.
Tamil novels pdf free download
Ramanichandran novels PDF
srikala novels PDF
Mallika manivannan novels PDF
muthulakshmi raghavan novels PDF
Infaa Alocious Novels PDF
N Seethalakshmi Novels PDF
Sashi Murali Tamil Novels PDF Download

traininginstitute said...

This is really very nice post you shared, i like the post, thanks for sharing..
data science training

Pallavireddy said...

i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
Data Engineering Course in India

Ramesh Sampangi said...

Attend the best data science course training in Hyderabad program by AI Patasala, here you can learn data science concepts with practical knowledge.
Data Science Training and Placements in Hyderabad

traininginstitute said...

This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
data science course in malaysia

data analytics courses malaysia said...

I am another customer of this site so here I saw various articles and posts posted by this site,I curious more energy for some of them trust you will give more information further.

PMP Training in Malaysia said...

360DigiTMG, the top-rated organisation among the most prestigious industries around the world, is an educational destination for those looking to pursue their dreams around the globe. The company is changing careers of many people through constant improvement, 360DigiTMG provides an outstanding learning experience and distinguishes itself from the pack. 360DigiTMG is a prominent global presence by offering world-class training. Its main office is in India and subsidiaries across Malaysia, USA, East Asia, Australia, Uk, Netherlands, and the Middle East.

liftndriftpaperplane said...

Incredible post I should say and a debt of gratitude is in order for the data. Schooling is certainly a tacky subject. Be that as it may, is still among the main subjects within recent memory. I appreciate your post and anticipate more. You have made some valid statements there. I looked on the web to study the issue and discovered a great many people will oblige your perspectives on this site...
how to make a paper airplane eagle | how to make a boomerang airplane | the eagle paper airplane | best paper airplane design for distance and accuracy

traininginstitute said...

Thanks for your post. I’ve been thinking about writing a very comparable post over the last couple of weeks, I’ll probably keep it short and sweet and link to this instead if thats cool. Thanks.
cyber security course malaysia

saipdf said...

Very useful post
Sai Satcharitra Pdf
Sai Satcharitra Telugu Pdf
Sai Satcharitra Tamil Pdf

deekshitha said...

I wholeheartedly congratulate the writer of this post for explaining the difficult concepts of data science in a simple and easy-to-understand manner. My only regret is that I didn’t read this post earlier. I have made many career decisions in my life after reading this, and have no regrets whatsoever to date.mlops training