Recently I’ve been trying to do some lab setup for a demonstration. One of the constant changes I do is creating and deleting VM’s in my laptop and creating various Network switches for VM’s. Now assigning a VM-Switch to a virtual machine can be easy task by using GUI but it can be painful it you’ve to go through each VM’s settings page and selecting the correct VM-Switch. You’ll get the idea if you’ve been managing more than 10-15 VM’s in your lab setup like I do
(My HYPER-V console in the laptop)
(VM-Switch I’ve created for various labs)
Now using PowerShell we can assign VM-Switch to selected number of VM’s in one parameter or to entire VM’s in my laptop in one go. Now this reminds me the famous quote in Spiderman movie “Great power comes with great responsibilities” Anyway we’ll use the power carefully in the PowerShell.
Now assuming I want to change only selected VM’s VM-Switch to “PrivateNetwork” I can run the PS command as follows,
Connect-VMNetworkAdapter -VMName Demo-WIN2008R2-DC,Demo-WIN2008R2-SRV1,DEMO-WIN2008R2-SRV2 –Switch PrivateNetwork
Assuming I want to change all VM’s VM-Switch name to “VMSW01” all I have to do is type the following command,
Connect-VMNetworkAdapter –VMName * –SwitchName VMSW01
Note: I found out in your VM-Switch naming convention if you keep spaces and use that VMM-Switch name in PowerShell it won’t work. This is something I yet to find out.