Enable MAC Learning as Default on vSphere Distributed Switch

This short PowerCLI script will change the vSphere Distributed Switch default port group configuration to enable MAC learning policy. This means every port group on such switch inherits this configuration and will have MAC learning enabled unless specifically disabled.

For more information why would you need that read William’s Lam blog.

$vds = get-vdswitch 'DSwitch1'
$spec = New-Object VMware.Vim.VMwareDVSConfigSpec
$spec.DefaultPortConfig = New-Object VMware.Vim.VMwareDVSPortSetting
$spec.DefaultPortConfig.MacManagementPolicy = New-Object VMware.Vim.DVSMacManagementPolicy
$spec.DefaultPortConfig.MacManagementPolicy.MacLearningPolicy = New-Object VMware.Vim.DVSMacLearningPolicy

$spec.DefaultPortConfig.MacManagementPolicy.MacLearningPolicy.Enabled = $True
$spec.DefaultPortConfig.MacManagementPolicy.MacLearningPolicy.AllowUnicastFlooding = $True
$spec.DefaultPortConfig.MacManagementPolicy.MacLearningPolicy.Limit = 4000
$spec.DefaultPortConfig.MacManagementPolicy.MacLearningPolicy.LimitPolicy = "DROP"
$spec.ConfigVersion = $vds.ExtensionData.Config.ConfigVersion
$vds.ExtensionData.ReconfigureDvs_Task($spec)

 

Update 08/07/2020

In case you are using this approach for nested vSphere lab instead of the old promiscuous mode, make sure the vmk0 vmkernel port has a different MAC address than the MAC address of the vmnic of the nested ESXi host. This is because when the vmk0 is migrated to a different ESXi host uplink the vDS will not learn the MAC address on the new switch port as it conflicts with the assigned MAC on the first uplink port (same MAC cannot be learnt on two ports).

The vmkernel port MAC can be easily changed by editing /etc/vmware/esx.conf file.

12 thoughts on “Enable MAC Learning as Default on vSphere Distributed Switch

  1. Hey Miguel

    Thanks for writing this script.

    I have a problem.. I copied and pasted your script into Notepad and edited the get-vdswitch ” string to the name of my vDSwtich and then saved the doc as a .ps1 PowerShell Script.

    I then run the script in VMware PowerCLI 12.1.0 which returns “Task task-xxxx” after I’ve run the script, but when I add new PortGroups to the vDS, they all have MacLearning disabled “false”.

    Am I doing something wrong?

    Thanks!

    Andy

  2. Hi Tomas…
    I run the script and which returns “Task task-xxxx” after I’ve run the script.
    How can I verify if MacLearning is enable ?
    William’s scripts not works….

    Is there another way for see how to MacLearning policy is set ?

      1. I tried to run the two script (Get and Set MacLearn) without success….
        I’ve VCSA and ESXi 6.7 U3. I created a VDS 6.6 with name DS-VMW-ESX and a portgroup with name “MGMT-vMotion” but when run Get-MacLear I don’t receive any output..

        PS E:\> .\Get-MacLearn.ps1 -DVPortgroupName @(“MGMT-vMotion”)
        PS E:\>

        If run cmdlet Get-VDPortGroup its works fine and name is correct.

        1. I found where to check macLearningPolicy for VDS via MOB path:
          https://vmw-vcenter.dl-vmw.maticmind.it/mob/?moid=dvportgroup%2d97&doPath=config%2edefaultPortConfig%2emacManagementPolicy%2emacLearningPolicy

          I see these values:

          Properties

          NAME TYPE VALUE
          allowUnicastFlooding boolean true
          enabled boolean true
          inherited boolean true
          limit int 4000
          limitPolicy string “DROP”

          Can you confirm that if I see these values on all dpgroup, the MacLearning is enable at the VDS level?

          Thanks,
          for your support.

  3. What would be the expected output from running this script? When I ran it I got this result:

    Type Value
    —- —–
    Task task-11095

    Didn’t seem to show any errors so I assume it ran correctly?

      1. Thanks for that information. By any chance do you know the procedure I should use to check the task status?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.