Hey Checkyourlogs Fans,

For your viewing pleasure we have added a nice modification to our BigDemo scripts used to provision environments for our demos and books.

This time we have cleaned up a lot of the manual steps required to get routing working in our example labs in #Azure.

The Routing and NAT Switch is what we have fixed up this time.


Now, it is a bit tricky to get all this working properly but we have a working prototype:


Function Install-NetNat {
   param
  (
    [string]$VMName, 
    [string]$GuestOSName
  )

    Write-Output -InputObject "[$($VMName)]:: Configuring NAT on the Hyper-V Internal Switch `"$($env:computername)`""
    $CheckNATSwitch = get-vmswitch | where Name -eq $virtualNATSwitchName | Select Name

    If ($CheckNATSwitch -ne $null) {
    write-Host "Internal NAT Switch Found"}
    Else {
    
    write-Host "Not Found"
    Write-Host "Creating NAT Switch"

    New-VMSwitch -SwitchName $virtualNATSwitchName -SwitchType Internal 
    $ifindex = Get-NetAdapter | Where Name -like *$virtualNATSwitchName* |  New-NetIPAddress 192.168.10.1 -PrefixLength 24 
    
    Get-Netnat | Remove-NetNat -confirm:$false
    New-NetNat -Name $virtualNATSwitchName -InternalIPInterfaceAddressPrefix 192.168.10.0/24
               
    }
    }

Function Install-RRAS{
    param
    (
        [string] $VMName, 
        [string] $GuestOSName,
        [string] $IPAddress
    ) 

    Add-VMNetworkAdapter -VMName $VMName -SwitchName $virtualNATSwitchName

    Invoke-Command -VMName $VMName -Credential $domainCred {
    Write-Output -InputObject "[$($VMName)]:: Setting InternetIP Address to 192.168.10.254"


  
    $null = New-NetIPAddress -IPAddress "192.168.10.254" -InterfaceAlias 'Ethernet 2' -PrefixLength 24
    $newroute = '192.168.10.1'
    Write-Output -InputObject "[$($VMName)]:: Configuring Default Gateway"
    $null = Get-Netroute | Where DestinationPrefix -eq "0.0.0.0/0" | Remove-NetRoute -Confirm:$False
    #$null = Test-NetConnection localhost
    new-netroute -InterfaceAlias "Ethernet 2" -NextHop $newroute  -DestinationPrefix '0.0.0.0/0' -verbose
    $null = Get-NetAdapter | where name -EQ "Ethernet" | Rename-NetAdapter -NewName CorpNet
    $null = Get-NetAdapter | where name -EQ "Ethernet 2" | Rename-NetAdapter -NewName Internet
    Write-Output -InputObject "[$($VMName)]:: Installing RRAS"
    $null = Install-WindowsFeature -Name RemoteAccess,Routing,RSAT-RemoteAccess-Mgmt 
    #$null =  Stop-Service -Name WDSServer -ErrorAction SilentlyContinue
    #$null = Set-Service -Name WDSServer -StartupType Disabled -ErrorAction SilentlyContinue

    $ExternalInterface="Internet"
    $InternalInterface="CorpNet"
    Write-Output -InputObject "[$($VMName)]:: Coniguring RRAS - Adding Internal and External Adapters"
    $null = Start-Process -Wait:$true -FilePath "netsh" -ArgumentList "ras set conf ENABLED"
    $null = Set-Service -Name RemoteAccess -StartupType Automatic
    $null = Start-Service -Name RemoteAccess

     Write-Output -InputObject "[$($VMName)]:: Configuring NAT - Lab is now Internet Enabled"
    $null = Start-Process -Wait:$true -FilePath "netsh" -ArgumentList "routing ip nat install"
    $null = Start-Process -Wait:$true -FilePath "netsh" -ArgumentList "routing ip nat add interface ""CorpNet"""
    $null = Test-NetConnection 192.168.10.1
    $null = Test-NetConnection 4.2.2.2
    $null = cmd.exe /c "netsh routing ip nat add interface $externalinterface"
    $null = cmd.exe /c "netsh routing ip nat set interface $externalinterface mode=full"
    $null = Test-NetConnection 192.168.10.1
   # $null = Test-NetConnection $($Subnet)1
    $null = Test-NetConnection 4.2.2.2
     Write-Output -InputObject "[$($VMName)]:: Disable FireWall"
    $null = cmd.exe /c "netsh firewall set opmode disable"
      
    
    }

 

The real test for this is to see if we can get out to the Internet automatically once configured.


The code is up on github: https://github.com/dkawula/Deploying-System-Center-Virtual-Machine-Manager-2016

This is really handy because it cuts down on the time for me to write the next revisions of the books.

Happy Friday and happy learning everyone,

Dave Kawula – Founder MVPDays Publishing