Jump to content

[VBS] Shell.run running too many times


Recommended Posts

Hello,

I have made a script where, if a user falls within a defined IP range, a batch file will run.

The problem I'm having is that if a user has both a LAN and WIFI connection in the same range, the batch file will run twice.

Any idea how I can make the batch file run only once? I've tried using counters but I kept getting errors and can not figure it out.

Thanks alot in advance !

Please see the Code Box for the entire code.

If Ip > "194.7.23.199" And Ip < "194.7.23.255" Or Ip > "192.168.180.99" And Ip < "192.168.180.200" Or Ip > "192.168.181.99" And Ip < "192.168.181.200" and InStr(sysInfo.ComputerName, "OU=" & SBX) > 0 Then

shell.run("\\SERVER\NETLOGON\Other\pstbackup\Check.bat")

Dim Wmi :Set Wmi = GetObject("winmgmts:\\.\root\CIMV2")  
Dim Ip, Z1, Z2, i
Dim shell, counter
set shell=createobject("wscript.shell")
Set sysInfo = CreateObject("ADSystemInfo")

For Each Obj in Wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=1",,48)
If not isNull(Obj.IPSubnet) Then
If not isNull(Obj.IPAddress) Then
Z1 = Join(Obj.IPAddress, ",")
If Len(Z1) <= 15 Then
Ip = Z1
ElseIf Len(Z1) >= 16 Then
Z2 = Split(Z1, ",")
Ip = Z2(0)
End If
End If
End If




If Ip > "194.7.23.199" And Ip < "194.7.23.255" Or Ip > "192.168.180.99" And Ip < "192.168.180.200" Or Ip >

"192.168.181.99" And Ip < "192.168.181.200" and InStr(sysInfo.ComputerName, "OU=" & SBX) > 0 Then

shell.run("\\SERVER\NETLOGON\Other\pstbackup\Check.bat")



Elseif Ip > "192.168.199.109" And Ip < "192.168.199.161" and InStr(sysInfo.ComputerName, "OU=" & SB) > 0 Then
shell.run("\\SERVER\NETLOGON\Other\pstbackup\Check.bat")
End If
Next
set shell=nothing
WScript.Quit

Link to comment
Share on other sites


Perhaps try this code, this is only a guess at what you want.


Dim Wmi :Set Wmi = GetObject("winmgmts:\\.\root\CIMV2")
Dim Ip, Z1, Z2, i
Dim shell, counter
Set shell=createobject("wscript.shell")
Set sysInfo = CreateObject("ADSystemInfo")
'-> Start Loop
For Each Obj in Wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=1",,48)
If not isNull(Obj.IPSubnet) Then
If not isNull(Obj.IPAddress) Then
Z1 = Join(Obj.IPAddress, ",")
If Len(Z1) <= 15 Then
Ip = Z1
ElseIf Len(Z1) >= 16 Then
Z2 = Split(Z1, ",")
Ip = Z2(0)
End If
End If
End If
Next
'-> End Loop Process One Piece Of Info Only
'-> Check Only The Single Result
If Ip > "194.7.23.199" And Ip < "194.7.23.255" Or _
Ip > "192.168.180.99" And Ip < "192.168.180.200" Or _
Ip > "192.168.181.99" And Ip < "192.168.181.200" And _
InStr(sysInfo.ComputerName, "OU=" & SBX) > 0 Then
shell.run("\\SERVER\NETLOGON\Other\pstbackup\Check.bat")
Elseif Ip > "192.168.199.109" And Ip < "192.168.199.161" And _
InStr(sysInfo.ComputerName, "OU=" & SB) > 0 Then
shell.run("\\SERVER\NETLOGON\Other\pstbackup\Check.bat")
End If

Link to comment
Share on other sites

And an Exit For statement after the command you want run only once inside the If..else..then, that way once the statement is true and the backup script runs it will exit the for loop and continue the script after the Next statement.

Something like this: (snipped to just the if..then the rest of the code would remain the same)

    If Ip > "194.7.23.199" And Ip < "194.7.23.255" Or _
Ip > "192.168.180.99" And Ip < "192.168.180.200" Or _
Ip > "192.168.181.99" And Ip < "192.168.181.200" and _
InStr(sysInfo.ComputerName, "OU=" & SBX) > 0 Then
shell.run("\\SERVER\NETLOGON\Other\pstbackup\Check.bat")
Exit For
Elseif Ip > "192.168.199.109" And Ip < "192.168.199.161" and InStr(sysInfo.ComputerName, "OU=" & SB) > 0 Then
shell.run("\\SERVER\NETLOGON\Other\pstbackup\Check.bat")
Exit For
End If

Link to comment
Share on other sites

You got more issues than that. Making it run once is really simple (I'd just set a "within IP range" boolean flag myself for this) and logic/flow can be simplified (like using a nested "for each" instead of all the Z1/Z2/Ip stuff and using some guard clauses) but the main issue is that your IP range checking is completely broken in the first place. You're doing a string comparison on groups of numbers separated by dots and that just doesn't work. Just try this:

if "192.168.180.100" < "192.168.180.99" then msgbox "I told you..."

Why that says 100 is less than 99? Because it's only looking at each number character per character, comparing their individual values, and 1 (first digit after the 3rd dot, in 100) is less than 9 (first digit after the 3rd dot, in 99). Similarly, a dot that isn't placed exactly in the same spot (in cases where you don't get 3 digits i.e. very often) will make the tests "fail" too, by comparing the dot with a number (the dot always being worth "less" than any number). For example:

if "123.123.123.123" > "123.123.12.123" then msgbox "A dot is worth less than a 3"

This snippet only works because the dot after 12 is worth 46 and the 3 in the first number is worth 51 -- NOT because 123 is larger than 12. I'm sure you're starting to see the various issues here.

So you will have to split the IPs in each of its different octets, then use those to test the IP ranges. Another method people use sometimes is converting the IP to one very large number but then it's never obvious what you're comparing to without proper comments, a calculator and some time.

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...