#206812 - 2013-02-26 10:06 PM
processing array with for each error
|
salatconed
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
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
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
|
|
|
|
#206815 - 2013-02-26 10:38 PM
Re: processing array with for each error
[Re: Mart]
|
salatconed
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
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...
; 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
|
|
|
|
#206819 - 2013-02-26 10:55 PM
Re: processing array with for each error
[Re: ShaneEP]
|
ShaneEP
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.
; 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
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2312 anonymous users online.
|
|
|