Free Script: Network Team and Virtual Switch Manipulation for Hyper-V 2012

Table of contents

I’ve designed a PowerShell script to help you with network team and virtual switch management in Hyper-V Server 2012. It will also work for Windows Server 2012.

Download the script file: SwitchesAndTeams

This will work whether or not you have a GUI version of Windows Server 2012 or if it’s Hyper-V Server 2012.

Features

  • Create and delete network teams
  • Set IP information on the primary team adapter
  • Create and delete virtual switches
  • Create and delete virtual adapters
  • Set IP information on management OS virtual adapters
  • Set the VLAN ID for management OS virtual adapters
  • New in 1.1: Show existing teams, switches, and virtual adapters

The script is entirely menu-driven.

Usage

  1. Download the ZIP file. In Windows Explorer, right-click it and go to Properties. Click the “Unblock” button. Alternately, use the Unblock-File  PowerShell cmdlet.
  2. Extract the contents, or at least the SwitchesAndTeams.ps1 and SwitchesAndTeams.psd1 files to the system where you want to work with virtual switches and network teams. This script does not operate remotely.
  3. At the console of that system run PowerShell.exe. Then, in the folder that you extracted the files to, run .SwitchesAndTeams.ps1  to get started. Don’t forget that you can use [Tab] autocompletion so that you don’t have to type the entire name.

Purpose

The primary purpose is to make it a bit easier to manipulate these items, especially in a non-GUI environment. Even the GUI has no method for creating or removing virtual adapters within the management operating system. The secondary purpose is to provide examples of how these items can be manipulated using PowerShell.

Enjoy!

 

Altaro Hyper-V Backup
Share this post

Not a DOJO Member yet?

Join thousands of other IT pros and receive a weekly roundup email with the latest content & updates!

