SCVMM 2012 R2 – Scripted deployment of Hyper-V networking

For those folks that have worked with SCVMM networking for Hyper-V know it’s not the most intuitive thing out there and certainly not easy to throw on multiple hosts. Now I needed to deploy networking on dozens of hosts, a script was definitely needed.

Now a word of caution – the script is not dynamic in the sense that it would not work properly if anything changed in the count of logical switches on the reference host or the uplink profile/virtual network adapter port profiles. I do not anticipate our environment to change any time soon, so this works for me. I am working on making it as dynamic as I’d like it to be so you don’t have to worry about changes in networking on the reference host. Till then, you’ll need to tweak the script to make it keep working for you.

Note the following items too:

  • The script works asynchronously, so it cycles through the hosts in the $csvname file one after the other. I am trying to find a way to get it going synchronously to reduce the time it takes to run. When I ran it for my environment, it ran for about 2 hours (too long I know!)
  • It uses the idea of Job Groups so as to ensure the individual switches and/or vNICs were created one after the other. I found the script wouldn’t produce desired outcomes without Job Groups. The Job Groups IDs are just random numbers, you can use whatever in there but they have to 32 digits in there with exactly the same number of hyphens and digits within the hyphens
  • I’ve made it Sleep on some occasions because I discovered some hosts took a bit longer to get the management configured. You can of course vary the Sleep duration to suit you
  • The script assumes the IP settings are on the right NICs and done correctly. The only NIC that should have any IP addresses and/or gateway is one of your host’s management adapters. If you don’t ensure this, it’ll fail to deploy the management switch properly
  • The name of the reference host can be either the FDQN or the un-qualified name, it does not matter
  • It does not confirm if the switching has been successfully created, I found it added to the complexity and the time it takes to run. After it finishes, check the Jobs window for errors or go to Hosts > Properties > Virtual Switches and check what got created
  • It assumes you have created an IP pool for dishing out IP addresses for the Live Migration vNIC. The script automatically obtains the name of the IP pool from the reference host, stores the name in a variable and gets SCVMM to give the Live Migration vNIC an IP address
  • It does not need the $csvname file to be in the same location as the script
  • Finally, it assumes you have a connection to the SCVMM instance managing the hosts you need to deploy networking on. If you don’t have a connection, either logon to the SCVMM machine or better – just establish a connection using the Get-SCVMMServer cmdlet, supply administrative credentials and you are good to go

This script will evolve over time to make it smarter, more dynamic and simultaneously work for all hosts in the $csvname file. Come back a bit later and you’ll likely see it updated. Hopefully this make someone’s life easier!


Clear-Host
Write-Host "Ensure no NICs other than one Management NIC has any IP addressing and GATEWAYS before proceeding"
Write-Host ""
Sleep 10

$ReferenceHost = Read-Host "Enter name of reference host"
$SCVMReferenceHost = Get-SCVMHost -ComputerName $ReferenceHost
$SCVN = Get-SCVirtualNetwork -VMHost $SCVMReferenceHost
$HostVirtualNetworkAdapters = Get-SCVirtualNetworkAdapter -VMHost $ReferenceHost | Sort-Object -Descending

Write-Host ""
Write-Host "Gathering host networking information.." -ForegroundColor Green
Write-Host ""
Sleep 5
Write-Host "This host is connected to Logical Switch(es):" $SCVN.LogicalSwitch -ForegroundColor Green | select Name -Unique
Write-Host ""
Write-Host "This host is connected to Logical Network(s):" $SCVN.LogicalNetworks -ForegroundColor Green
Write-Host ""
Write-Host "This host is assigned Port Classification(s):" $HostVirtualNetworkAdapters.PortClassification -ForegroundColor Green
Write-Host ""
Write-Host "This host is assigned Virtual Network Adapter(s):" $HostVirtualNetworkAdapters.Name -ForegroundColor Green
Write-Host ""
Write-Host "Hosts listed in .csv file you've entered will now be configured with networking based on reference host:" $SCVMReferenceHost -ForegroundColor Gray
Write-Host ""

