Jump to content

setting a static IP with a Batch File/vbs


Recommended Posts

Hey guys,

Is it possible to set a static IP on a computer using a Batch File or VB script?

Basically I want to run a script, which will configure the machines IP, Gateway etc, would the batch or vbs edit the registry if so could you show me how?

Also could I retrive this information at a later stage in the same way??

Thanks in advance

Link to comment
Share on other sites


Go to this Link and see if any of the scripts there will help you. Look for the Network section on the left side of the page.

Here is a script from there for static IP

strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration " _
& "where IPEnabled=TRUE")
strIPAddress = Array("192.168.1.141")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.100")
strGatewayMetric = Array(1)

For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic( _
strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(_
strGateway, strGatewaymetric)
Next

Edited by gunsmokingman
Link to comment
Share on other sites

Thanks gunsmokingman! this was perfect :D thanks for digging it up for me!

Although I do have one question (A stupid one) for you :blushing:

How can the sql select statement work if I dont have sql installed on the machine, is there a "hidden / OS" sql service running?

Thanks!

Link to comment
Share on other sites

Thanks gunsmokingman! this was perfect :D thanks for digging it up for me!

Although I do have one question (A stupid one) for you :blushing:

How can the sql select statement work if I dont have sql installed on the machine, is there a "hidden / OS" sql service running?

Thanks!

Not really a full SQL, but a database that is accessed in much the same way.
Link to comment
Share on other sites

How awsomely almost timely for me! We were having DSL problems and the ISP was having me switch between DHCP and static IPs (not a lot, but it's easier to run a script than right click/properties a bunch of times etc.).

I will have to remember this for later. Thanks phiban for the question that lead to gunsmokingman's answer!

Link to comment
Share on other sites

Hey gun, thanks for the info, can you also add a bit to assign the dns also at the same time in the script?? It would be great for me to force the dns that I want coming out of dhcp. My domain setup gets a different dns from our dhcp server, different than when I statically assign ip's, as we have 2 different ISP's in the building..

Would it be a metter of just adding :

strDNSservers = Array ("192.168.177.200")

and add this :

errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNSServers)

Final Script including dns server entry??

strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration " _
& "where IPEnabled=TRUE")
strIPAddress = Array("192.168.1.141")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.100")
strGatewayMetric = Array(1)
strDNSservers = Array("192.168.1.2")

For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic( _
strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(_
strGateway, strGatewaymetric)
errDNS = objNetAdapter.SetDNSServerSearchOrder(_
strDNSServers)
Next

Seemed to work, but I wanted someone to correct me if I am missing something.. :)

Edited by Stealth111
Link to comment
Share on other sites

Is this your fianl code or did you want me to try and write what you wanted. Remeber I have no network to test the script on.

strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration " _
& "where IPEnabled=TRUE")
strIPAddress = Array("192.168.1.141")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.100")
strGatewayMetric = Array(1)
strDNSservers = Array("192.168.1.2")

For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic( _
strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(_
strGateway, strGatewaymetric)
errDNS = objNetAdapter.SetDNSServerSearchOrder(_
strDNSServers)
Next

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...