The lazy programmer scripts
scripts that work

Lazy create Windows NAT adapter GitHub

This script works on PowerShell >= 4.

Tested on Windows 10.

#ps1
$switchName = "VMInternalSwitch"
$natNetwork = "NATNetwork"
$subnet = "192.168.137.0/24"

# Create VM Switch
New-VMSwitch -SwitchName $switchName -SwitchType Internal

# Get VMswitch associated net adapter index
$index = (Get-NetAdapter -Name "*($switchName)").IfIndex

# Create the gateway
New-NetIPAddress -IPAddress (($subnet -split "/")[0]).replace(".0",".1") -PrefixLength (($subnet -split "/")[1]) -InterfaceIndex $index

# Get the nat network and remove if it exists -- needs confirmation
Get-NetNat | Remove-NetNat -Confirm:$true

# Create and configure the NAT Network
New-NetNat -Name $natNetwork -InternalIPInterfaceAddressPrefix $subnet

That's all, folks!

Written by kami on Monday August 14, 2017
Permalink - Tags: powershell, windows, networking

Leave a comment

comments powered by Disqus

« Lazy create associative array-like construction in Bash - Lazy lsof for Windows »