$LogicalSwitches = $SCVN.LogicalSwitch | Sort-Object -Descending

$csvName = Read-Host "Provide the path to .csv file containing host list..."
If([string]::IsNullOrEmpty($csvName))
{
    Write-Host "Invalid CSV file name...exiting" -ForegroundColor Red
    exit
}
Else
{
    # Importing host list
    Write-Host "Importing CSV..." -ForegroundColor Cyan
    Write-Host ""
    Write-Host "This script will now automatically deploy network configuration on hosts listed in the file supplied by you"
    Write-Host ""
    $HostNames = get-content -path $csvName
    }
Sleep 5

Write-Host "Kick back and relax!" -ForegroundColor Yellow
Write-Host ""

foreach ($HostName in $HostNames){
        
        Write-Host "Checking host to see if CredSSP is enabled. CredSSP allow SCVMM to delegate your user credentials to Hyper-V hosts"

        Write-Host ""
        $HostCredSSP = Invoke-Command -ComputerName $HostName -ScriptBlock {Get-Item  WSMan:\localhost\Client\Auth\CredSSP}

            If ($HostCredSSP.value -eq "false") {Write-Host "CredSSP is not enabled, enabling.. This can take 30s - 1min"
                Enable-WSManCredSSP Client -DelegateComputer $HostName -Force
                Sleep 30}
            Else {
                "CredSSP is already enabled, skipping.."}
        
        Write-Host ""

        ## Creating Virtual Machine traffic switch first ##

        Write-Host "Creating virtual machine networking" -ForegroundColor Green

        #### JobGroup section ONE start ####
        
        $VMHost = Get-SCVMHost -ComputerName $HostName
        $NetworkAdapter = Get-SCVMHostNetworkAdapter -VMHost $VMHost | Where {$_.ConnectionName -eq "$HostName-VM2"}
        $VMUplinkPortProfileSet = Get-SCUplinkPortProfileSet | Where {$_.LogicalSwitch -eq $LogicalSwitches[0]}
        Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $NetworkAdapter -UplinkPortProfileSet $VMUplinkPortProfileSet -JobGroup "8da7daeb-9974-42d9-a63d-8758c3327152"
        $NetworkAdapter = Get-SCVMHostNetworkAdapter -VMHost $VMHost | Where {$_.ConnectionName -match "$HostName-VM1"}
        Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $NetworkAdapter -UplinkPortProfileSet $VMUplinkPortProfileSet -JobGroup "8da7daeb-9974-42d9-a63d-8758c3327152"
        $NetworkAdapter = @()
        $AdapterVM2 = Get-SCVMHostNetworkAdapter -VMHost $vmHost | Where {$_.ConnectionName -match "$HostName-VM2"}
        $NetworkAdapter += $AdapterVM2
        $AdapterVM1 = Get-SCVMHostNetworkAdapter -VMHost $vmHost | Where {$_.ConnectionName -match "$HostName-VM1"}
        $NetworkAdapter += $AdapterVM1
        New-SCVirtualNetwork -VMHost $VMHost -VMHostNetworkAdapters $NetworkAdapter -LogicalSwitch $LogicalSwitches[0] -JobGroup "8da7daeb-9974-42d9-a63d-8758c3327152"
        Set-SCVMHost -VMHost $VMHost -JobGroup "8da7daeb-9974-42d9-a63d-8758c3327152" -RunAsynchronously
        
        #### JobGroup section ONE end ####

        ## Creating Host Management traffic switch next ##

        Write-Host "Creating Host Management networking" -ForegroundColor Green

        #### JobGroup section TWO start ####

        $VMHost = Get-SCVMHost -ComputerName $HostName
        $HostLogicalNetworks = $SCVN.LogicalNetworks | Sort-Object -Descending
        $NetworkAdapter = Get-SCVMHostNetworkAdapter -VMHost $VMHost | Where {$_.ConnectionName -match "$HostName-MGT1"}
        $HostMgmtUplinkPortProfileSet = Get-SCUplinkPortProfileSet | Where {$_.LogicalSwitch -eq $LogicalSwitches[1]}
        Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $NetworkAdapter -UplinkPortProfileSet $HostMgmtUplinkPortProfileSet -JobGroup "039adfb3-8e78-458d-b44f-eb741fd23424"
        $NetworkAdapter = Get-SCVMHostNetworkAdapter -VMHost $VMHost | Where {$_.ConnectionName -match "$HostName-MGT2"}
        Set-SCVMHostNetworkAdapter -VMHostNetworkAdapter $NetworkAdapter -UplinkPortProfileSet $HostMgmtUplinkPortProfileSet -JobGroup "039adfb3-8e78-458d-b44f-eb741fd23424"
        $NetworkAdapter = @()
        $AdapterMGT1 = Get-SCVMHostNetworkAdapter -VMHost $VMHost | Where {$_.ConnectionName -match "$HostName-MGT1"}
        $NetworkAdapter += $AdapterMGT1
        $AdapterMGT2 = Get-SCVMHostNetworkAdapter -VMHost $VMHost | Where {$_.ConnectionName -match "$HostName-MGT2"}
        $NetworkAdapter += $AdapterMGT2
        $VMNetwork = Get-SCVMNetwork -LogicalNetwork $HostLogicalNetworks[1] | Sort-Object -Descending
        $HostVirtualNetworkAdapters = Get-SCVirtualNetworkAdapter -VMHost $ReferenceHost | Sort-Object -Descending
        New-SCVirtualNetwork -VMHost $VMHost -VMHostNetworkAdapters $NetworkAdapter -LogicalSwitch $LogicalSwitches[1] -JobGroup "039adfb3-8e78-458d-b44f-eb741fd23424" -CreateManagementAdapter -ManagementAdapterName $HostVirtualNetworkAdapters[0] -ManagementAdapterVMNetwork $VMNetwork[0] -ManagementAdapterPortClassification $HostVirtualNetworkAdapters[0].PortClassification
        Set-SCVMHost -VMHost $VMHost -JobGroup "039adfb3-8e78-458d-b44f-eb741fd23424" -RunAsynchronously

        Sleep 210

        #### JobGroup section TWO end ####

        ## Long sleep duration as it can take some time to create networking ##

        Write-Host "Waiting for host to re-establish connectivity with SCVMM as it momentarily drops when the above networking is being configured" -ForegroundColor Green
        Write-Host ""

        ## Creating Live Migration virtual adapter last ##

        Write-Host "Creating Live Migration networking" -ForegroundColor Green
        Write-Host ""

        #### JobGroup section THREE start ####

        $LiveMigNetwork = Get-SCVMNetwork -LogicalNetwork $HostLogicalNetworks[3]
        $LiveMigLogicalNetDef = Get-SCLogicalNetworkDefinition -LogicalNetwork $HostLogicalNetworks[3]
        $LiveMigStaticAddressPool = Get-SCStaticIPAddressPool -LogicalNetworkDefinition $LiveMigLogicalNetDef

        New-SCVirtualNetworkAdapter -VMHost $VMHost -Name $HostVirtualNetworkAdapters[1].Name -VMNetwork $LiveMigNetwork -LogicalSwitch $LogicalSwitches[1] -JobGroup "039adfb3-8e78-458d-b44f-eb741fd23425" -VLanEnabled $true -VLanID $HostVirtualNetworkAdapters[1].VLanID -PortClassification $HostVirtualNetworkAdapters[1].PortClassification -IPv4AddressType "Static" -IPv4AddressPool $LiveMigStaticAddressPool -MACAddressType "Static" -MACAddress "00:00:00:00:00:00"
        Set-SCVMHost -VMHost $VMHost -JobGroup "039adfb3-8e78-458d-b44f-eb741fd23425" -RunAsynchronously

        #### JobGroup section THREE end ####

        Write-Host "Host networking created, check Jobs window for information" -ForegroundColor Green

}

Leave a Comment

Your email address will not be published.