Run script for one IP address range with VBS _VBS

Source: Internet
Author: User
Tags case statement
Ask:
Hey, scripting guy!. I want to run the script on all the computers in a subnet. Is there a way to do this task without having to hard-code all the IP addresses into the script?

-RB

For:
Hi, RB. Depending on your description in the e-mail message, it seems your settings are similar: Your subnet has a range of IP addresses of 192.168.1.1 to 192.168.1.254. You want to create a script that starts with the first IP address, runs a code on the appropriate computer, and then turns to the second address, runs the same code, and runs the same script on each computer by address order. In addition, you want to do this with as few lines of code as possible, and you don't have to hard-code hundreds of IP addresses.

So, is there a way to do that? Of course it is, and it's simpler than you think.

First, let's show you how to loop through an IP address range. This is an example code, so it is only used to echo the name of each IP address. After describing how the script works, we'll give a more practical example:

Copy Code code as follows:

On Error Resume Next

intstartingaddress = 1
intendingaddress = 254
Strsubnet = "192.168.1."

For i = intstartingaddress to Intendingaddress
StrComputer = strsubnet & I
WScript.Echo StrComputer
Next

No, really, that's the whole script. We first assigned a number of variables: Assign the value "1" to intstartingaddress, assign the value "254" to intendingaddress, and the value "192.168.1." Assigned to Strsubnet. (note the English period after "1"). As you might guess, these values will be used as building blocks to establish an IP address.

After assigning these variables, we created a for-next loop that runs 1 (intstartingaddress) to 254 (intendingaddress) variables. Why do we have to recycle 1 to 254? The reason is simple: that's your IP range. What if the IP range is 192.168.1.7 to 109.168.1.54? No problem: Use the same loop, but change the value of intStartingAddress to "7" and change the Intendingaddress value to "54".

In this loop, we 192.168.1 the string value. In series with the current value of the loop variable (i). In the first run loop--when "I" equals "1", we will 192.168.1. Combined with 1. What do you think? We got a value of 192.168.1.1, which happens to be the first IP address. The last time we run the loop, we'll put the 192.168.1. Combined with 254 to get the value of the last IP address--192.168.1.254. Run the script and you will get:

192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4

Pretty simple, huh?

Of course, you might not be more interested in echoing a set of IP addresses; you want to run some kind of WMI code. Good:

On Error Resume Next

intstartingaddress = 1
intendingaddress = 254
Strsubnet = "192.168.1."

For i = intstartingaddress to Intendingaddress
StrComputer = strsubnet & I

Set objWMIService = GetObject _
("winmgmts:\\" & StrComputer & "\root\cimv2")
Set Colitems = objWMIService.ExecQuery _
("SELECT * from Win32_OperatingSystem")
For each objitem in colitems
WScript.Echo StrComputer & ":" & Objitem.caption
Next

Next

As you can see, we set the value of the StrComputer variable again to an IP address. Then connect to the WMI service on the computer represented by this address. This is easy to do, because WMI can use either the computer name or the IP address to connect to the computer.

Now, we're going to add a little trick. You mentioned in your email that you want to exclude several IP addresses (which may represent routers or other things). Good. Here is a modified script that uses a Select case statement to exclude certain computers:

intendingaddress = 254
Strsubnet = "192.168.1."

For i = intstartingaddress to Intendingaddress
Select case I
Case 10
Case 50
Case 100

Case Else
StrComputer = strsubnet & I
Set objWMIService = GetObject _
("winmgmts:\\" & StrComputer & "\root\cimv2")
Set Colitems = objWMIService.ExecQuery _
("SELECT * from Win32_OperatingSystem")
For each objitem in colitems
WScript.Echo StrComputer & ":" & Objitem.caption
Next

End Select
Next

Notice the change that occurs when the "I" value equals 10, 50, or 100. Yes: nothing has changed. If the IP address of the computer is 192.168.1.10, 192.168.1.50, or 192.168.1.100, no changes will occur, no WMI code will be run, and the script executes the loop. The WMI code will be executed only on computers that have other IP addresses other than the three addresses above. This is a simple and effective way to exclude specific IP addresses from the WMI part of the script.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.