I have always been looking for ways to simplify my Lab deployments and getting access to the Internet for my VM's and came across a simple solution using the NAT functionality within Hyper-V.
Creating the NAT Switch
Open Powershell and run the following commands.
- Creating the VMSwicth to be used for the NAT
New-VMSwitch -SwitchName "NATSwitch" -SwitchType Internal
- Now you can search for the Interface alias of the vSwitch you created.
Get-NetIPInterface -InterfaceAlias *NATSwitch*
- Once created you can create the NAT gateway address for the switch we just created.
New-NetIPAddress -IPAddress 192.168.0.254 -PrefixLength 24 -InterfaceAlias “vEthernet (NATSwitch)"
IPAddress -- NAT Gateway IP specifies the IPv4 or IPv6 address to use as the NAT gateway IP
PrefixLength -- NAT Subnet Prefix Length defines the NAT local subnet size (subnet mask)
InterfaceAlias-- The alias of the switch created in the first step
- Now we create the subnet to be used for the Internal network to be NAT
New-NetNat -Name NatNetwork -InternalIPInterfaceAddressPrefix 192.168.0.0/24
Now you will be able to allocate manually/automatically via DHCP to the VM's that are assigned to use this vSwitch with IP's in the created network and use the IP created in the NetIPAddress as the Default Gateway. Your VM's will now have access to the external resources.