<#


.Synopsis


   Toggle internet on/off, but preserve LAN connection


.DESCRIPTION


    Requires local admin rights.

    

    If a route to 0.0.0.0/0 exists remove it. If it does not exist

    add it to the primary (or specified) interface with the assumed

    (or specified) gateway address as the NextHop.


.EXAMPLE


    InternetToggle.ps1 -DefaultGateway 10.0.0.1 -InterfaceIndex 19


.NOTES


   Author : "Andrew Schott"<andrew@joletec.com>

   version: 1.0

   Date   : 2021-06-10


#>




param(

    $DefualtGateway = "10.0.0.1",

    $InterfaceIndex = (Get-NetIPConfiguration).NetProfile.InterfaceIndex

)



TRY{

    Get-NetRoute -DestinationPrefix "0.0.0.0/0" -ErrorAction Stop

}CATCH{

    New-NetRoute -DestinationPrefix "0.0.0.0/0" -InterfaceIndex $InterfaceIndex -NextHop $DefualtGateway

    $RouteExisted = $false

}


IF($RouteExisted -eq $null){

    Remove-NetRoute -DestinationPrefix "0.0.0.0/0" -Confirm:$false

}