Create an external VM switch in Hyper-V

This morning I wanted to quickly add an External Hyper-V switch in my lab.
I’ve already added an Internal switch but as I needed to get access to Internet, an External switch was required.

Without reading the help, I naturally did:

New-VMSwitch -Name External -SwitchType External

Even if the External value was technically allowed by tab completion, the following error reminded me how stupid was my above assumption.
New-VMSwitch : Cannot validate argument on parameter ‘SwitchType’. The argument “External” does not belong to the set
“Internal,Private” specified by the ValidateSet attribute. Supply an argument that is in the set and then try the
command again.

Of course, to get access to Internet or to the same network as the physical nic, the External switch needs to be bound to a physical network card!

To help also states:

-SwitchType
Specifies the type of the switch to be created. Allowed values are Internal and Private. To create an External
virtual switch, specify either the NetAdapterInterfaceDescription or the NetAdapterName parameter, which
implicitly set the type of the virtual switch to External.

Then I remembered that I already did this operation some months ago where I had only one physical adapter:

New-VMSwitch -Name Prod -NetAdapterName ((Get-NetAdapter|            
? Status -eq "Up")[0].Name)

I couldn’t use exactly the same above command I previously used because the firt item returned by the filtered Get-NetAdapter” cmdlet was the Internal switch I previously set.

As you can see, filtering with the ‘Status’ wasn’t enough. I decided to compare the properties between the 2 network cards instances returned by the Get-NetAdapter cmdlet.

I quickly did…

$a =  (Get-NetAdapter  | ? Status -eq "Up")[0]            
$b =  (Get-NetAdapter  | ? Status -eq "Up")[1]            
Compare-Object -ReferenceObject $a -DifferenceObject $b

…and couldn’t get the result I expected 😦

Comparing instances of WMI objects isn’t as trivial as it seems to be. Here’s the quick and dirty thing I did:

$a =  (Get-NetAdapter  | ? Status -eq "Up")[0]            
$b =  (Get-NetAdapter  | ? Status -eq "Up")[1]            
# Fill-in an array with properties that have a value            
$props = @()            
($a | gm -MemberType Property).Name | % {            
    if (($a.$_)|Out-String) {             
        $props += $_             
    }            
}            
# Compare properties            
$props | % {             
 Compare-Object -Ref $a -Diff $b -Property $_ | Out-String            
}

After that, I found the NdisPhysicalMedium that seems promising and able to differentiate the Internal switch and the physical Nic of the Hyper-V host.
I found the meaning of the different values on http://www.powershellmagazine.com/2013/04/04/pstip-detecting-wi-fi-adapters

To install my External VM switch, I just extended the filter and did:

New-VMSwitch -Name External -NetAdapterName (            
Get-NetAdapter |?{            
 $_.Status -eq "Up" -and $_.NdisPhysicalMedium -eq 14             
 }).Name

Advertisements

One thought on “Create an external VM switch in Hyper-V

  1. This works if you only have one NIC
    $NAPhysical = Get-NetAdapter -Physical
    $NAPhysical.Name #Don’t need this line but I use for testing”
    New-VMSwitch -Name “VMExternal” -NetAdapterName $NAPhysical.Name

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s