Adding several networks to your VMM 2012 SP1 with PowerShell

I have created an environment at a customer with bare metal deployment with Hyper-V 2012 and SC VMM 2012 SP1 and in the bare metal profile I am utilizing the logical switches to get a consistent configuration on all hosts and not needing to configure each host every time a new network is going to be set up.

In an earlier post I made a script for adding networks into VMM 2012 but now in SP1 we have logical switches and now also a VM network, the later that adds a bit of complexity in the adding process.

In this case we are using VLAN´s and I want to be able to configure and add them without going through the console and all dialogs in the GUI

To get VLAN properly working you also need to check a box in the logical network properties

Screen Shot 2013-06-10 at 22.23.35

So this script asumes that you have a Logical Network already defined and what we do here is adding the subnets in the network sites. If someone finds another way to why I have to add all the subnetVLan objects every time I update the Logical Network Definition, that would be super.. If I do not add all the objects the networks without dependencies are removed.. not so smart

As you can see on this error message when I just try to update with a new Subnet VLAN and not including the other VLAN objects the Set-SCLogicalNetworkDefinition tries to remove and as there is already an VM network it fails. If you look at the script from the gui wizard you will see and understand what I am fuzzing about.

Screen Shot 2013-06-10 at 22.41.01

The structure of the CSV file is as the next screendump and If you already have an excel document or some other information table you could easily change the script to suit your environment and deployment.

Screen Shot 2013-06-10 at 22.34.42

And when running the following PowerShell script it will create not only the subnets in the Network Site but also the VM Networks. Here is the logical network before running the script

Screen Shot 2013-06-10 at 22.57.23

And here is after

Screen Shot 2013-06-10 at 23.02.18

And the VM network, as you can see on the details on the ADM-Servers details it is connected to the VM Subnet with the VLAN 399

Screen Shot 2013-06-10 at 23.03.17
Screen Shot 2013-06-10 at 23.03.33

And here is the script:

# Add Networks to VMM 
#
# Niklas Åkerlund 2013-06-10
$LogicalNetName = "VMNET"
$LogicalNetDefName = "DevNets"
$ImportFile = "C:\PowerShell\networkvms.csv"

# The networks that is going to be imported
$vlans = import-csv $ImportFile -Delimiter ";"
$LogicalNet = Get-SCLogicalNetwork -Name $LogicalNetName

$allSubnetVlan = @()
foreach ($vlan in $vlans) {
    # First in fabric 
    $LogicalNetDef = Get-SCLogicalNetworkDefinition -LogicalNetwork $LogicalNet -Name $LogicalNetDefName
    $allSubnetVlan = $LogicalNetDef.SubnetVLans    
    $Subnet = $vlan.IPnet + $vlan.Octet
    $SubnetVlan = New-SCSubnetVLan -Subnet $Subnet -VLanID $vlan.VLAN
    $allSubnetVlan += $SubnetVLAN
    Set-SCLogicalNetworkDefinition -LogicalNetworkDefinition $LogicalNetDef -SubnetVLan $allSubnetVlan

    # VM Networks
    $vmNetwork = New-SCVMNetwork -Name $vlan.Name -LogicalNetwork $LogicalNet -IsolationType "VLANNetwork"
    New-SCVMSubnet -Name $vlan.Name -LogicalNetworkDefinition $LogicalNetDef -SubnetVLan $SubnetVLAN -VMNetwork $vmNetwork

}

There are some things that I am working on the next version of this simple script, I am going to make it as a function and also some error checking and also verify if the networks already exists or not, that will be posted in another article 🙂