Configure gateway metric (VBScript)

This code can be found in Chapter 1 of Windows Server 2003 Networking Recipes

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' From the book "Windows Server 2003 Networking Recipes"

' This code configured the local interface
'   with default gateway of 10.0.0.1,
'   and a metric of 1
' ------ SCRIPT CONFIGURATION ------
strComputer = "."
strGateway = Array("10.0.0.1")
strGatewayMetric = Array(1)
' --------- END CONFIGURATION ------

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set adapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

For Each a in adapters
    errGateways = a.SetGateways(strGateway, strGatewaymetric)
    If errGateways = 0 Then
        WScript.Echo "Success! The default gateway has been set."
    Else
        WScript.Echo "Error! The default gateway could not be set."
    End If
Next

This code has been viewed 2566 times.

New from the creators of TechTasks.com: StatSheet.com