Azure DevOps, Scrum, & .NET Software Leadership and Consulting Services

Introducing Slide Speaker: Videos with Voice-over from your PowerPoint and Google Slides Presentations

Powershell Script for Hyper-V to Wi-Fi NAT


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

SUBSCRIBE TO THE BLOG


One response to “Powershell Script for Hyper-V to Wi-Fi NAT”

  1. Manuel Avatar

    It does not work. It only works with the ethernet working, if you are only on WIFI it does not give you internet

Leave a Reply

Your email address will not be published. Required fields are marked *

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