17 thoughts on "Free Script: Network Team and Virtual Switch Manipulation for Hyper-V 2012"

  • Nick says:

    Hello, i just wanted to thank you for the script! One suggestion i have would be to add the ability to show existing teams, netadapters, vmnetworkadapters, and vmswitches. Offering that plus the ability to do with -managementos would be really awesome, since now if you don’t remember all of the networks you have in a complicated converged design, you have to open another powershell window to get the data. Otherwise it’s really awesome and will definitely save me some time.

    • Eric Siron says:

      Thanks!
      I think your suggestion is dead-on. It hadn’t completely slipped my mind, but getting the display to work as I wanted was severely delaying completion of the script so I went ahead without it. Watch for a forthcoming update that introduces that enhancement.

    • Eric Siron says:

      1.1 has been uploaded with changes to address this. I’ll admit I cheated on the team display, but the wheel looked round enough to me.

  • setting up a basic switch it failed because my nics are named ethernet 1,ethernet 2… I think the space threw it off and your script needs “” around the nic name. I worked that way when I added it manually

  • Thanks for fixing it I will try it out, I was very exicted to see someone who could do the menu driven powershell script. Maybe you can blog about how to do that, as I would love to learn that skill set. Also check out my blog to some scripts like, getting a detailed report/inventory of your vhost and VM’s and creating a vm. I am currently looking into a vmconnect.exe replacement called freeRDP and loading it with powershell. I am trying to specialize in the free core server and manage it almost like the GUI but with all free APPS/scripts. That is why I really want to learn powershell and scripting like you are doing that is menu driven.

  • New version works great.

  • Love your articles and scripts… Looking for a heavy hitter to join my codeplex projects. Hoping you are interested. Project 1 is reporting.codeplex.com it is a very simple script ( by your coding standards) that gets you a detailed inventory report of the VHOST and it’s VM’s for Free Core and datacenter standalone 2012 servers. Project 2 (yet to kick off) is http://stephanco.blogspot.com/2013/06/building-hyper-v-vhost-without-buying.html… it is a amalgum of batch files and powershell scripts.. yes even your Switches and teams… that allows you to configure and manage a Free Core 2012 server without having to have a pay for 3rd party app like vt utilities, 5Nine manager or windows 8 / Server 2012 GUI to Hyper-v Manager. It is not as clean as i would like it but it works. There is a replacement for vmconnect.exe (which does not come with free core server) an open source app called wfreeRDP.exe. You have to have this or you can not connect to a VM that has not IP yet. Please let me know if you are interest in joining either or both of my porjects. i clould use a coder with your talents. Thanks for all you hard work I have benefited greatly from your sharing and knowledge.

    • Eric Siron says:

      Well, thanks! I’m honored to be asked. At this time, I’m in the middle of two major projects, one of which is a bottom-up rework of the Corefig project, which seems to have some overlap with what you’re doing. The other project is consuming most of my time, but we can circle back at a later time.

  • AI have written a very simple powershell script to add vswitch, now where near the polished code you write, but it does simplify the NIC selection. In your code it brings up the alias name and you have to figure out what phyical nic that is.

    This code take the guess work out of it, for a simple vswitch that is to use the smae NIC and IP as your VHOST management NIC or if you only have one NIC.

    $VHOST = $Env:COMPUTERNAME
    $vhost
    get-vmswitch
    Write-Host -foreground “magenta” “”
    Write-Host -foreground “magenta” “If there are no vSwitches shown, you have none built. ”
    Write-Host -foreground “magenta” “”

    #Get the NIC aliases and Ip addresses
    Write-Host -foreground “magenta” “Listing of your NIC’s and IP addresses”
    Get-NetIPAddress | Where-Object {$_.ipaddress -like “*.*.*.*”} | select interfacealias, ipaddress | format-table -autosize

    #Ask for input
    $SelectedIndex = Read-Host “Type 0 to exit out of this Menu or Enter to Continue”
    $Exit = $SelectedIndex

    if($EXIT -eq 0)
    {exit}
    else {
    $SelectedIndex1 = Read-Host “Type the Interface Alias name that is next to your VHOST’s IP Address”
    $SelectedIndex2 = Read-Host “Type the vSwitch Name”

    $NIC = $SelectedIndex1
    $VSWITCH = $SelectedIndex2

    Write-Host -foreground “magenta” “”
    Write-Host -foreground “magenta” “Creating the vSwitch called “$VSWITCH” on the NIC named “$NIC” – Be patient this takes approx 90 seconds.”
    New-VMSwitch -Name “$VSWITCH” -NetAdapterName “$NIC” -AllowManagementOS $True
    get-vmswitch}

    For a newbie using this code should be simple, your code asks questions I still do not know the real answer too but I just got lucky with my guesses. Hope this helps. someone out there.

  • If you want some suggestions of items to change/add to corefig I have a lot. Most of which are in my projects and or blog. I would love to donate my scripts and or knowledge. Contact me at [email protected] I think corefig could be really great with the right add-ons.

  • tom miller says:

    Eric,
    Great script! Just a minor comment: When replying to yes or no, sometimes you have to enter YES or NO, other times you enter Y or N. Be cleaner if it’s one way or another for the entire script.

    Thanks for the hard work.
    Tom

    • Eric Siron says:

      Thanks for the heads-up. Inconsistency certainly wasn’t intentional. In all cases, the inputs should only check to see if there is a “y” or an “n” for the first character. I’ll give it a review.

  • DSN says:

    excellent work dude, works a charm!!

  • Pedro da Silva says:

    Thank you for this script. A question that is bugging me, though. How do the load balancing algorithms in the script map to the GUI? For example, in LACP mode, the script lists Hyper-V Ports, Transport Ports, IP Addresses and MAC Addresses. The first I can understand. But which one will map, say, to the Dynamic Mode we can select using the GUI?

    • Pedro da Silva says:

      Ok, I can now see the Dynamic mode that was introduced with Windows 2012 R2 is not supported in this script.

Leave a comment or ask a question

Your email address will not be published. Required fields are marked *

Your email address will not be published.

Notify me of follow-up replies via email

Yes, I would like to receive new blog posts by email

What is the color of grass?

Please note: If you’re not already a member on the Dojo Forums you will create a new account and receive an activation email.

cyber security roundtable