VMware Cloud Community
ChristianNilsso
Contributor
Contributor
Jump to solution

Adding new VirtualPortGroup to a folder.

Hi.

I'm trying to figure out if there is a way to create a new virtualportgroup, and then add it to a folder under Network in a scritp/oneliner.

Today I use this to add portgroups:

Get-Cluster "Your Cluster" | Get-VMHost | Get-VirtualSwitch -Name "vSwitch0"`

| New-VirtualPortGroup -Name "New VLAN" -VLanId 123

For security reasons we have set permissions on the folders for different usergroups

But now I manualy need to move the new portgroup to the right folder, it whould be nice to be able to do this in the script.

We run ESX4.0 u1

//Christian

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

This is possible but you will have to use the SDK method since the Move-Folder cmdlet doesn't support the 'network' folder yet.

This is a sample script

$pgName = 'MyPG2'                          # Name of the new portgroup
$esxName = 'esx41.test.local'          # ESX host where the new portgroup is created
$tgtFolder = 'MyNet'                          # Child folder in the network folder

$esxImpl = Get-VMHost $esxName

# Create portgroup
$pgImpl = $esxImpl | Get-VirtualSwitch -Name 'vSwitch1' | New-VirtualPortGroup -Name $pgName  -VLanId 777

# Get network folder
$esx = $esxImpl | Get-View
$dc = Get-Datacenter -VMHost $esxImpl | Get-View
$netFolder = Get-View $dc.NetworkFolder

# Find portgroup
$netFolder.ChildEntity | where {$_.Type -eq 'Network'} | %{
	$child = Get-View $_
	if($child.Name -eq $pgName){
		$pgMoRef = $child.MoRef
	}
}

# Move portgroup into folder
$netFolder.ChildEntity | where {$_.Type -eq 'Folder'} | %{
	$child = Get-View $_
	if($child.Name -eq $tgtFolder){
		$child.MoveIntoFolder($pgMoRef)
	}
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

This is possible but you will have to use the SDK method since the Move-Folder cmdlet doesn't support the 'network' folder yet.

This is a sample script

$pgName = 'MyPG2'                          # Name of the new portgroup
$esxName = 'esx41.test.local'          # ESX host where the new portgroup is created
$tgtFolder = 'MyNet'                          # Child folder in the network folder

$esxImpl = Get-VMHost $esxName

# Create portgroup
$pgImpl = $esxImpl | Get-VirtualSwitch -Name 'vSwitch1' | New-VirtualPortGroup -Name $pgName  -VLanId 777

# Get network folder
$esx = $esxImpl | Get-View
$dc = Get-Datacenter -VMHost $esxImpl | Get-View
$netFolder = Get-View $dc.NetworkFolder

# Find portgroup
$netFolder.ChildEntity | where {$_.Type -eq 'Network'} | %{
	$child = Get-View $_
	if($child.Name -eq $pgName){
		$pgMoRef = $child.MoRef
	}
}

# Move portgroup into folder
$netFolder.ChildEntity | where {$_.Type -eq 'Folder'} | %{
	$child = Get-View $_
	if($child.Name -eq $tgtFolder){
		$child.MoveIntoFolder($pgMoRef)
	}
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ChristianNilsso
Contributor
Contributor
Jump to solution

LucD

Thanks for a quick reply.

It works perfectly.

I'll be working in the lab to se if I can get it to att multiple portgroups on multiple hosts.

I guess I can att $esxName1, $esxName2 and $esxImpl1, $esxImpl2 to add a port group to multible Hosts

You are te king of PowerCLI. I bow before you Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That shouldn't be a problem.

Note that if these ESX(i) servers are in a cluster, you will see the portgroup only once in the Networking view.

So there should be only 1 move to the folder.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ChristianNilsso
Contributor
Contributor
Jump to solution

Hi.

I got it to work with 2 host and 1 PG.

But not with 2 hosts and 2 PG's.

It does not move the second PG.

Tryed to add:

  1. Find portgroup server 2

$netFolder.ChildEntity | where {$_.Type -eq 'Network'} | %{

$child2 = Get-View $_

if($child.Name -eq $pgName2){

$pgMoRef2 = $child.MoRef2

}

}

  1. Move portgroup 2 into folder

$netFolder.ChildEntity | where {$_.Type -eq 'Folder'} | %{

$child2 = Get-View $_

if($child.Name -eq $tgtFolder){

$child.MoveIntoFolder($pgMoRef2)

}

}

But that did not help.

And anyway, If I have 3 hosts and 10 new PG's it will get a bit messy if I can't use variables for at least host. like Get-Cluster | Get-VMHost.

I guess I can run the script for 1 PG at a time. It's not often we mass update PG's Smiley Happy

Thanks for you help

0 Kudos
LucD
Leadership
Leadership
Jump to solution

A modified version of the earlier script that:

*) will create the PG on each server in a cluster

*) will move the PG to the folder

$pgName = 'MyPG2' 						# Name of the new portgroup
$clusterName = 'MyCluster'		# ESX host where the new portgroup is created
$tgtFolder = 'MyNet' 					# Child folder in the network folder

$esxHosts  = Get-Cluster -Name $clusterName | Get-VMHost
$esxHosts | %{
	$esxImpl = $_

	# Create portgroup on each ESX server
	$pgImpl = $esxImpl | Get-VirtualSwitch -Name 'vSwitch1' | New-VirtualPortGroup -Name $pgName -VLanId 777
}

# Get network folder
$esx = $esxHost | Select -First 1 | Get-View
$dc = Get-Datacenter -VMHost $esxImpl | Get-View
$netFolder = Get-View $dc.NetworkFolder

# Find portgroup
$netFolder.ChildEntity | where {$_.Type -eq 'Network'} | %{
	$child = Get-View $_
	if($child.Name -eq $pgName){
		$pgMoRef = $child.MoRef
	}
}

# Move portgroup into folder
$netFolder.ChildEntity | where {$_.Type -eq 'Folder'} | %{
	$child = Get-View $_
	if($child.Name -eq $tgtFolder){
		$child.MoveIntoFolder($pgMoRef)
	}
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

LucD
Leadership
Leadership
Jump to solution

And another version that will create multiple portgroups on each ESX host in a cluster

$pgNames = 'MyPG1','MyPG2' 		# Name of the new portgroups
$clusterName = 'MyCluster' 		# ESX host where the new portgroup is created
$tgtFolder = 'MyNet' 					# Child folder in the network folder

$esxHosts = Get-Cluster -Name $clusterName | Get-VMHost

foreach($pgName in $pgNames){
	$esxHosts | %{
		$esxImpl = $_

		# Create portgroup on each ESX server
		$pgImpl = $esxImpl | Get-VirtualSwitch -Name 'vSwitch1' | New-VirtualPortGroup -Name $pgName -VLanId 777
	}

	# Get network folder
	$esx = $esxHost | Select -First 1 | Get-View
	$dc = Get-Datacenter -VMHost $esxImpl | Get-View
	$netFolder = Get-View $dc.NetworkFolder

	# Find portgroup
	$netFolder.ChildEntity | where {$_.Type -eq 'Network'} | %{
		$child = Get-View $_
		if($child.Name -eq $pgName){
			$pgMoRef = $child.MoRef
		}
	}

	# Move portgroup into folder
	$netFolder.ChildEntity | where {$_.Type -eq 'Folder'} | %{
		$child = Get-View $_
		if($child.Name -eq $tgtFolder){
			$child.MoveIntoFolder($pgMoRef)
		}
	}
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

ChristianNilsso
Contributor
Contributor
Jump to solution

Hi.

Thx for al your help.

I'll give it a go in the lab and tell you the resultes

0 Kudos
ChristianNilsso
Contributor
Contributor
Jump to solution

That script works like a charm, like al your work.

Just one thing. Can I get different vlan id on the different pg's?

Can you specify it in $pgNames ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, (nearly) everything is possible with PowerShell Smiley Wink

$pgNames = 'MyPG1','MyPG2' 		# Name of the new portgroups
$vlanIds = 111,222			# VLAN ids for the new portgroups (same order as PGs)
$clusterName = 'MyCluster' 		# ESX host where the new portgroup is created
$tgtFolder = 'MyNet' 		# Child folder in the network folder

$esxHosts = Get-Cluster -Name $clusterName | Get-VMHost

for($i=0;$i -lt $pgNames.Count;$i++){
	$pgName = $pgNames[$i]
	$vlanId = $vlanIds[$i]

	$esxHosts | %{
		$esxImpl = $_

		# Create portgroup on each ESX server
		$pgImpl = $esxImpl | Get-VirtualSwitch -Name 'vSwitch1' | New-VirtualPortGroup -Name $pgName -VLanId $vlanId
	}

	# Get network folder
	$esx = $esxHost | Select -First 1 | Get-View
	$dc = Get-Datacenter -VMHost $esxImpl | Get-View
	$netFolder = Get-View $dc.NetworkFolder

	# Find portgroup
	$netFolder.ChildEntity | where {$_.Type -eq 'Network'} | %{
		$child = Get-View $_
		if($child.Name -eq $pgName){
			$pgMoRef = $child.MoRef
		}
	}

	# Move portgroup into folder
	$netFolder.ChildEntity | where {$_.Type -eq 'Folder'} | %{
		$child = Get-View $_
		if($child.Name -eq $tgtFolder){
			$child.MoveIntoFolder($pgMoRef)
		}
	}
}

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ChristianNilsso
Contributor
Contributor
Jump to solution

You have magic PowerCLI powers my friend.

If your going to vmworld in Copenhagen, I'll track you down and get you a beer Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm doing a session (TA6944) with Alan Renouf in Copenhagen.

I'll be there Wednesday-Thursday.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ITSavant
Contributor
Contributor
Jump to solution

Based on LucD's sample I offer the following, suspiciously absent, function:

Function Move-VirtualPortGroup {

<#

.SYNOPSIS

Adds a virtual port group to a datcenter network folder

.DESCRIPTION

Move-VirtualPortGroup moves a virtual port group into a datcenter network folder

.PARAMETER PGNAME

Specify the Name of the Virtual Port Group you want to add to a Network Folder

.PARAMETER NETFOLDER

Specify the Name of the Network Folder to move the Virtual Port Group

.PARAMETER CLUSTER

Specify the Datacenter containing the Virtual Port Group and Network Folder

.PARAMETER SERVER

Specify the vCenter Server containing the Datacenter, Virtual Port Group, and Network Folder

.EXAMPLE

Move-VirtualPortGroup -PGName "VirtualPortGroup-01" -NetFolder "NetworkFolder-01" -Cluster $CL -Server $vCenter

Moves virtual port group "VirtualPortGroup-01" into the Network Folder "NetworkFolder-01" of Datacenter $DC

.EXAMPLE

Move-VirtualPortGroup -PGName "VirtualPortGroup-01" -NetFolder (Get-Cluster MyCluster -Server $vCenter | Get-Folder -Type Network "NetworkFolder-01")

Moves virtual port group "VirtualPortGroup-01" into the Network Folder "NetworkFolder-01" of Cluster $CL

.LINK

Sample Code (LucD):  https://communities.vmware.com/thread/280814

Get-VirtualPortGroup

New-VirtualPortGroup

Remove-VirtualPortGroup

Set-VirtualPortGroup

.INPUTS

VirtualPortGroupImpl

.OUTPUTS

Task

.COMPONENT

VMware vSphere PowerCLI

#>

  param([parameter(Mandatory=$true)] $PGName,

        [parameter(Mandatory=$true, ValueFromPipeline=$true)] $NetFolder,

        [parameter(Mandatory=$true, ValueFromPipeline=$true)] $Cluster,

        [parameter(Mandatory=$false, ValueFromPipeline=$true)] $Server)

  begin {

  $Datacenter = (Get-Cluster -Name $Cluster -Server $Server  | Get-DataCenter)

  }

  process {

  $pgNames = $PGName

  $clusterName = $Cluster

  $tgtFolder = $NetFolder

  $esxHosts = Get-Cluster -Name $clusterName -Location $Datacenter -Server $Server | Get-VMHost

  $esx = $esxHosts | Select -First 1 | Get-View

  foreach($pgName in $pgNames){$esxHosts | %{$esxImpl = $_}}

  $dc = Get-Datacenter -VMHost $esxImpl | Get-View

  $netFolder = Get-View $dc.NetworkFolder

  $netFolder.ChildEntity | where {$_.Type -eq 'Network'} | %{$child = Get-View $_;if($child.Name -eq $pgName){$pgMoRef = $child.MoRef}}

  $netFolder.ChildEntity | where {$_.Type -eq 'Folder'} | %{$child = Get-View $_; if($child.Name -eq $tgtFolder){$child.MoveIntoFolder($pgMoRef)}}

  }

}

Save above as File "Move-VirtualPortGroup.ps1"

NOTE: Updated 8.17.2016 to fix bug when connecting to multiple vCenters.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Nice one, thanks for sharing


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos