Change Virtual Machine Network Configuration with PowerShell
Summary: Learn how to use Windows PowerShell to change the virtual machine network configuration in Windows Server 2012.
Microsoft Scripting Guy, Ed Wilson, is here. If you are a seasoned Hey, Scripting Guy! Blog reader, you know that the most frequent guest blogger is Sean Kearney. If you are new to the blog, I welcome you, and I encourage you to catch up with Sean’s previous blogs.
Sean is a Windows PowerShell MVP and an Honorary Scripting Guy. Sean has been selected to present sessions called Integrating with Microsoft System Center 2012 and Windows PowerShell at TechEd NA and TechEd Europe this year. In his free time, Sean has written several blog posts about Hyper-V and some other cool stuff, and for a few weeks, Sean will be the designated guest blogger on Fridays. Take it away Sean…
The other weekend I had a pile of virtual machines on our Windows Server 2012 with Hyper-V box. We had created a new network configuration with a “teamed lan” that we wanted to take advantage of.
My co-worker and System Center mentor, Brad, looked over, “Sean, can you spend the afternoon and reconfigure the virtual machines to connect to the newly programmed Hyper-V network?”
I stared at him as if he just gave birth to a bag of corn chips. “Huh? An hour? I’ll bet you that I can do it in less than five minutes!”
Knowing that I know Windows Powershell, Brad was intrigued, “Prove it!”
I popped into the environment and ran the following cmdlet to see the names of the switches.
This produced some useful output in the Windows Powershell console—showing me all of the network switch names and configurations.
Now my challenge was to know which was the new and which was the old setup. I could look through the GUI console; but with Windows Powershell, I could have it tell me the same thing.
I pulled down the list of virtual machines with Get-VM and used a new cmdlet from Hyper-V, Get-VMNetworkAdapter. This cmdlet will show me the configuration of the network adapters within a virtual machine.
GET-VM | GET-VMNetworkAdapter
By looking at the list, I could see the old switch configuration that I was using was named “Hyper-V-Lan1.”
Making the change was so easy that my friend Brad’s eyes popped open just like Jim Carrey in “The Mask.” I simply invoked another new built-in cmdlet, Connect-VMNetworkAdapter. All I had to do was plug in the new network name I got from Get-VMSwitch.
GET-VM | GET-VMNetworkAdapter | Connect-VMNetworkAdapter –Switchname ‘New-cool-Hyper-V-Lan’
I looked up with a big goofy grin on my face, “Done! I’ll see you tomorrow.”
Brad blinked, “Tomorrow?”
“You just asked me to spend the afternoon. I did. I just spent it in a more efficient method!”
The best part is knowing this: I could have just as easily switched the configuration on thousands of machines in a very similar time frame.
Feel the Power within you.
The Energized Tech
Way cool stuff, Sean. Join Sean next week for more Windows PowerShell and Hyper-V stuff. Join me tomorrow for the Weekend Scripter.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy