dvSwitch scripting – Part 7 – Find portgroup/Change VLAN Id

In a comment on one of the previous dvSwitch posts, see dvSwitch scripting – Part 2 – dvPortgroup, Gert asked how he could check if a portgroup with a specific VLAN Id existed on a distributed virtual switch.

Since a function that allows you to search for a portgroup that meets specific requirements can be quite useful, I decided to create a new function to do just that.

Additionally I will show in this post how you can change the VLAN Id of a specific portgroup.

The script that accomplishes this is quite simple.

This is the dvSwitch and portgroup configuration from which we start.

Annotations

Line 1-15: The Get-dvSwitch function was already presented in Part 2 of the dvSwitch series.

Line 18-20: The Get-dvSwPg function uses for now two parameters with which it can look for a specific portgroup. These are the portgroup name and the VLAN id. The function can easily be extended to look for other properties that uniquely distinguish a portgroup. Also note that the function only takes one of the parameters, so it is either the portgroup name or the VLAN Id for now.
Line 34-51: The Set-dvSwPgVLAN function configures a portgroup with the provided VLAN Id.

Line 42-43: As always, it is critical to provide the “current” config version, otherwise the call to the ReconfigureDVPortgroup_Task method will fail.

Line 59: A sample call to the Get-dvSwPg function. In this sample the VALN Id is used to find the portgroup. If no matching portgroup is found, the function will returm $null.

Line 62: A sample call to the Set-dvSwPgVLAN function. It changes the VLAN Id from 111 to 902 in this example.

The result of the above script:

Feel free to post comments and questions.

8 Comments

    Ole

    Great! Thank you!

    Ole

    Exactly.

      LucD

      Ole, have a look at the updated dvSwitch scripting – Part 2 – dvPortgroup post. You can now define VLAN types !

    Ole

    Hi,

    I’ve found great use of your examples, but one tiny, yet important thing keeps stopping me. When I add a distributed portgroup, how do specify that is of “Vlan type: VLAN”? I can create portgroups and change vlanID – but can’t find the propery to change it to vlan! :-/

      LucD

      Hi Ole, I suspect you mean the choice between “none”, “VLAN”, “VLAN Trunking” and “Private VLAN” for the VLAN type field in the vSphere Client when you create a new portgroup ?

    gert van gorp

    thanks luc,

    this will make my scripting a bit easier. Wil test & implement it next week because I am in the states on a vmware partner conference.

    greetings

    Gert

      LucD

      Gert,
      Enjoy your stay at PEX.
      Just let me know if it works for you when you’re back.
      Luc.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

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