Introduction: Intro to VB Script: a Beginners Guide
***((((ALERT: Although there are a lot of steps. this is actually quite easy. I just explain everything in detail for the beginners. If you just want the code jump to the last step and you will find the full code there.))))***
I play a lot of Xbox live and always have to disable the internet on my computer because it hogs bandwidth. Mainly because I am downloading or uploading things all the time. The problem isn't shutting it off but it is turning it back on that I forget, making it to where I loose out on all that upload/download time.
So I decided to create a program to do it for me. I was going to write it in Vb.net but I just recently had to nuke and pave my operating system on my computer because of a bad storm frying my hard drive, and haven't reinstalled VB.net yet so I just figured this would give me a reason to play with vbscript.
I also made an image in paint to display in the background so i knew it was running.
I play a lot of Xbox live and always have to disable the internet on my computer because it hogs bandwidth. Mainly because I am downloading or uploading things all the time. The problem isn't shutting it off but it is turning it back on that I forget, making it to where I loose out on all that upload/download time.
So I decided to create a program to do it for me. I was going to write it in Vb.net but I just recently had to nuke and pave my operating system on my computer because of a bad storm frying my hard drive, and haven't reinstalled VB.net yet so I just figured this would give me a reason to play with vbscript.
I also made an image in paint to display in the background so i knew it was running.
Step 1: Figure Out What You Want to Do.
The first thing I always do is make a plan.
What do I want the program to do?
A) I want to be able to open the script and it will automatically shut off the connection.
B) I want it to display a giant display in the background so there is no mistake that the internet is off, this way I don't forget to turn it back on.
C) I want it to give me a choice to turn it back on and then wait for a response.
D) I want it to make sure i turn it back on so i don't forget.
E) Then once I actually do say yes to turn it back on, the script turns it back on and then closes the background.
F) Exit everything.
What do I want the program to do?
A) I want to be able to open the script and it will automatically shut off the connection.
B) I want it to display a giant display in the background so there is no mistake that the internet is off, this way I don't forget to turn it back on.
C) I want it to give me a choice to turn it back on and then wait for a response.
D) I want it to make sure i turn it back on so i don't forget.
E) Then once I actually do say yes to turn it back on, the script turns it back on and then closes the background.
F) Exit everything.
Step 2: Things I Use But Not Really Needed
All you really need is a text editor like notepad. Everything else is optional.
I use Notepad++ when I do VB script so because it treats it like vb.net does with colors and knows the language. It helps to avoid mistakes. It also numbers the lines so you can diagnose what is the problem when you get an error code.
Can be gotten from here notepad ++
I use Notepad++ when I do VB script so because it treats it like vb.net does with colors and knows the language. It helps to avoid mistakes. It also numbers the lines so you can diagnose what is the problem when you get an error code.
Can be gotten from here notepad ++
Step 3: Start Programming
Well the first thing you want to do is create a text file and save it to whatever you want it to be named with the file extension .vbs
Example : NetworkShutOffScriptThatShutsOffTheInternetSoIcanPlayGamesOnMyXboxAndNotGetLag.vbs
Example 2: games.vbs
All that matters is that you name it with the file extension .vbs
I usually do this on the desktop because I open it about a zillion times while I'm working on it.
Now just right click the file and hit edit or edit with notepad++
Example : NetworkShutOffScriptThatShutsOffTheInternetSoIcanPlayGamesOnMyXboxAndNotGetLag.vbs
Example 2: games.vbs
All that matters is that you name it with the file extension .vbs
I usually do this on the desktop because I open it about a zillion times while I'm working on it.
Now just right click the file and hit edit or edit with notepad++
Step 4: Step 1: Opening a Command Prompt Window.
So I decided to do most of the work I would just use programs already stored on the computer to do what i want so. Less Coding and no downloads...Woohoo.
Easiest is command prompt.
so we want to open a command prompt window to do so we want to build an object
to do so we type:
set shellobj = CreateObject("WScript.Shell")
What it does: Set an instance of shellobj to be equal to create object w script shell. This way all we have to do is type shellobj instead of CreateObject("WScript.Shell") everytime we need new command.
Now that we have created an object we instruct the object to open a command prompt window.
to do that we type:
shellobj.run "cmd"
What it does: This would be the equivalent of typing CMD into the run box on the start menu. Had we not set the variable shellobject we would have had to have typed: CreateObject("WScript.Shell").run "cmd"... see how much easier it is now. Less room for mistakes.
Click file... then Save... then go to the desktop and double click the vbsfile you made it should run
If everything goes right you should get a windows command prompt to show up.....yaaaaaa!!!
if not check your coding, so far you should have:
set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
You can add comments into your script so you remember what you did later by puting a comma in front of the line like this:
'this is to open a command prompt
When I am coding a lot of times I will do this so I remember what I did later on when I am looking at it for. The script just ignores that line and moves on . If you see them you can do the same.
Easiest is command prompt.
so we want to open a command prompt window to do so we want to build an object
to do so we type:
set shellobj = CreateObject("WScript.Shell")
What it does: Set an instance of shellobj to be equal to create object w script shell. This way all we have to do is type shellobj instead of CreateObject("WScript.Shell") everytime we need new command.
Now that we have created an object we instruct the object to open a command prompt window.
to do that we type:
shellobj.run "cmd"
What it does: This would be the equivalent of typing CMD into the run box on the start menu. Had we not set the variable shellobject we would have had to have typed: CreateObject("WScript.Shell").run "cmd"... see how much easier it is now. Less room for mistakes.
Click file... then Save... then go to the desktop and double click the vbsfile you made it should run
If everything goes right you should get a windows command prompt to show up.....yaaaaaa!!!
if not check your coding, so far you should have:
set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
You can add comments into your script so you remember what you did later by puting a comma in front of the line like this:
'this is to open a command prompt
When I am coding a lot of times I will do this so I remember what I did later on when I am looking at it for. The script just ignores that line and moves on . If you see them you can do the same.
Step 5: Step 2: Setting Up Timers
Ok now that we have a spiffy new window we need to populate it with commands so we can get it to do what we want.
The problem is that some computers take longer than others to open windows so we want to have the script wait a little bit to start. if it starts too soon the computer will fill in the blank before you get the window open and you will either get a partial command or none at all.
So we need to set up a timer. To do this we type:
wscript.sleep 200
What it does: It tells the script to wait an X amount of time before executing the next command. 1000 equals one second so mine is waiting a fifth of a second. You may find your computer needs to wait more time or less. If you watch the computer and it doesn't type the commands correctly, change the number to a higher number like 2000 would be 2 seconds and probably plenty of time.
Save the file now
your code at this point should be:
set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
wscript.sleep 200
The problem is that some computers take longer than others to open windows so we want to have the script wait a little bit to start. if it starts too soon the computer will fill in the blank before you get the window open and you will either get a partial command or none at all.
So we need to set up a timer. To do this we type:
wscript.sleep 200
What it does: It tells the script to wait an X amount of time before executing the next command. 1000 equals one second so mine is waiting a fifth of a second. You may find your computer needs to wait more time or less. If you watch the computer and it doesn't type the commands correctly, change the number to a higher number like 2000 would be 2 seconds and probably plenty of time.
Save the file now
your code at this point should be:
set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
wscript.sleep 200
Step 6: Step 3: Make a Picture
I just opened paint and typed "Internet Disconected" into it and made it 800x600 and saved it as id.bmp and saved it in my root directory, in my case k:\ but yours will probably be c:\
Step 7: Step 4: Sending Commands, Opening the Picture
Ok now that we have a command window waiting for commands all we have to do is type the commands into the window using the sendkeys command.
When we use the sendkeys command it is like typing but it will type whatever we want it to type. Anything we can type we can send including the enter keys needed to execute programs.
now that we know how to send keys lets open our picture we just made, then put in another timer afterword so that it gives the picture time to load.
To do this we type:
shellobj.sendkeys "k:\id.bmp{enter}"
wscript.sleep 200
What it does: This is the same as typing k:\id.bmp into the run box. It should open your picture in the default picture viewer for a bmp... in my case it is windows picture and fax viewer but most people install after market programs that do this as well so who knows what yours will open in.
now save your file and run it .
You should get a command prompt and a picture to pop up. so far so good!!!
your code so far should be:
set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "k:\id.bmp{enter}"
wscript.sleep 200
When we use the sendkeys command it is like typing but it will type whatever we want it to type. Anything we can type we can send including the enter keys needed to execute programs.
now that we know how to send keys lets open our picture we just made, then put in another timer afterword so that it gives the picture time to load.
To do this we type:
shellobj.sendkeys "k:\id.bmp{enter}"
wscript.sleep 200
What it does: This is the same as typing k:\id.bmp into the run box. It should open your picture in the default picture viewer for a bmp... in my case it is windows picture and fax viewer but most people install after market programs that do this as well so who knows what yours will open in.
now save your file and run it .
You should get a command prompt and a picture to pop up. so far so good!!!
your code so far should be:
set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "k:\id.bmp{enter}"
wscript.sleep 200
Step 8: Step 5: Sending Commands, Shut Off Internet.
At this point if you haven't done so yet, download the pdf file at the end so if you screw up you have an Instructable downloaded on how to fix it.
Now this is where the fun begins.
Ok so we will use ipconfig to release our IP address. Without an IP address we cannot get any internet traffic because the router doesn't know to where send it to the computer.
To do this we type:
shellobj.sendkeys "ipconfig /release{enter}"
What it does: This instructs the program IPCONFIG to release the IP address. the {enter} is telling the computer that you hit the enter key. this would be like typing ipconfig /release into the run window and hitting enter.
Usually after you do this, if you run ipconfig it will tell you your IP address is 0.0.0.0 This is what we want. Without an IP address the router doesn't know we are here and no programs can "dial out" so to speak.
Now that we have successfully finished shutting off the IP we want to close the window. Now we could write another line that says:
shellobj.sendkeys "exit{enter}"
But it would be easier to just edit the above line and append it to the end so you have:
shellobj.sendkeys "ipconfig /release{enter}exit{enter}"
Now save it but don't run it because you will shut off your internet connection.
If you do run it, and your internet shuts off, just change the switch from ipconfig /release to ipconfig /renew and it run it again and it should turn it back on. And if all else fails reboot and you should be fine.
your code so far should be:
set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "k:\id.bmp{enter}"
wscript.sleep 200
shellobj.sendkeys "ipconfig /release{enter}exit{enter}"
Now this is where the fun begins.
Ok so we will use ipconfig to release our IP address. Without an IP address we cannot get any internet traffic because the router doesn't know to where send it to the computer.
To do this we type:
shellobj.sendkeys "ipconfig /release{enter}"
What it does: This instructs the program IPCONFIG to release the IP address. the {enter} is telling the computer that you hit the enter key. this would be like typing ipconfig /release into the run window and hitting enter.
Usually after you do this, if you run ipconfig it will tell you your IP address is 0.0.0.0 This is what we want. Without an IP address the router doesn't know we are here and no programs can "dial out" so to speak.
Now that we have successfully finished shutting off the IP we want to close the window. Now we could write another line that says:
shellobj.sendkeys "exit{enter}"
But it would be easier to just edit the above line and append it to the end so you have:
shellobj.sendkeys "ipconfig /release{enter}exit{enter}"
Now save it but don't run it because you will shut off your internet connection.
If you do run it, and your internet shuts off, just change the switch from ipconfig /release to ipconfig /renew and it run it again and it should turn it back on. And if all else fails reboot and you should be fine.
your code so far should be:
set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "k:\id.bmp{enter}"
wscript.sleep 200
shellobj.sendkeys "ipconfig /release{enter}exit{enter}"
Step 9: Step 6: Creating Message Box and Waiting for Answer
Ok, now that we have successfully shut off the internet and closed the command prompt window, we want a message box to pop up asking if we want to turn the internet back on.
But here is where it gets tricky, we want it to wait for a yes but if we click no we don't want it to go away so we will use a while statement to do this.
First we need to dimension a variable for the messgebox so we can assign it number variables. I know it sounds confusing but it really isn't. we already did it once with the line:
set shellobj = CreateObject("WScript.Shell")
this one is even easier
to set it up we just type:
Dim MyVar
What it does: Now we have dimensioned a variable named MyVar
When you get a" yes/no" message box pop up on your screen an you click the buttons it assigns a value to each button, in this case "yes" is 1 and "no" is 2.
We want the variable to start with a "no" So we assign it the number 2 by default, this way it pauses until a button is clicked
To do this we type:
myvar=2
What it does: now MyVar is equal to 2, this way in the next line it will stop and wait for input.
Now we just want it to sit there and wait until we a ready to turn the internet back on.
to do this we type:
while myvar=2
What it does: By doing this we have told the script that as long as the value of myvar =2 then just keep looping the messagebox until we get any other value than 2, wich in this case could only be 1
Next we actually want to make a message box pop up with yes/no buttons.
To do this we type:
MyVar = MsgBox ("Turn on internet connection", 65, "Turn On Internet Connection")
What it does: Now the MyVar variable will be assigned whatever value is assigned to the button that is clicked on the message box. the first part in the quotes is what the messagebox will actually say. The 65 designates it as a Yes/No messagebox. and the last part in quotes is the lable of the messagebox.
to check to see what value it assigns when you click yes or no you could add in a line that says msgbox(myVar) and it will print whatever value is assigned to myvar in a message box.
now that we have a message box we need to close the while statement. Everything that is in between the open and close of the while statement will run every time you click no, because if you click no it is still a value of 2 and loops back to the first part.
to end the while statement type:
wend
What it does: Once you put an end on a while statement it will act a s a loop until the argument is no longer met. So if you continue to hit no it will loop but once you hit yes, it will break free of the loop and continue on with the script.
Save the file but wait to run it till next step.
Your code so far should be:
set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "k:\id.bmp{enter}"
wscript.sleep 200
shellobj.sendkeys "ipconfig /release{enter}exit{enter}"
Dim MyVar
myvar=2
while myvar=2
MyVar = MsgBox ("Turn on internet connection", 65, "Turn On Internet Connection")
wend
But here is where it gets tricky, we want it to wait for a yes but if we click no we don't want it to go away so we will use a while statement to do this.
First we need to dimension a variable for the messgebox so we can assign it number variables. I know it sounds confusing but it really isn't. we already did it once with the line:
set shellobj = CreateObject("WScript.Shell")
this one is even easier
to set it up we just type:
Dim MyVar
What it does: Now we have dimensioned a variable named MyVar
When you get a" yes/no" message box pop up on your screen an you click the buttons it assigns a value to each button, in this case "yes" is 1 and "no" is 2.
We want the variable to start with a "no" So we assign it the number 2 by default, this way it pauses until a button is clicked
To do this we type:
myvar=2
What it does: now MyVar is equal to 2, this way in the next line it will stop and wait for input.
Now we just want it to sit there and wait until we a ready to turn the internet back on.
to do this we type:
while myvar=2
What it does: By doing this we have told the script that as long as the value of myvar =2 then just keep looping the messagebox until we get any other value than 2, wich in this case could only be 1
Next we actually want to make a message box pop up with yes/no buttons.
To do this we type:
MyVar = MsgBox ("Turn on internet connection", 65, "Turn On Internet Connection")
What it does: Now the MyVar variable will be assigned whatever value is assigned to the button that is clicked on the message box. the first part in the quotes is what the messagebox will actually say. The 65 designates it as a Yes/No messagebox. and the last part in quotes is the lable of the messagebox.
to check to see what value it assigns when you click yes or no you could add in a line that says msgbox(myVar) and it will print whatever value is assigned to myvar in a message box.
now that we have a message box we need to close the while statement. Everything that is in between the open and close of the while statement will run every time you click no, because if you click no it is still a value of 2 and loops back to the first part.
to end the while statement type:
wend
What it does: Once you put an end on a while statement it will act a s a loop until the argument is no longer met. So if you continue to hit no it will loop but once you hit yes, it will break free of the loop and continue on with the script.
Save the file but wait to run it till next step.
Your code so far should be:
set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "k:\id.bmp{enter}"
wscript.sleep 200
shellobj.sendkeys "ipconfig /release{enter}exit{enter}"
Dim MyVar
myvar=2
while myvar=2
MyVar = MsgBox ("Turn on internet connection", 65, "Turn On Internet Connection")
wend
Step 10: Step 7: Turning Internet Back On.
Now that someone finally clicked yes and it has passed the while statement, we need to urn on the internet.
I usually pop up a confirmation so the end user knows what is going on but it is purely optional.
To pop up a confirmation type:
msgbox("We will now Turn The Internet Connection back on!!!")
What it does: it sends a message box that says We will now Turn The Internet Connection back on!!! and gives you an ok button to click.
Now all we have to do is turn the internet back on and and close everything out.
to turn the internet back on we just open a command prompt set the timer then use ipcionfig again.
to do this type:
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "ipconfig /renew{enter}"
What it does: it reverses the turn off command from before and renews your IP address. Once you have renewed your IP address you can now get internet connections again.
Now all we have to do is clean up the open windows. Again we could use a sendkeys command but i am just gonna append the last line to hold the cleanups as well.
In this instance i am going to use taskkill program to kill the program displaying the picture. In this case it is windows picture and fax viewer and shows up as rundll32.exe in the processes tab of the task manager so that is what we need to kill. If your picture opens using another program, like irfanview, or acdsee, find the name it goes by in the processes tab and ureplace the rundll32.exe witht he name you find.
The command would be:
taskkill /F /IM rundll32.exe
What it does: the taskkill is the name of the program the /F switch tells it to force a kill and the /IM switch tells it the image name
So now we append that to the last line, add in a enter to execute the command and then an exit command and another enter and it looks like this:
shellobj.sendkeys "ipconfig /renew{enter}taskkill /F /IM rundll32.exe{enter}exit{enter}"
Save your file.
I usually pop up a confirmation so the end user knows what is going on but it is purely optional.
To pop up a confirmation type:
msgbox("We will now Turn The Internet Connection back on!!!")
What it does: it sends a message box that says We will now Turn The Internet Connection back on!!! and gives you an ok button to click.
Now all we have to do is turn the internet back on and and close everything out.
to turn the internet back on we just open a command prompt set the timer then use ipcionfig again.
to do this type:
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "ipconfig /renew{enter}"
What it does: it reverses the turn off command from before and renews your IP address. Once you have renewed your IP address you can now get internet connections again.
Now all we have to do is clean up the open windows. Again we could use a sendkeys command but i am just gonna append the last line to hold the cleanups as well.
In this instance i am going to use taskkill program to kill the program displaying the picture. In this case it is windows picture and fax viewer and shows up as rundll32.exe in the processes tab of the task manager so that is what we need to kill. If your picture opens using another program, like irfanview, or acdsee, find the name it goes by in the processes tab and ureplace the rundll32.exe witht he name you find.
The command would be:
taskkill /F /IM rundll32.exe
What it does: the taskkill is the name of the program the /F switch tells it to force a kill and the /IM switch tells it the image name
So now we append that to the last line, add in a enter to execute the command and then an exit command and another enter and it looks like this:
shellobj.sendkeys "ipconfig /renew{enter}taskkill /F /IM rundll32.exe{enter}exit{enter}"
Save your file.
Step 11: Finished Product.
If everything went right your finished product should look something like this:
set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "k:\id.bmp{enter}"
wscript.sleep 200
shellobj.sendkeys "ipconfig /release{enter}exit{enter}"
Dim MyVar
myvar=2
while myvar=2
MyVar = MsgBox ("Turn on internet connection", 65, "Turn On Internet Connection")
wend
msgbox("We will now Turn The Internet Connection back on!!!")
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "ipconfig /renew{enter}taskkill /F /IM rundll32.exe{enter}exit{enter}"
_
end of script
_
Now that the programming is done you can run the program.
What should happen:
1.) You should get CMD window pop up for a very short time then close
2.) Your internet should no longer work. but you wont see this
3.) Your picture should open.
4.) You should get a pop up asking you if you want to turn the internet back on.
5.) No matter how many times you click no the message box should ding at you but ope up again asking if you want to turn the internet back on.
6.) Once you click yes the message box should disappear and a CMD window should show back up
7.) Once the IP address is renewed the CMD window and the picture should close.
There you have it. A very easy VBscript to turn your internet on and off. Hope you like it.
I actually made another script that is almost identical to this one that shuts off the network adapters using a program called DevCon, which is like a command line device manager. Works real well to. Script is identical to this one except where in this one it has commadns for ipconfig that one has commands for DevCon.
set shellobj = CreateObject("WScript.Shell")
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "k:\id.bmp{enter}"
wscript.sleep 200
shellobj.sendkeys "ipconfig /release{enter}exit{enter}"
Dim MyVar
myvar=2
while myvar=2
MyVar = MsgBox ("Turn on internet connection", 65, "Turn On Internet Connection")
wend
msgbox("We will now Turn The Internet Connection back on!!!")
shellobj.run "cmd"
wscript.sleep 200
shellobj.sendkeys "ipconfig /renew{enter}taskkill /F /IM rundll32.exe{enter}exit{enter}"
_
end of script
_
Now that the programming is done you can run the program.
What should happen:
1.) You should get CMD window pop up for a very short time then close
2.) Your internet should no longer work. but you wont see this
3.) Your picture should open.
4.) You should get a pop up asking you if you want to turn the internet back on.
5.) No matter how many times you click no the message box should ding at you but ope up again asking if you want to turn the internet back on.
6.) Once you click yes the message box should disappear and a CMD window should show back up
7.) Once the IP address is renewed the CMD window and the picture should close.
There you have it. A very easy VBscript to turn your internet on and off. Hope you like it.
I actually made another script that is almost identical to this one that shuts off the network adapters using a program called DevCon, which is like a command line device manager. Works real well to. Script is identical to this one except where in this one it has commadns for ipconfig that one has commands for DevCon.