All Forums >> [Scripting] >> WSH & Client Side VBScript >> Script to switch between Static IP and DHCP Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
I am working on a script that is an adaptation of something I found in Microsoft's script center. It is really pretty simple. You enter "cscrip", the name/path of the script, and 3 arguments into a command propmt. This will either enable DHCP on all network adapters or set a static IP based on the arguments you send. Example...
cscript setstaticip.vbs true enables DHCP on all network adapters
cscript setstaticip.vbs false 172.17.0.15 172.17.0.254 sets static ip with 2nd argument, and gateway with 3rd argument ...it also sets subnet mask and dns address, but those are constants in my case (it would be easy to change though)
I have this script called by a VB program to make it easy to use.
The thing works like a charm on active network connections, but I have noticed one thing...
If you do not have the network cable connected properly then it won't make the changes. I, at the very least, need it to be able to enable DHCP on all network adapters, even those with "network cable uplugged."
The errors I get for these adapters are... Enable DHCP: 94(Path, file, or object not found) Set Static IP: 81(Unable to configure DHCP service)
Anyway, I could really use some help on this one.
Thanks a ton!
Matt "WarHorse"
Here is the code. Try it for youself. If things start messing up you can fix it by manually putting the settings back.
1. I don't understand why the following line is necessary. Why can't you just use the existing object from the object collection instead of creating a new one like what you did below ?
Set objNicChanged = objWMIService.Get("Win32_NetworkAdapterConfiguration.Index=" & objNicConfig.Index)
2. You said when you have this "If intStaticReturn = 0 or intStaticReturn = 94 Then", you are able to set teh static ip on the unplugged connection. Are able to verify that the IP address and subnet mask are ACTUALLY configured on the NIC ?
I'm thinking that maybe those 2 settings weren't actually "set". As a result, the NIC is still configured to use DHCP even thought the gateway and DNS are statically configure on the NIC. As a result,
"If Not objNicConfig.DHCPEnabled Then" failed; therefore,
Secondly, I mentioned that I got this script from Microsoft and made some changes to get it to work for me.
You are probably right about the line that finds the changed NICs or whatever it is there for.
I didn't make it clear, but I commented out the statement. I guess I should post better code in the future.
The SetStaticIP function is messy, but I have a pretty good grasp on how to make it work for me.
I really have to finish this thing and have thought of a way that will allow the script to work without the option of enabling DHCP on the "unplugged" NICs.
However, it would be way better if I could get that to work.
Is it going to be possible to enable DHCP on the "unplugged" NICs?
Not from what I've seen; of course, I haven't see much and I could be wrong.
One thing you should be aware of is that as soon as you change the IP from DHCP to static (or vice versa), you will probably lose all network connectivity to the remote host. Keep in mind the idea that you do not want to break a network connection when you remotely administer a machine since that will also "break" your connection to the remote machine. One thing I would suggest is that you can run the script as part of a Startup script so all you have to do is restart the remote machines which can be done using WMI as well.
Thanks for you help. I am going to work on a few ideas to work around this problem. I am aware that the scrip will drop the network connection. This script will be on a laptop and the reason they need to run it is to get connected to the network.
Hey Warhorse ... There is almost always more than one way to skin a horse
The DHCP and Static IP settings are all just Registry Entries ... in order to find it ... make a static IP other than 127.0.0.1 or 192.168.0.1 ... aka make something unique and then search the registry for it.
I use to use a .reg file back in the day to setup small gaming networks ... this would allow you to setup the Static / Dynamic IP addresses without having to have a network cable plugged in.
While I dont have the scripts that I used back then ... I do have another script that is similar that you may find useful.
Note: I changed it a little as to not give away my domain name or IP ... but it should still be functional code
Also note that If you wanted to, you could change the static lines of text to encluded dynamic variables ... to allow for it to work with diffrent IP addresses depending on god only knows what ... username maybe?
But anyway, you can do the same thing for the networksettings of Windows ver 98 through XP ... just got to find the keys
I know that you have to restart Internet Explorer in order for that one to take effect ... I am not sure about the Network Settings ... I dont believe you do, but like I said, it has been quite a while since I have used that particular Reg Key
yah, I find that dynmicly creating a .reg file and then running it from the script that created it is more effective/easier that the built in reg editing stuff ...