Home > PowerCLI, PowerShell, dvSwitch, vSphere > dvSwitch scripting – Part 7 – Find portgroup/Change VLAN Id

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

February 8th, 2010 LucD Leave a comment Go to comments

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.

function Get-dvSwitch{
	param($dcName,$dvSwName)

	$dcNetFolder = Get-View (Get-Datacenter $dcName | Get-View).NetworkFolder
	$found = $null
	foreach($net in $dcNetFolder.ChildEntity){
		if($net.Type -eq "VmwareDistributedVirtualSwitch"){
			$temp = Get-View $net
			if($temp.Name -eq $dvSwName){
				$found = $temp
			}
		}
	}
	$found
}

function Get-dvSwPg{
	param($dvSw,
		[string]$PGName,
		[int]$VLANnr)

# Search for Portgroup Name
	if($PGName){
		$dvSw.Portgroup | %{Get-View -Id $_} | `
			where{$_.Name -eq $PGName}
	}
# Search for VLAN number
	elseif($VLANnr){
		$dvSw.Portgroup | %{Get-View -Id $_} | `
			where{$_.Config.DefaultPortConfig.Vlan.VlanId -eq $VLANnr}
	}
}

function Set-dvSwPgVLAN{
	param($dvSw, $dvPg, $vlanNr)

	$spec = New-Object VMware.Vim.DVPortgroupConfigSpec
	$spec.defaultPortConfig = New-Object VMware.Vim.VMwareDVSPortSetting
	$spec.DefaultPortConfig.vlan = New-Object VMware.Vim.VmwareDistributedVirtualSwitchVlanIdSpec
	$spec.defaultPortConfig.vlan.vlanId = $vlanNr

	$dvPg.UpdateViewData()
	$spec.ConfigVersion = $dvPg.Config.ConfigVersion

	$taskMoRef = $dvPg.ReconfigureDVPortgroup_Task($spec)

	$task = Get-View $taskMoRef
	while("running","queued" -contains $task.Info.State){
		$task.UpdateViewData("Info")
	}
}

$datacenterName = "Home1"
$dvSwitchName = "dvSw1"
$dvSwPgName = "dvPg2"

$dvSw = Get-dvSwitch $datacenterName $dvSwitchName

$dvSwPg = Get-dvSwPg $dvSw -VLANnr 111

if($dvSwPg){
	Set-dvSwPgVLAN $dvSw $dvSwPg 901
}

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.

  1. Ole
    March 30th, 2010 at 09:12 | #1

    Great! Thank you!

  2. Ole
    March 27th, 2010 at 07:54 | #2

    Exactly.

  3. Ole
    March 26th, 2010 at 14:15 | #4

    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! :-/

    • March 26th, 2010 at 14:30 | #5

      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 ?

  4. February 8th, 2010 at 19:22 | #6

    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

    • February 8th, 2010 at 21:23 | #7

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

  1. No trackbacks yet.