Create Virtual networks in VMM with Powershell

4 minute read

I’m now working on a HyperV migration project to move all servers from one datacenter to a new datacenter. This implies that I need to move all virtual machines with HyperV replica, configure virtual networks in the new datacenter etc.

My customer is using VLAN Isolation in VMM. For more information about Network isolation have a look here.  Service Providers often use dedicated VLANs and PVLANs to isolate different tenants from one another.  As described in the blog post I’m using one logical network for all of the tenants, and configure sites, vlans and networks per tenant. Multiple VM networks may be linked to the same Logical Network if Network Virtualization is enabled, with each one of these VM Networks separated from and totally unaware of the existence of any of the others. But if you have a lot tenants this is a lot of manual work 🙂

As I couldn’t reuse the old VMM server and config in the new datacenter for several reasons I have to re-configure the networking (Vlans, subnets, network sites, etc)  configuration from scratch. So  I found a way to automate all of this labour intensive tasks with Powershell.

This blogpost is not about how to configure step by step networking in VMM, if you need more information on how to configure networking in vmm have a look here  or download the Networking white paper from Kristian Nese .

The key thing to be able to keep an overview (if you are using networking in VMM) and for easy troubleshooting is NAMING CONVENTION. Without a proper naming convention it will be very hard to now which VMnetwork is connected to which site and using this vlan or this subnet…

So I’m using the following naming convention:

  • Logical Network Name = “Customer – Logical Network – Name”
  • Network Site = “Customer + VLANname + Site + VLanID”
  • Virtual Network Name =  “Customer + VLANname + VMNetwork + VLanID”
  •  SubnetName = “Customer + VLANname + VMSubnet+ VLanID”
  • etc

You get the point.

 

Now let’s get started!

First of all the the script assumes you already have a Logical network defined using VLAN based indepentend networks.

logical network

 

Open Powershell ISE and copy past the script. Next you just change the following variables in the scrip to match your own:

[xml]

$Subnet = "10.100.40.10/24"  
$VlanID = 521  
$VlanName = "ITSystems"  
$Customer = "Contoso"

[/xml]

The end result is:

  • All my network sites are created with proper naming convention
  • Associated Vlans and subnet defined

network sites

and Virtual Networks created:

vmnetworks

and Port Profile changed as well.

 

Below you can find the whole script:

[xml]

PowerShell Script to configure Networking in VMM

#

The script assumes you already have defined your networkig in VMM as explained here for examaple http://charbelnemnom.com/2014/05/create-a-converged-network-fabric-in-vmm-2012-r2/

Once this is done and you have your logical network, your logical swtich and your port profile, you need to create sites and VLAN per tenant if your using VLAN isolation

So the next labour intensive task is to create sites,vlans, subnet, reconfigure the portprofile with new site ,create a virtual network etc manually.

Auhtor: Alexandre Verkinderen

#Get Variable
#Specify your existing Logical network name
$NetworkName = “Contoso – Logical Network – Tenants”
#Specify your existing PortProfile.
$portProfile = Get-SCNativeUplinkPortProfile -Name “Contoso – Reference – Profile” -ID “a3c36703-4e0b-4b25-baa7-69b347be8c72”
#Specify the new subnet, the new vlan ID and a Name for the new network site
############################
$Subnet = “10.100.40.10/24”
$VlanID = 521
$VlanName = “ITSystems”
$Customer = “Contoso”
############################

$NetworkSiteName = $Customer + “ – “ + $VlanName+ “ – Site – “ + $VlanID
$VMNetworkName = $Customer + “ – “ +$VlanName+ “ – VMNetwork – “ + $VlanID
$VMSubnetName = $Customer + “ – “ + $VlanName+ “ – VMSubnet – “ + $VlanID

#First we get the Logical network that has already been created
$logicalNetwork = Get-SCLogicalNetwork | Where {$.Name -Match $NetworkName }
#The Set-SCLogicalNetwork cmdlet changes the properties of a System Center Virtual Machine Manager (VMM) logical network object.
Set-SCLogicalNetwork -Name $NetworkName -Description “” -LogicalNetwork $logicalNetwork -RunAsynchronously -EnableNetworkVirtualization $false -UseGRE $false -LogicalNetworkDefinitionIsolation $true
#We are defining the new network settings for the All hosts group. If you need more granularity you can specify other host group names here
$allHostGroups = @()
$allHostGroups += Get-SCVMHostGroup | Where {$
.Name -Match “All Hosts” }
$allSubnetVlan = @()
$allSubnetVlan += New-SCSubnetVLan -Subnet $Subnet -VLanID $VlanID

#CreateSite
#he New-SCLogicalNetworkDefinition cmdlet creates a definition for a System Center Virtual Machine Manager (VMM) logical network. The logical network can be associated with one or more host groups. A logical network definition is also called a network site.
#After you create a new logical network, use the logical network definitinon to assign IP subnets and VLANs to the logical network.
New-SCLogicalNetworkDefinition -Name $NetworkSiteName -LogicalNetwork $logicalNetwork -VMHostGroup $allHostGroups -SubnetVLan $allSubnetVlan -RunAsynchronously
Write-Output “Site ok”

#CreateVMNetwork
#The New-SCVMNetwork cmdlet creates a virtual machine network. Virtual machine networks support multiple methods of isolation: No isolation, network virtualization, external, and VLAN
$vmNetwork = New-SCVMNetwork -Name $VMNetworkName -LogicalNetwork $logicalNetwork -IsolationType “VLANNetwork”

$logicalNetworkDefinition = Get-SCLogicalNetworkDefinition -Name $NetworkSiteName -LogicalNetwork $logicalNetwork
$subnetVLAN = New-SCSubnetVLan -Subnet $Subnet -VLanID $VlanID
$vmSubnet = New-SCVMSubnet -Name $VMSubnetName -LogicalNetworkDefinition $logicalNetworkDefinition -SubnetVLan $subnetVLAN -VMNetwork $vmNetwork
Write-Output “VMNetwork ok”

#ChangePortProfile
$logicalNetworkDefinitionsToAdd = @()
$logicalNetworkDefinitionsToAdd += Get-SCLogicalNetworkDefinition -Name $NetworkSiteName
Set-SCNativeUplinkPortProfile -NativeUplinkPortProfile $portProfile -Name “Contoso – Reference – Profile” -Description “” -EnableNetworkVirtualization $true -LBFOLoadBalancingAlgorithm “Dynamic” -LBFOTeamMode “SwitchIndependent” -RunAsynchronously -AddLogicalNetworkDefinition $logicalNetworkDefinitionsToAdd
Write-Output “Profile Changed”
[/xml]

You can also download the whole script from here: https://gallery.technet.microsoft.com/Configure-Virtual-Machine-a362f121

Hope this helps,

Alexandre Verkinderen

Leave a comment