MSFN Forum: [VBS] Shell.run running too many times - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[VBS] Shell.run running too many times Rate Topic: -----

#1 User is offline   kiro 

  • Group: Members
  • Posts: 2
  • Joined: 30-September 11
  • OS:95
  • Country: Country Flag

Posted 30 September 2011 - 05:44 AM

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



#2 User is offline   Tripredacus 

  • K-Mart-ian Legend
  • Group: Super Moderator
  • Posts: 8,665
  • Joined: 28-April 06
  • OS:Server 2012
  • Country: Country Flag

Posted 30 September 2011 - 08:49 AM

There are multiple ways to make it run only once. In your bat file, you can write to the registry before performing your commands, then also check for the presense of this value before running those commands.

#3 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,351
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 30 September 2011 - 11:21 AM

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 



#4 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,266
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 30 September 2011 - 05:24 PM

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



#5 User is offline   CoffeeFiend 

  • Coffee Aficionado
  • Group: Super Moderator
  • Posts: 5,399
  • Joined: 14-July 04
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 01 October 2011 - 07:07 AM

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.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy