— New online course!  Getting Started with Azure DevOps — Special launch price 50% off —

Powershell Script for Hyper-V to Wi-Fi NAT

by

One of the big hassles when you’re working with Hyper-V on a laptop is networking.  Specifically, getting your Hyper-V guest machines to get network access through your laptop’s wifi connection.

Well, Ben Armstrong blogged a new feature in Hyper-V that allows you to do NAT over wifi.  In his post, he pointed to the documentation on Microsoft’s site on how to configure this new NAT networking feature.  That documentation is a little hard to follow so I created a powershell script that puts all the steps together.

Here’s the script:

$switchName = ‘nat-switch’
$switchNetworkAdapterName = “vEthernet ($switchName)”
$natGatewayIpAddress = ‘192.168.5.1’
$natIpPrefixLength = 24
$natNetworkName = ‘nat-network’
$natNetworkInternalIPInterfaceAddressPrefix = ‘192.168.5.0/24’

New-VMSwitch -SwitchName $switchName -SwitchType Internal

Start-Sleep -Seconds 1

$newVirtualSwitch = Get-VMSwitch -Name $switchName

$virtualSwitchNetworkAdapter = Get-NetAdapter -Name $switchNetworkAdapterName

$newVirtualSwitchIfConfig = ($virtualSwitchNetworkAdapter).ifIndex

New-NetIPAddress -IPAddress $natGatewayIpAddress -PrefixLength $natIpPrefixLength -InterfaceIndex $newVirtualSwitchIfConfig

New-NetNat –Name $natNetworkName –InternalIPInterfaceAddressPrefix $natNetworkInternalIPInterfaceAddressPrefix

For this to work on your machines, you’ll need to adjust the variable values at the top of the script.  $switchName is the name of the new NAT-based switch that you’re going to create.  $natGatewayIpAddress is the IP address that you want your virtual machines to use as their IP gateway address.  $natNetworkName is the name of the hidden NAT network that is created on your Hyper-V host machine (aka. your laptop).  $natNetworkInternalIpInterfaceAddressPrefix is defines the IP address range that will be covered by NAT.  (HINT: $natNetworkInternalIpInterfaceAddressPrefix needs to relate to the value in $natGatewayIpAddress because it defines the subnet mask.)

Here’s a link to download the powershell script.

-Ben


One Response to "Powershell Script for Hyper-V to Wi-Fi NAT"

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: