Learn about Windows PowerShell
Summary: Re-create network switches by using only Windows PowerShell.
Hey, Scripting Guy!
As part of our continuing disaster recovery tests, my organization would like to know if it is possible to re-create the Hyper-V virtual switches in an isolated location without using the GUI?
Honorary Scripting Guy, Sean Kearney here—filling in for our good friend Ed. I think he’s untangling some old VBScript scripts he found in the back room.
Note This is the third post in a series. To review, see:
Would it surprise you that the answer to your question is, “Yes, YES, and ab-so-JellyBaby-lutely YES!”
Last time, we found a virtual machine that we brought to our Hyper-V box called CONTOSOVM1 and it was missing the virtual switch called PublicNetwork.
I can get this issue patched up by simply using the New-VMSwitch cmdlet to create a virtual switch on a private network:
NEW-VMSWITCH –name ‘PublicNetwork’ –SwitchType Private
All this will do is allow us to import the virtual machine and start it. It’s not very useful, is it? With a name like PublicNetwork, I would think that it was probably publicly accessible?
Most smart Administrators create their virtual networks with names that make sense, such as:
And not names like “The Land of Bumble Bees and Flying Chickens.” (If you do this, you have to clearly document why you chose that name and what purpose it serves.)
So to make this useful to the rest of the network world, we need to know where to attach this switch. If you’re putting together things from scratch, you’ll need some knowns.
First let’s ensure that the only wire attached is the one you want to connect to and that it is physically attached to the back of the Hyper-V box. Then we can run one of the newer cmdlets to see which network adapters are live and attached (ideally, only the one):
This will show us all the network adapters on the computer in question and their status (up/down, MAC address)
Here’s info about my simple box with one detached network adapter:
To connect the virtual switch to a real network adapter, you need to know its descriptive name in Windows. That’s shown in the column on the left called Name when you access Get-NetAdapter.
If you know the name of the switch and the name of the virtual network that you want to create, use New-VMSwitch. The most typical scenario is to create the switch and not share it with the management operating system:
NEW-VMSwitch –NetAdapterName ‘Ethernet’ –Name ‘PublicNetwork’
Or if you have a smaller environment, and you need to run both off the same network adapter, use:
NEW-VMSwitch –NetAdapterName ‘Ethernet’ –Name ‘PublicNetwork’ –AllowManagementOS $TRUE
To determine which card you should attach to (remember I suggested only having the network wire you need attached?), check the Status of the card, which should not be Disconnected.
…of course, unless you’re like me, typing this blog post on my laptop.
If you already have a switch created (like the Private one we created earlier), you can connect it to the network adapter by using this command:
SET-VMSwitch –Name ‘PublicNetwork’ –NetAdapterName ‘Ethernet’ –AllowManagementOS $FALSE
So now the tricky part is out of the way (of course, presuming your network team has provisioned the back-end switches and routing). Tomorrow we’ll see about actually getting these virtual machines imported and operational in the Hyper-V environment.
I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Sean Kearney, Honorary Scripting Guy and Windows PowerShell MVP