Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change a default gateway automaticaly

Status
Not open for further replies.

SimonMag

Technical User
Mar 20, 2001
123
GB
I have the following problem:

We have arround 100+ PC's on the network that are configured for DHCP, but have a manual gateway address assigned via the 'Advanced' tab under TCP/IP settings, this is overiding the gateway address assigned via DHCP. I need to change this address. This is where the problem lies. I have looked in the registry on a couple of PC's and the tcp/ip settings are under a unique identifier for each lan adapter, so I can't search for a specific registry key as they will all be different.

Is there a way that anyone knows of to be able to change this gateway address?

Here is a typical entry from the registry, in bold is the identifier that is unique for eack lan adapter:

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\{3C4749C4-01BC-43B5-BABE-3DD5AE5D3979}\Parameters\Tcpip]
"EnableDHCP"=dword:00000001
"IPAddress"=hex(7):30,00,2e,00,30,00,2e,00,30,00,2e,00,30,00,00,00,00,00
"SubnetMask"=hex(7):30,00,2e,00,30,00,2e,00,30,00,2e,00,30,00,00,00,00,00
"DefaultGateway"=hex(7):00,00
"DhcpIPAddress"="172.20.24.121"
"DhcpSubnetMask"="255.255.0.0"
"DhcpServer"="172.20.0.6"
"Lease"=dword:0003f480
"LeaseObtainedTime"=dword:4755007a
"T1"=dword:4756faba
"T2"=dword:4758766a
"LeaseTerminatesTime"=dword:4758f4fa
"DhcpDefaultGateway"=hex(7):31,00,37,00,32,00,2e,00,32,00,30,00,2e,00,31,00,2e,\
00,31,00,00,00,00,00
"DhcpSubnetMaskOpt"=hex(7):32,00,35,00,35,00,2e,00,32,00,35,00,35,00,2e,00,30,\
00,2e,00,30,00,00,00,00,00
 
Would this help

Code:
strComputer = "."
strGateway = Array("10.0.0.1")
strGatewayMetric = Array(1)

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
 
Thanks for that, it has given me something to think about, but I only need the script to run if the gateway address is equal to 10.10.10.1 and if it is to remove the default gateway altogether.

Thanks

Simon.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top