Link to home
Start Free TrialLog in
Avatar of marrowyung
marrowyung

asked on

need to restart Windows serve AFTER add network route?

Hi,

I heard that once adding network route, the Windows server needs to reboot for the change to take effect, seems not making sense, am I right?

what command is need to make change of Windows server network take effect ?

SOLUTION
Avatar of kevinhsieh
kevinhsieh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marrowyung
marrowyung

ASKER

Route takes effect as soon as you add it.

some very good support tell me that and I doubt!
then how to make it permanent ?

tks.

this is the script by product support in powershell, I don't think it is permanent, right?

function Add-Routes
{   
    # 169.254.169.254 
    # 169.254.169.250 
    # 169.254.169.251 
    # 169.254.169.249 
    # 169.254.169.123 
    # 169.254.169.253 
    $ipAddrs = @("169.254.169.254/32", "169.254.169.250/32", "169.254.169.251/32", "169.254.169.249/32", "169.254.169.123/32","169.254.169.253/32")
    
    $sleepTime = 1
    $count = 0

    # Retry logic for querying primary network interface and adding routes.
    while($true)
    {
        try
        {
            Write-Log "Checking primary network interface"

            # If the current platform is not Nano, use this implementation for a better performance.
            if (-not (Test-NanoServer))
            {
                $networkAdapter = Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "AdapterTypeId='0' AND NetEnabled='True' AND NOT Name LIKE '%Hyper-V Virtual%' AND NOT Name LIKE '%Loopback Adapter%' AND NOT Name LIKE '%TAP-Windows Adapter%'" | Sort-Object -Property "InterfaceIndex" | select InterfaceIndex
                if(-not $networkAdapter -or $networkAdapter.Length -eq 0) 
                {
                    throw New-Object System.Exception("Failed to find the primary network interface")
                }
                $interfaceIndex = $networkAdapter[0].InterfaceIndex
                $networkAdapterConfig = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "InterfaceIndex='$interfaceIndex'" | select IPConnectionMetric, DefaultIPGateway

                # Filter for ipv4 (internetwork) addresses instead of ipv6 addresses (internetworkv6)
                [string[]] $defaultIPV4Gateway = @(FilterIPAddresses -Addresses $networkAdapterConfig.DefaultIPGateway -AddressFamily "InterNetwork")
                $defaultGateway = $defaultIPV4Gateway[$defaultIPV4Gateway.Length - 1]

                $interfaceMetric = $networkAdapterConfig.IPConnectionMetric
            }
            else
            {
                $ipConfigs = Get-NetIPConfiguration | Sort-Object -Property "InterfaceIndex" | select InterfaceIndex, IPv4DefaultGateway
                if(-not $ipConfigs -or $ipConfigs.Length -eq 0) 
                {
                    throw New-Object System.Exception("Failed to find the primary network interface")
                }
                $primaryIpConfig = $ipConfigs[0]
                $interfaceIndex = $primaryIpConfig.InterfaceIndex
                $defaultGateway = $primaryIpConfig.IPv4DefaultGateway.NextHop
                $interfaceMetric = 1
            }
            
            Write-Log "Primary network interface found. Adding routes now..."

            foreach ($ipAddr in $ipAddrs)
            {
                try
                {
                    Remove-NetRoute -DestinationPrefix $ipAddr -PolicyStore ActiveStore -Confirm:$false -ErrorAction SilentlyContinue
                    Remove-NetRoute -DestinationPrefix $ipAddr -PolicyStore PersistentStore -Confirm:$false -ErrorAction SilentlyContinue
                    New-NetRoute -DestinationPrefix $ipAddr -InterfaceIndex $interfaceIndex `
                        -NextHop $defaultGateway -RouteMetric $interfaceMetric -ErrorAction Stop
                    Write-Log ("Successfully added the Route: {0}, gateway: {1}, NIC index: {2}, Metric: {3}" `
                        -f $ipAddr, $defaultGateway, $interfaceIndex, $interfaceMetric)
                }
                catch
                {
                    Write-Log ("Failed to add routes: {0}" -f $ipAddr)
                }
            }
            
            # Break if routes are added successfully.
            break
        }
        catch
        {
            Write-Log ("Failed to add routes.. attempting it again {0}" -f $_.Exception.Message)
        }
        
        # It logs the status every 2 minutes.
        if (($count * $sleepTime) % 120 -eq 0) 
        { 
            Write-Log "Message: Failed to add routes.. attempting it again" -LogToConsole
        }

        Start-Sleep -seconds $sleepTime
        $count ++
    }
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
so the powershell script about create persistent route ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
tks all.