#197225 - 2009-12-30 03:30 PM
Error setting Variable
|
Tesdall
Getting the hang of it
Registered: 2009-10-02
Posts: 78
Loc: USA
|
I have a script that is listed belowe that appears to work up untill line 76. When i get a message in dos stating the following:
ERROR : Error in expression.! Script: s:\scripts\installie8\installie8.kix Line : 75
Line 76 is right under the label :Corporate.
Once again i am learning about kix and my coding is ugly and bloated. Thanks for taking the time to help!
; Created by Tesdall 11/04/2009
; Updated on 12/30/2009
Break on
$nul = MessageBox("Close All Programs, Your PC will reboot after Internet Explorer 8 is installed","Internet Explorer Install",48)
$y=SubStr( @IPADDRESS0, 1, 11)
select
Case $y="19. 42. 84"
;Inside Corp
goto "Corporate"
Case $y="19. 42. 88"
;Inside Corp
goto "Corporate"
Case $y="19. 42.200"
;Inside Corp Lab
goto "Corporate"
Case $y="19. 42. 85"
;Inside Mill
goto "Corporate"
Case $y="19. 42.194"
;Inside Col
goto "Corporate"
Case $y="19. 42.195"
;Inside Mont
goto "Corporate"
Case $y="19. 42.196"
;Inside Ot
goto "Ot"
Case $y="19. 42.197"
;Inside Pac -- pacdata1
goto "Pac"
Case $y="19. 42.198"
;Inside Map
goto "Corporate"
Case $y="19. 42.201"
;Inside Roc
goto "Corporate"
Case $y="19. 42.202"
;Inside Kos
goto "Corporate"
Case $y="19. 42.203"
;Inside Dub
goto "Corporate"
Case $y="19. 42.211"
;Inside Mau
goto "Mau"
Case $y="19. 42.206"
;Jac
goto "Jac"
case 1
$ip="serve3"
EndSelect
----------------------------------------------
:Corporate
$server="serve3"
goto "code"
----------------------------------------------
----------------------------------------------
:Ot
$server="serve2"
If Ping($IP, 0)
goto "Corporate"
else
goto "Code"
Endif
----------------------------------------------
----------------------------------------------
:Pac
;Pacdata1
$server="19.42.197.123"
If Ping($ip, 0)
goto "Corporate"
Else
goto "Code"
Endif
----------------------------------------------
----------------------------------------------
:Jac
$server="19.42.206.234"
If Ping($ip, 0)
Goto "Corporate"
Else
Goto "code"
EndIf
----------------------------------------------
----------------------------------------------
:Mau
;Inside Mau
$server="serve7"
If Ping($ip, 0)
goto "corporate"
Else
goto "code"
EndIf
----------------------------------------------
:Code
$install="\\"+$server+"\apps\internet\ie 8\install\IE8-Setup-Full.exe"
RUN $install
Exit
function Ping($Computer,$GetIP,optional $LoopCount,optional $TimeOut)
if $GetIP
dim $ip, $ipfile, $
$ipfile = @scriptdir + '\ip.txt'
shell '%Comspec% /q /e:1024 /c for /F "tokens=2 delims=[]" %%i IN ('+ chr(39)
+ '"ping ' + $Computer1 + ' -n 1 | find "]""' + chr(39) + ') do echo %%i >"' + $ipfile + '"'
$ = open(10,$ipfile,2) $ip = readline(10) $ = close(10) del $ipfile
if $ip
$Ping = $ip
else
$Ping = 'Bad IP address ' + $Computer + '!'
endif
exit 0
else
if $TimeOut
for $c = 0 to $LoopCount
shell '%Comspec% /C ping ' + $Computer + ' -n 1 -w ' + $TimeOut + ' | find /C "TTL=" > nul'
if @error = 0
$Ping = 1
exit 0
endif
next
else
for $c = 0 to $LoopCount
shell '%Comspec% /C ping ' + $Computer + ' | find /C "TTL=" > nul'
if @error = 0
$Ping = 1
exit 0
endif
next
endif
$Ping = 0
endif
endfunction
I had to edit the code to not display any "bad" information. Please note the ip address / servers / labels are made up and the code works otherwise.
Edited by Tesdall (2009-12-30 03:32 PM) Edit Reason: Updated Code
_________________________
Where ever you go, there you are.
|
Top
|
|
|
|
#197227 - 2009-12-30 04:01 PM
Re: Error setting Variable
[Re: Tesdall]
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Don't use GOTO. There are only a very few specific times you need it, and this isn't one of them.
When you have many similar cases, see if you can consolidate. There is no point for example in having specific cases which match the default.
Applying these suggestions will reduced your code dramatically as you'll see from the (untested) example below (I've left out the PING() UDF for clarity)
; Created by Tesdall 11/04/2009
; Updated on 12/30/2009
Break on
$nul = MessageBox("Close All Programs, Your PC will reboot after Internet Explorer 8 is installed","Internet Explorer Install",48)
$sSubnet=SubStr(@IPADDRESS0,1,11)
$sCorpServer="serve3"
select
Case $sSubnet="19. 42.197" ; PAC
$sPreferred="19.42.197.123"
Case $sSubnet="19. 42.211" ; Mau
$sPreferred="serve7"
Case $sSubnet="19. 42.206" ; Jac
$sPreferred="19.42.206.234"
Case $sSubnet="19. 42.196" ; Ot
$sPreferred="serve2"
case 1 ; Default is corporate server
$sPreferred=$sCorpServer
EndSelect
; If the preferred server is not responding then fall back to the corporate server
If Ping($sPreferred,0) $sPreferred=$sCorpServer EndIf
$sInstallPath="\\"+$sPreferred+"\apps\internet\ie 8\install\IE8-Setup-Full.exe"
RUN $sInstallPath
Exit @ERROR
Edited by Richard H. (2009-12-30 04:04 PM) Edit Reason: Oops. Missed an EndIf
|
Top
|
|
|
|
#197229 - 2009-12-30 04:17 PM
Re: Error setting Variable
[Re: Richard H.]
|
Tesdall
Getting the hang of it
Registered: 2009-10-02
Posts: 78
Loc: USA
|
I am used to using goto for batch files. SOOOO anyways, i re-thunked my logic on this, and with the first post i was able to condence (like ths soup) my script. I love Condencing?
; Created by Tesdall 11/04/2009
; Updated on 12/30/2009
Break on
$nul = MessageBox("Close All Programs, Your PC will reboot after Internet Explorer 8 is installed","Internet Explorer
Install",48)
DIM $Noresponce
DIM $Server
DIM $Y
$y = Val(Split(Join(Split(@IPADDRESS0, ' '), ''), '.')[2])
$server="serve3"
select
Case $y="84" or "88" or "200" or "85" or "194" or "195" or "198" or "201" or "202" or "203" or "206"
Case $y="196"
;Inside Ot
$server="server2"
If Ping($server, 0) >=0
else
$server="serve3"
Endif
Case $y="197"
;Inside Pac -- pacdata1
$server="19.42.197.123"
If Ping($server, 0) >=0
else
$server="serve3"
Endif
Case $y="211"
;Inside Mau
$server="serve7"
If Ping($server, 0) >=0
else
$server="serve3"
Endif
case 1
$ip="serve3"
EndSelect
If Ping($server, 0) >=0
Else
$Noresponce = MessageBox("There is no server avaiable at this time, please wait and try again","Internet Explorer Error",48)
Endif
$install="\\"+$server+"\apps\internet\ie 8\install\IE8-Setup-Full.exe"
RUN $install
$Null = MessageBox("Thank you for installing Internet Explorer 8, It may take a minute or two for something to show up on
your screen","Internet Explorer Install",48)
Exit
;;
;;======================================================================
;;
;;FUNCTION ping()
;;
;;ACTION ping - Pings a host
;;
;;AUTHOR Glenn Barnas
;;
;;VERSION 2.0 - 2007/10/20 - WHS version
;; 1.0 - based on KORG Ping UDF by Jochen Polster, enhanced to
;; return values and IP's
;;
;;SYNTAX ping(host, [Flag], [Wait])
;;
;;PARAMETERS host - name of host to ping
;; FLAG - if negative, returns IP Address
;; if >0, specifies number of tries (default is 1)
;; Wait - optional ping timeout value
;;
;;
;;REMARKS ERROR is set to 0 if success, 1 otherwise.
;;
;;RETURNS FLAG >= 0: returns 1 if host is reachable, 0 if not
;; FLAG < 0: Returns IP address if resolvable, 0.0.0.0 if not
;;
;;DEPENDENCIES OS Commands Ping & Find
;;
;;TESTED WITH NT4, W2K, WXP
;;
;;EXAMPLES Ping('hostname') ; returns Success/Failure
;; Ping('hostname',-1) ; returns IP Address
;
Function Ping($_Host, OPTIONAL $_Flag, OPTIONAL $_Wait)
Dim $_oExec ; WSH Object
Dim $_Tries ; # of times to ping
Dim $_Timeout ; Ping timeout value
Dim $_Response ; Response Flag
Dim $_Line ; Line returned from command string
Dim $_Cmd ; first part of command string
Dim $_Count ; current ping count
$_Flag = Val($_Flag) ; determine what to do
$_Wait = Val($_Wait) ;
$_Tries = 1 ; one ping
$_Timeout = 1000 ; 1 second timeout
; set timeout if Wait is non-zero
If $_Wait > 0
$_Timeout = $_Wait
EndIf
If $_FLAG > 0 ; Multiple pings - return on first reply
$_Tries = $_FLAG
EndIf
; Ping the host $_Tries times, but only until a response is received
$_Count = 0
; search for reply from host during PING
$_Cmd = '%COMSPEC% /c ping.exe -4 -n 1 -w ' + $_Timeout + ' ' + $_Host
If $_Flag < 0
$_Cmd = $_Cmd + ' | %SystemRoot%\System32\Find "Pinging"'
Else
$_Cmd = $_Cmd + ' | %SystemRoot%\System32\Find "Reply" | %SystemRoot%\System32\Find "TTL="'
EndIf
Do
$_oExec = CreateObject("WScript.Shell").Exec($_Cmd)
If Not VarType($_oExec)=9 $Ping = 'WScript.Shell Exec Unsupported' Exit 10 EndIf
$_Line = Split(Join(Split($_oExec.StdOut.ReadAll + $_oExec.StdErr.ReadAll,CHR(13)),''),CHR(10))[0]
$_Response = IIf($_Line, 1, 0)
If $_Response
$_Count = $_Tries
EndIf
$_Count = $_Count + 1
If $_Count < $_Tries Sleep 0.25 EndIf
Until $_Count >= $_Tries
; If FLAG >= 0, return success/failure - otherwise return IP address
If $_FLAG >= 0
$Ping = $_Response
Else
If Not $_Response
$Ping = '0.0.0.0'
Else
; In this mode we return the IP address - we should have the HOSTNAME
; handle the 'duh' factor for times when we get the IP address instead!
If InStr($_Line,'[') > 0
$Ping= Split(Join(Split($_Line,']',-1),'['), '[')[1]
Else
$Ping = Split(Split($_Line,' ',-1)[1], ':')[0]
EndIf
EndIf
EndIf
Exit Not $_Response ; set the error code
EndFunction
Edited by Tesdall (2009-12-30 04:26 PM)
_________________________
Where ever you go, there you are.
|
Top
|
|
|
|
#197230 - 2009-12-30 04:18 PM
Re: Error setting Variable
[Re: Tesdall]
|
Glenn Barnas
KiX Supporter
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
|
Here's a different approach. It relies on a Ping() UDF that returns true if the host replies and false if it doesn't. There are several here as well as the two (ping and kfping) on my web site's UDF library.; Created by Tesdall 11/04/2009
; Updated on 12/30/2009
Break on
$nul = MessageBox('Close All Programs, Your PC will reboot after Internet Explorer 8 is installed','Internet Explorer Install',48)
;get the third octet from the network address
$Net = Val(Split(Join(Split(@IPADDRESS0, ' '), ''), '.')[2])
Select
Case $Net = 84 ;Inside Corp
$Server='serve3'
Case $Net = 88 ;Inside Corp
$Server='serve3'
Case $Net = 200 ;Inside Corp Lab
$Server='serve3'
Case $Net = 85 ;Inside Mill
$Server='serve3'
Case $Net = 194 ;Inside Col
$Server='serve3'
Case $Net = 195 ;Inside Mont
$Server='serve3'
Case $Net = 196 ;Inside Ot
$Server='serve2'
Case $Net = 197 ;Inside Pac -- pacdata1
$Server='19.42.197.123'
Case $Net = 198 ;Inside Map
$Server='serve3'
Case $Net = 201 ;Inside Roc
$Server='serve3'
Case $Net = 202 ;Inside Kos
$Server='serve3'
Case $Net = 203 ;Inside Dub
$Server='serve3'
Case $Net = 211 ;Inside Mau
$Server='serve7'
Case $Net = 206 ;Jac
$Server='19.42.206.234'
case 1
$Server='serve3'
EndSelect
'Want to use ' $Server ' for install' ? ; for debugging, show desired server
If Not Ping($Server) ; if secondary server does not respond, fail over to main server
$Server='serve3'
Endif
'Using ' $Server ' for install' ? ; for debugging, show actual server
If Ping($Server) ; run command if target server responds
$Install = '\\' + $server + '\apps\internet\ie 8\install\IE8-Setup-Full.exe'
Run $Install
EndIf
Exit 0 Nothing other than the $Net=... code has been tested, since it is environment-specific.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
#197234 - 2009-12-30 04:27 PM
Re: Error setting Variable
[Re: Glenn Barnas]
|
Tesdall
Getting the hang of it
Registered: 2009-10-02
Posts: 78
Loc: USA
|
i updated my code and absorbed the net command. When i ran it, it apeared to work. I got all the way down to the bottom with no errors. I updated the code above. Could you please review again?
_________________________
Where ever you go, there you are.
|
Top
|
|
|
|
#197236 - 2009-12-30 04:39 PM
Re: Error setting Variable
[Re: Richard H.]
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
; Created by Tesdall 11/04/2009
; Updated on 12/30/2009
Break on
$nul = MessageBox("Close All Programs, Your PC will reboot after Internet Explorer 8 is installed","Internet Explorer @Install",48)
DIM $Noresponse
DIM $Server,$sDefaultServer
DIM $Y
$y = Val(Split(Join(Split(@IPADDRESS0, ' '), ''), '.')[2])
$sDefaultServer="serve3"
Select
; The following case is not required as the action matched the default
; Case $y="84" or "88" or "200" or "85" or "194" or "195" or "198" or "201" or "202" or "203" or "206"
Case $y="196"
$server="server2"
Case $y="197"
;Inside Pac -- pacdata1
$server="19.42.197.123"
Case $y="211"
;Inside Mau
$server="serve7"
case 1
$server=$sDefaultServer
EndSelect
; Switch to default if preferred not available
If Not Ping($server, 0) $server=$sDefaultServer EndIf
If Not Ping($server, 0)
$Noresponse = MessageBox("There is no server avaiable at this time, please wait and try again","Internet Explorer Error",48)
Exit @ERROR
Endif
$install="\\"+$server+"\apps\internet\ie 8\install\IE8-Setup-Full.exe"
RUN $install
$Null = MessageBox("Thank you for installing Internet Explorer 8, It may take a minute or two for something to show up on your screen","Internet Explorer Install",48)
Exit @ERROR
|
Top
|
|
|
|
#197239 - 2009-12-30 05:09 PM
Re: Error setting Variable
[Re: Richard H.]
|
Tesdall
Getting the hang of it
Registered: 2009-10-02
Posts: 78
Loc: USA
|
Hope you can agree that this is ok code, and that it doesn't suck to bad.
; Created by Tesdall 11/04/2009
; Updated on 12/30/2009
Break on
$nul = MessageBox("Close All Programs, Your PC will reboot after Internet Explorer 8 is installed","Internet Explorer Install",48)
DIM $Server
DIM $y
$y = Val(Split(Join(Split(@IPADDRESS0, ' '), ''), '.')[2])
$server="serve3"
select
Case $y="84" or $y="85" or $y="88" or $y="194" or $y="195" or $y="198"
or $y="200" or $y="201" or $y="202" or $y="203" or $y="206"
Case $y="196"
;Inside Ot
$server="serve2"
If Ping($server, 0) >=0
else
$server="serve3"
Endif
Case $y="197"
;Inside Pac -- pacdata1
$server="199.42.197.123"
If Ping($server, 0) >=0
else
$server="serve3"
Endif
Case $y="211"
;Inside Mau
$server="serve7"
If Ping($server, 0) >=0
else
$server="serve3"
Endif
Case 1
$Slow = MessageBox("You might be on a VPN or Slow connection.
It may take awhile to install.
If you have any questions please call help desk at (800) *** ****
otherwise select yes to install","Internet Explorer Install", 4, 67)
if $slow = 6
? "Yes was selected, Installing..."
else
$slow = 7
? "No was selected, Exiting..."
Endif
Exit
EndSelect
If Ping($server, 0) >=0
Else
$Noresponce = MessageBox("There is no server avaiable at this time, please wait and try again","Internet Explorer Error",48)
Endif
$Install = '\\' + $server + '\apps\internet\ie 8\install\IE8-Setup-Full.exe'
RUN $install
$Null = MessageBox("Thank you for installing Internet Explorer 8, It may take a minute or two for something to show up on
your screen","Internet Explorer Install",48)
Exit
;;======================================================================
;;
;;FUNCTION ping()
;;
;;ACTION ping - Pings a host
;;
;;AUTHOR Glenn Barnas
;;
;;VERSION 2.0 - 2007/10/20 - WHS version
;; 1.0 - based on KORG Ping UDF by Jochen Polster, enhanced to
;; return values and IP's
;;
;;SYNTAX ping(host, [Flag], [Wait])
;;
;;PARAMETERS host - name of host to ping
;; FLAG - if negative, returns IP Address
;; if >0, specifies number of tries (default is 1)
;; Wait - optional ping timeout value
;;
;;
;;REMARKS ERROR is set to 0 if success, 1 otherwise.
;;
;;RETURNS FLAG >= 0: returns 1 if host is reachable, 0 if not
;; FLAG < 0: Returns IP address if resolvable, 0.0.0.0 if not
;;
;;DEPENDENCIES OS Commands Ping & Find
;;
;;TESTED WITH NT4, W2K, WXP
;;
;;EXAMPLES Ping('hostname') ; returns Success/Failure
;; Ping('hostname',-1) ; returns IP Address
;
Function Ping($_Host, OPTIONAL $_Flag, OPTIONAL $_Wait)
Dim $_oExec ; WSH Object
Dim $_Tries ; # of times to ping
Dim $_Timeout ; Ping timeout value
Dim $_Response ; Response Flag
Dim $_Line ; Line returned from command string
Dim $_Cmd ; first part of command string
Dim $_Count ; current ping count
$_Flag = Val($_Flag) ; determine what to do
$_Wait = Val($_Wait) ;
$_Tries = 1 ; one ping
$_Timeout = 1000 ; 1 second timeout
; set timeout if Wait is non-zero
If $_Wait > 0
$_Timeout = $_Wait
EndIf
If $_FLAG > 0 ; Multiple pings - return on first reply
$_Tries = $_FLAG
EndIf
; Ping the host $_Tries times, but only until a response is received
$_Count = 0
; search for reply from host during PING
$_Cmd = '%COMSPEC% /c ping.exe -4 -n 1 -w ' + $_Timeout + ' ' + $_Host
If $_Flag < 0
$_Cmd = $_Cmd + ' | %SystemRoot%\System32\Find "Pinging"'
Else
$_Cmd = $_Cmd + ' | %SystemRoot%\System32\Find "Reply" | %SystemRoot%\System32\Find "TTL="'
EndIf
Do
$_oExec = CreateObject("WScript.Shell").Exec($_Cmd)
If Not VarType($_oExec)=9 $Ping = 'WScript.Shell Exec Unsupported' Exit 10 EndIf
$_Line = Split(Join(Split($_oExec.StdOut.ReadAll + $_oExec.StdErr.ReadAll,CHR(13)),''),CHR(10))[0]
$_Response = IIf($_Line, 1, 0)
If $_Response
$_Count = $_Tries
EndIf
$_Count = $_Count + 1
If $_Count < $_Tries Sleep 0.25 EndIf
Until $_Count >= $_Tries
; If FLAG >= 0, return success/failure - otherwise return IP address
If $_FLAG >= 0
$Ping = $_Response
Else
If Not $_Response
$Ping = '0.0.0.0'
Else
; In this mode we return the IP address - we should have the HOSTNAME
; handle the 'duh' factor for times when we get the IP address instead!
If InStr($_Line,'[') > 0
$Ping= Split(Join(Split($_Line,']',-1),'['), '[')[1]
Else
$Ping = Split(Split($_Line,' ',-1)[1], ':')[0]
EndIf
EndIf
EndIf
Exit Not $_Response ; set the error code
EndFunction
Edited by Tesdall (2009-12-30 05:27 PM)
_________________________
Where ever you go, there you are.
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 382 anonymous users online.
|
|
|