Updated Add-SCNetworks function for virtual Networks in SCVMM 2012 SP1

Yesterday I described how I could get the networking info out from the VMM before a reinstall or if for another reason you need to get all networks defined.

In an earlier post I described my add networks script and now I have extended it a bit. I have this week made a function of it and also I have changed the import file format to fit the export-file as you get with the earlier post mentioned above. In the function I create a logic network if it is not already in place, that is also done for the network site in the logic network. The logic network created utilize VLAN.

So If you create or export a file with the following columns, Name, Subnet,VlanID you are going to success.

Screen Shot 2013-09-21 at 18.00.22

And when running the function:

Screen Shot 2013-09-21 at 18.05.13

One thing that I wanted also was the possibilty to create just one network with the script and that has also been added.

Screen Shot 2013-09-21 at 18.07.10

The function also handles if you already had setup a network with the same subnet and gives you a warning and do not create that net (maybe you accidentally try to add the same net twice and that is being handled)

Screen Shot 2013-09-21 at 18.11.55

Here is the function

# Add Networks to VMM 
#
# Niklas Åkerlund 2013-09-16
<#
.Synopsis
   Add networks to the logical network and creates virtual networks to them
.DESCRIPTION
   this function can take either a csv file as input for creating virtual networks
.EXAMPLE
   Add-SCNetworks -importfile .\networks.csv
.EXAMPLE
   Add-SCNetworks -Name DMZ -Subnet 192.168.10.0/24 -VlanID 100 -VMHostGroup DMZHosts
.Notes
Niklas Akerlund 2013-09-16
#>
function Add-SCNetworks
{
    [CmdletBinding()]
    Param
    (
        # Logical Network Name
        [Parameter(Position=0)]
        $LogicalNetworkName = "LumaNets",
        
        [Parameter(ValueFromPipelineByPropertyName=$true)]
        [Microsoft.SystemCenter.VirtualMachineManager.LogicalNetwork]
        $LogicalNetwork,

        # Network site name
        [string]
        $LogicalNetDefName="VMnets",
        
        # CSV file to be imported
        [string]
        $ImportFile = " ",

        # Name of the VM network
        $Name,
        
        $Subnet,
        
        $VlanId,
        
        $VMHostGroup= "All Hosts"

    )

    Process
    {
        
    if(!$LogicalNetwork){
        $LogicalNet = Get-SCLogicalNetwork -Name $LogicalNetworkName
        # If the logical network not exist, create it
        if(!$LogicalNet){
            $logicalNet = New-SCLogicalNetwork -Name $LogicalNetworkName -LogicalNetworkDefinitionIsolation $true -EnableNetworkVirtualization $false -UseGRE $false -IsPVLAN $false
        }
        Write-Verbose $LogicalNet.Name
    }else{
        $LogicalNet = $LogicalNetwork
    }

    $allSubnetVlan = @()

    # The networks that is going to be imported
    if ($ImportFile -ne " "){
        $vlans = import-csv $ImportFile -Delimiter ";"
       

        foreach ($vlan in $vlans) {
            # Check that the actual network not already is configured
            if(!((Get-SCLogicalNetworkDefinition).SubnetVLans.Subnet -contains $vlan.Subnet)){

                # First in fabric 
                $LogicalNetDef = Get-SCLogicalNetworkDefinition -LogicalNetwork $LogicalNet -Name $LogicalNetDefName
                $allSubnetVlan = $LogicalNetDef.SubnetVLans    
                $Subnet = $vlan.Subnet 
                Write-Verbose $Subnet
                $SubnetVlan = New-SCSubnetVLan -Subnet $Subnet -VLanID $vlan.VlanID
                $allSubnetVlan += $SubnetVLAN
                if($LogicalNetDef){
                    Set-SCLogicalNetworkDefinition -LogicalNetworkDefinition $LogicalNetDef -SubnetVLan $allSubnetVlan
                }else {
                    $LogicalNetDef= New-SCLogicalNetworkDefinition -LogicalNetwork $LogicalNet -Name $LogicalNetDefName -SubnetVLan $allSubnetVlan -VMHostGroup $VMHostGroup
                }

                # Then create VM Networks
                $vmNetwork = New-SCVMNetwork -Name $vlan.Name -LogicalNetwork $LogicalNet -IsolationType "VLANNetwork"
                #$vmSubnet = 
                New-SCVMSubnet -Name $vlan.Name -LogicalNetworkDefinition $LogicalNetDef -SubnetVLan $SubnetVLAN -VMNetwork $vmNetwork
            }else{
                Write-Host "Subnet $Subnet already exists" -ForegroundColor Red
            }
        }
    }else{
        # add only a single network
            if(!((Get-SCLogicalNetworkDefinition).SubnetVLans.Subnet -contains $Subnet)){
            
                # First in fabric 
                $LogicalNetDef = Get-SCLogicalNetworkDefinition -LogicalNetwork $LogicalNet -Name $LogicalNetDefName
                $allSubnetVlan = $LogicalNetDef.SubnetVLans    
                #$Subnet = $IPnet + $Octet
                Write-Verbose $Subnet
                $SubnetVlan = New-SCSubnetVLan -Subnet $Subnet -VLanID $vlanId
                $allSubnetVlan += $SubnetVLAN
                if($LogicalNetDef){
                    Set-SCLogicalNetworkDefinition -LogicalNetworkDefinition $LogicalNetDef -SubnetVLan $allSubnetVlan
                }else {
                    New-SCLogicalNetworkDefinition -LogicalNetwork $LogicalNet -Name $LogicalNetDefName -SubnetVLan $allSubnetVlan -VMHostGroup $VMHostGroup
                }

                # Then create VM Networks
                $vmNetwork = New-SCVMNetwork -Name $Name -LogicalNetwork $LogicalNet -IsolationType "VLANNetwork"
                #$vmSubnet = 
                New-SCVMSubnet -Name $Name -LogicalNetworkDefinition $LogicalNetDef -SubnetVLan $SubnetVLAN -VMNetwork $vmNetwork
            }else{
                Write-Host "Subnet $Subnet already exists" -ForegroundColor Red
            }
    
    }
    }
}

It works on both VMM 2012 SP1 and VMM 2012 R2 (preview)

I have looked at Alvin Morales latest blog post about creating networks and will see if I can add some more functionality into my code later on, I think that using parameters instead is a better way. There is always improvements that can be done and the only problem is the time 😉

Comments

Leave a Reply to A gathering of links, part 3 | Hyper-V according to meCancel reply

name*

email* (not published)

website