Page 1 of 1 1
Topic Options
#206812 - 2013-02-26 10:06 PM processing array with for each error
salatconed Offline
Just in Town

Registered: 2012-05-31
Posts: 4
Loc: NY, USA
I keep getting an error on this script and I cant understand why. The error is: Error in expression.! Line: 29 Sorry for the messy formatting, didnt know how to indent. I know the script doesnt to anything, the next step is to run a shell command. Thanks 1 $lan = 0 2 $wlan = 0 3 $index = 0 4 DIM $gateway[10] 5 6 $strComputer = "." 7 $objWMIService = GetObject("winmgmts:\\"+ $strComputer + "\root\cimv2") 8 $colItems = $objWMIService.ExecQuery("Select * from Win32_IP4RouteTable") 9 For Each $objItem in $colItems 10 ..if [quote] [/quote]instr($objItem.Destination, "0.0.0.0") 11 ....; test for wired route 12 ....if instr($objItem.NextHop, "172.28")=1 OR instr($objItem.NextHop, "192.198.7")=1 13 ......$lan = 1 14 ....else 15 ......$gateway[$index] = $objItem.NextHop + "," + $objItem.InterfaceIndex 16 ......$index = $index + 1 17 ......$wlan = 1 18 ....endif 19 ..endif 20 Next 21 22 ; test for multiple default gateways 23 if $lan AND $wlan 24 ..;shell command to remove route 25 ..;route delete 0.0.0.0 mask 0.0.0.0 192.168.2.254 if 15 26 ..for each $item in $gateway 27 ....$data[] = split($item,",") 28 ....$route_cmd = "route delete 0.0.0.0 mask 0.0.0.0 " + $data[0] + " if " + $data[1] 29 ..Next 30 endif 31 32 GET$

Attachments
IPRoute.kix (246 downloads)
Description:



Top
#206813 - 2013-02-26 10:12 PM Re: processing array with for each error [Re: salatconed]
salatconed Offline
Just in Town

Registered: 2012-05-31
Posts: 4
Loc: NY, USA
I keep getting an error on this script and I cant understand why.
The error is: Error in expression.! Line: 29

 Code:
 1 $lan = 0
 2 $wlan = 0
 3 $index = 0
 4 DIM $gateway[10]
 5
 6 $strComputer = "."
 7 $objWMIService = GetObject("winmgmts:\\"+ $strComputer + "\root\cimv2")
 8 $colItems = $objWMIService.ExecQuery("Select * from Win32_IP4RouteTable")
 9 For Each $objItem in $colItems
10     if instr($objItem.Destination, "0.0.0.0")
11         ; test for wired route
12         if instr($objItem.NextHop, "172.28")=1 OR instr($objItem.NextHop, "192.198.7")=1
13             $lan = 1
14         else
15             $gateway[$index] = $objItem.NextHop + "," + $objItem.InterfaceIndex
16             $index = $index + 1
17             $wlan = 1
18         endif
19     endif
20 Next
21
22 ; test for multiple default gateways
23 if $lan AND $wlan
24     ;shell command to remove route
25     ;route delete 0.0.0.0 mask 0.0.0.0 192.168.2.254 if 15
26     for each $item in $gateway
27         $data[] = split($item,",")
28         $route_cmd = "route delete 0.0.0.0 mask 0.0.0.0 " + $data[0] + " if " + $data[1]
29     Next
30 endif
31
32 GET$

Top
#206814 - 2013-02-26 10:20 PM Re: processing array with for each error [Re: salatconed]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
I think your issue is on line 27. You are using $data[] as a variable but this is not possible like this because you are not referencing an element of the array. Removing the [] would improve things a lot.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#206815 - 2013-02-26 10:38 PM Re: processing array with for each error [Re: Mart]
salatconed Offline
Just in Town

Registered: 2012-05-31
Posts: 4
Loc: NY, USA
When I remove the backets on line 27, it moves the error to line 28 "Array reference out of bounds". When I put in a print statement it shows the split worked properly.
Top
#206816 - 2013-02-26 10:50 PM Re: processing array with for each error [Re: salatconed]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
I think the error is coming from having blank items in $gateway. Try replacing that second portion of your code with this...

 Code:
; test for multiple default gateways
if $lan AND $wlan
    ;shell command to remove route
    ;route delete 0.0.0.0 mask 0.0.0.0 192.168.2.254 if 15
    for each $item in $gateway
        if Len($item)
            $data = split($item,",")
            $route_cmd = "route delete 0.0.0.0 mask 0.0.0.0 " + $data[0] + " if " + $data[1]
        endif
    Next
endif


Edited by ShaneEP (2013-02-26 10:51 PM)

Top
#206817 - 2013-02-26 10:50 PM Re: processing array with for each error [Re: salatconed]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
The error you are seeing usually means that you are referencing a non-existing element of an array. Just for debugging add the lines below right after the split to see if both elements actually contain any data.

 Code:
For each $a in $data
  ?$a
Next
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#206818 - 2013-02-26 10:53 PM Re: processing array with for each error [Re: Mart]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
And the reason the LEN check is necessary is because....As Mart just said...If you have blank elements in the $gateway array, and then do a split on it, it will only have 1 element in $data, so when you try to use $data[1] it fails.
Top
#206819 - 2013-02-26 10:55 PM Re: processing array with for each error [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
And actually better than a LEN check, it might be safer to check that there is a ',' in the string since that is what you will be splitting on. So ignore my first suggestion and try this one instead.

 Code:
; test for multiple default gateways
if $lan AND $wlan
    ;shell command to remove route
    ;route delete 0.0.0.0 mask 0.0.0.0 192.168.2.254 if 15
    for each $item in $gateway
        if InStr($item,",")
            $data = split($item,",")
            $route_cmd = "route delete 0.0.0.0 mask 0.0.0.0 " + $data[0] + " if " + $data[1]
        endif
    Next
endif

Top
#206820 - 2013-02-26 11:17 PM Re: processing array with for each error [Re: ShaneEP]
salatconed Offline
Just in Town

Registered: 2012-05-31
Posts: 4
Loc: NY, USA
Mart, ShaneEP

Thank you both, the check fixed the script and beeter still I understand why.

Sal

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2312 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.064 seconds in which 0.028 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org