PowerShell

How to create a NAT-Switch on Hyper-V with PowerShell

NAT is a technology to convert IP addresses. If you have a test environment then it might be necessary that your VMs need an internet connection and that you don’t want to use the external switch for that. In this blog post I will cover this topis and show you how you can create a NAT-Switch in Hyper-V in less than 3 minutes. Let’s go.

The following code sample creates a NAT switch for the address range 192.168.99.0 /24. Please note that you need to adapt the code to your environment. Read the code sample carefully and fill in your desired netword range. Take also care of the network card id of your host network adapter which is ceratainly different from mine.

<#

Scenario

Hyper-V Host: 192.168.0.102 - Interface Index 88
VMs IP-Range: 192.168.99.0 /24

#>

# Create a new NAT-Switch

New-VMSwitch -Name NAT-Switch -SwitchType Intern

# Retrieve the Interface ID of your host netword card

Get-NetAdapter

# Configure the first address of 192.168.99.0 /24 on hosts netword card (Note the InterfaceIndex retrieved above)

New-NetIPAddress -IPAddress 192.168.99.1 -PrefixLength 24 -InterfaceIndex 88

# Activate NAT

New-NetNat -Name MyNAT -InternalIPInterfaceAddressPrefix 192.168.99.0/24

# Check NAT 

Get-NetNat

Have fun with your newly created Hyper-V network with NAT enabled.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

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