Linking a VM and a Virtual Switch with PowerShell and Windows 8

Linking a VM and a Virtual Switch with PowerShell and Windows 8


So far I have discovered a physical NIC of my Hyper-V Server and created an External Virtual Switch (Have I ever mentioned how happy I am that MSFT calls them Virtual Switches now!?)

I have not blogged about creating a New-VM � frankly it is super simple.  Just type New-VM with no parameters and you have a VM.

If you do this you have a New Virtual Machine and all Hyper-V VMs have a Network Adapter by default. 

This kind of dictates the Verb that we use.  Since there is one we won�t Add, we need to change properties.  So we Get and then Set.  Or if you are feeling brash just Set.

The traditional way (the way I learned in .Net programming class) to make a change without causing extra harm is to Get, change your setting, and Set. 

Wait, hold the phone.  That does not apply here.  There is a Verb we don�t see much.  Connect.

Using Get-VMNetworkAdapter -VMName New* I get the vNIC of the VM.  And I need to capture that to an object.

$vmNic = Get-VMNetworkAdapter -VMName New*

In my last article I created a VMSwitch named �VMs�.  This is what I need to attach the VM to.  I cannot just modify the $vmNic.SwitchName, this is a ReadOnly property.

Connect-VMNetworkAdapter -VMNetworkAdapter $vmNic -SwitchName VMs

And, it is pretty flexible.  I fed in the VM vNIC object using �VMNetworkAdapter.  But I could also have used the �VMName (if it only has one vNIC) or pass in the VMSwitch object using �VMSwitch instead of �SwitchName

Also, New-VM also allows you to define a -SwitchName at creation time.  But that is not as universally applicable.



go to link download