phiban Posted October 16, 2006 Share Posted October 16, 2006 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 More sharing options...
gunsmokingman Posted October 16, 2006 Share Posted October 16, 2006 (edited) 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 IPstrComputer = "."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 October 16, 2006 by gunsmokingman Link to comment Share on other sites More sharing options...
phiban Posted October 16, 2006 Author Share Posted October 16, 2006 Thanks gunsmokingman! this was perfect thanks for digging it up for me!Although I do have one question (A stupid one) for you 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 More sharing options...
allen2 Posted October 16, 2006 Share Posted October 16, 2006 You can also do it with the netsh.exe utility. Link to comment Share on other sites More sharing options...
LLXX Posted October 17, 2006 Share Posted October 17, 2006 Thanks gunsmokingman! this was perfect thanks for digging it up for me!Although I do have one question (A stupid one) for you 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 More sharing options...
CoffeeFiend Posted October 17, 2006 Share Posted October 17, 2006 (edited) . Edited November 7, 2006 by crahak Link to comment Share on other sites More sharing options...
JoeMSFN Posted October 17, 2006 Share Posted October 17, 2006 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 More sharing options...
Stealth111 Posted October 21, 2006 Share Posted October 21, 2006 (edited) 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)NextSeemed to work, but I wanted someone to correct me if I am missing something.. Edited October 21, 2006 by Stealth111 Link to comment Share on other sites More sharing options...
gunsmokingman Posted October 22, 2006 Share Posted October 22, 2006 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 More sharing options...
Stealth111 Posted October 22, 2006 Share Posted October 22, 2006 I guess I will stay with what I came up with, I tested it in 3 different enviornments in 3 different buildings and it seemed to work exactly as I needed it to, but thanks for the offer anyways!! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now