#197465 - 2010-01-20 07:06 PM
Re: Couple script issues.
[Re: Tsguy]
|
Glenn Barnas
KiX Supporter
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
|
OK - try this oneSetConsole('HIDE')
; Declaring variables ; ## YEAH?? So really declare them!!
;
Dim $PZB ; defines what PZB path will be set within CER.ini
Dim $CER ; defines where our user's CER.ini file is saved too, and looked for
Dim $PFX ; defines the value for PFX for claimsview
Dim $MENU ; defines the value for Menu for Claimsview
Dim $Rc ; Return code catcher
Dim $Path ; path string
Dim $Message ; message header
Dim $MaxSess ; maximum number of allowed sessions
Dim $TaskList ; command to return list of user tasks
Dim $aResult ; array containing STDOUT and STDERR arrays
; ## Get the idea? Declare the rest of the variables, too!
; ## Be consistent with Var Names. I prefer MixedCase for local vars, CAPS for globals and @-Macros
; ## Consistent use of commands improves readability - "WriteProfileString" is easier to recognize than "writeprofilestring"
; ## Not required, but use single quotes for Kix strings so double quotes can easily be embedded. Many
; ## DOS commands require double quotes
; ## Embedding vars and macros in strings is bad - use the form 'text ' + $Var + ' more text ' + @MACRO instead
$Rc = SetOption('NoVarsInString', 'On')
$Rc = SetOption('NoMacrosInString', 'On')
; ## init all vars here
$pzb = '\\root\Production$$'
$cer = '\\root\userconfigs$$\@userid\Production'
$pfx = '0001'
$Menu = 'Y'
$MaxSess = 2
;
;
$Message = ' You are not allowed to run more than two copies of OurApp. '
; ## LOAD THE REQUIRED UDF (or paste it to the end of this file, which is more portable)
; ## if you embed the UDF, delete the next line or you will have a duplicate UDF error
Call '.\WshPipe.udf'
; Checking for Cer.ini, copying it if it's not there.
;
$Path = '\\root\userconfigs$\' + @USERID ; ## NO VARS/MACROS INSIDE STRINGS
IF NOT Exist ($Path)
MD $Path
EndIf
;
IF NOT Exist ($cer + '\cer.ini')
MD $cer ; ## NO QUOTES AROUND VARS
Copy '\\root\' + cer$ + '\cer.ini' $cer
WriteProfileString($cer + '\cer.ini', 'System', 'PzbPath', $pzb)
EndIf
;
WriteProfileString($cer + '\cer.ini', 'CLAIMVIEW', 'PFX', $pfx)
WriteProfileString($cer + '\cer.ini', 'ClAIMVIEW', 'MENU', 'menu')
;
;
$Tasklist='tasklist /FI "Username eq %USERDOMAIN%\%USERID%" /FI "Imagename eq App.exe"'
$aResult=WshPipe($Tasklist)
;
If UBound($aResult[0]) < ($MaxSess +1)
Run 'C:\appname\app\app.exe ' + $cer + '\cer.ini'
Else
MessageBox('$Message',' ***You are not allowed to run more than two App sessions*** ',0,10)
EndIf
;
; ## Specify the exit code - leave nothing to chance
Exit 0 Review the comments marked as "; ## " for specific info. There was one small typo in my original example - UBound($aResult) will always return "1", since there are two elements in the array. We need to check the number of elements in the first sub-array - UBound($aResult[0]) is correct.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
#197468 - 2010-01-20 07:35 PM
Re: Couple script issues.
[Re: Tsguy]
|
Tsguy
Getting the hang of it
Registered: 2010-01-18
Posts: 67
Loc: Oregon
|
Glenn,
You da man! Thanks again for all your assistance with this.
Tweaking the UBound command worked. However, I seem unable to launch more than 1 application session now.
BTW = I will look to amend my kix scripting ways and adjust as you've suggested. I've been a hack for so long it will be hard to 'correct' myself and write code the proper way . .but i'll try ;).
TS
PS - Derp. haven't watched Southpark have you? = http://www.urbandictionary.com/define.php?term=derp&defid=134579
|
Top
|
|
|
|
#197470 - 2010-01-20 08:52 PM
Re: Couple script issues.
[Re: Glenn Barnas]
|
Glenn Barnas
KiX Supporter
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
|
Found the problem.. Using this code to test on my PCBreak On
Dim $MaxSess, $TaskList, $aResult, $Message, $Rc, $
$Message ='PROCSS LIMIT EXCEPTION'
$MaxSess = 4
$Tasklist='tasklist /FI "Username eq %USERDOMAIN%\%USERNAME%" /FI "Imagename eq cmd.exe"'
'Running ' $Tasklist ?
$aResult=WshPipe($Tasklist)
UBound($aResult[0]) -1 ' processes' ?
For $ = 0 to UBound($aResult[0])
$ ' ' $aResult[0][$] ?
Next
;
If UBound($aResult[0]) < ($MaxSess + 1)
Run '%COMSPEC%'
Else
$Rc = MessageBox('You are not allowed to run more than two App sessions',$Message,0,10)
EndIf it returnsTECHSERV14 - K:\Misc>limit
Running tasklist /FI "Username eq DOMAIN\userid" /FI "Imagename eq cmd.exe"
4 processes
0
1 Image Name PID Session Name Session# Mem Usage
2 ========================= ======== ================ =========== ============
3 cmd.exe 2976 RDP-Tcp#0 2 4,376 K
4 cmd.exe 5976 RDP-Tcp#0 2 5,476 K
5 Clearly there are more lines than the two headers I could "see", so we adjust the test line to
If UBound($aResult[0]) < ($MaxSess + 3) That should do it. When no tasks are present, the header lines fill 4 elements - 0 thru 3. The UBound less 3 returns the number of elements describing the commands found.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
#197475 - 2010-01-20 09:58 PM
Re: Couple script issues.
[Re: Tsguy]
|
Glenn Barnas
KiX Supporter
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
|
If you don't disable variable expansion - SetOption('NoVarsInStrings', 'on') - you need to "escape" the $ by doubling it. Same with macros and the @.
With variable expansion, Kix has to peek inside every string, look for "$" chars, and try to figure out if what follows it is a variable, and replace the variable with its value. When you want a "$", you use "$$", which tells Kix "no - really put a $ here!"
When you disable variable expansion, you basically are saying "I promise not to put variables inside of quoted strings, so - Kix - take it easy and just output the strings without any extra effort!" Yes, this requires extra effort on our part to build the text strings properly, but it avoids many other issues with data manipulation.
Imagine "The price is $$$Price" vs "The price is $" + $Price - where do you think more problems will occur?
Glenn
PS - take a look at the Sanity UDF. It calls out undeclared vars, duplicate declarations, finds mismatched quotes, parens, and even insures that paired commands (like If/EndIf) are properly matched up. KGen uses Sanity as a final step in assembling scripts, so it's a quick way to validate even simple scripts that don't use UDFs.
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
#197477 - 2010-01-21 12:26 AM
Re: Couple script issues.
[Re: Glenn Barnas]
|
Tsguy
Getting the hang of it
Registered: 2010-01-18
Posts: 67
Loc: Oregon
|
Had to tinker with the syntax a tad to get the application to launch properly, but here's the final product:
SetConsole('HIDE')
; Declaring Global variables
;
Dim $PZB ; defines what PZB path will be set within CER.ini
Dim $Path ; User path string
Dim $CER1 ; defines where the CER.ini base file is stored
Dim $CER2 ; defines the environment folder
Dim $CER3 ; defines the environment folder for the CER.ini
Dim $CER4 ; defines where the CER.ini file is saved to, and looked for
Dim $PFX ; defines the value for PFX for claimsview
Dim $MENU ; defines the value for Menu for claimsview
Dim $MaxSession ; Maximum number of allowed sessions
Dim $App ; defines the EXE to be Run
Dim $Message ; message header
Dim $TaskList ; command to return list of user tasks
Dim $aResult ; array containing STDOUT and STDERR arrays
Dim $Rc ; Return code catcher
;
Call 'c:\scripts\WshPipe.udf' ; Places a call to a script that helps identify running user EXE's.
;
$Rc = SetOption('NoVarsInString', 'On')
$Rc = SetOption('NoMacrosInString', 'On')
;
$pzb = '\\root\Production$'
$Path = '\\root\userconfigs$\' + @Userid
$cer1 = '\\root\cer$'
$cer2 = '\Production'
$cer3 = $Path + $cer2
$cer4 = $cer3 + '\cer.ini'
$pfx = '0001'
$Menu = 'Y'
$MaxSession = 2
$App = 'c:\appname\app\app.exe'
$Message = ' You are not allowed to run more than two copies of OurApp. '
$Tasklist='tasklist /FI "Username eq %USERDOMAIN%\%USERNAME%" /FI "Imagename eq App.exe"'
$aResult=WshPipe($Tasklist)
;
; Checking for Cer.ini, copy it if it's not there
;
IF NOT Exist ($Path)
MD $Path
EndIf
;
If NOT Exist ($cer4)
MD $cer3
copy $cer1 + '\cer.ini' $cer3
WriteProfileString($cer4, 'System', 'PzbPath', $pzb)
WriteProfileString($cer4, 'CLAIMVIEW', 'PFX', $pfx)
writeProfileString($cer4, 'CLAIMVIEW', 'MENU', $Menu)
EndIf
;
;
If UBound($aResult[0]) < ($MaxSession +3)
Run "$App $cer4"
Else
MessageBox ("$Message"," ***You are not allowed to run more than two App sessions*** ",0,10)
EndIf
;
;
Exit 0
I tried many different means of calling the App executable, but could not get it launch with the INI being specified until I finally put the two variables in quotes.
Seems like that's against syntax, but was the only way it would launch properly.
Thanks 20 times over again for your work with me on this Glenn, I'll be rolling this to test servers soon, and hopefully prod next week. All looks good so far.
TS
|
Top
|
|
|
|
#197491 - 2010-01-21 05:43 PM
Re: Couple script issues.
[Re: Glenn Barnas]
|
Tsguy
Getting the hang of it
Registered: 2010-01-18
Posts: 67
Loc: Oregon
|
Hiya Glenn,
You say "can't run", but it worked.
I did feel a bit naughty putting the two Vars in quotes, but after playing with it for a bit I was just happy to get it launching properly.
With adjusting the Run command to
It works properly too, and I hope that's the better syntax.
One last syntax question. As I look the final script over, it seems like there's more lines spent declaring variables, then there is doing the actual work. On the one hand, that makes it much much easier to mod this script for our different environments (qa, prod, dev, etc); two Vars get changed and it's good to go. On the other hand, I'm a bit confused between the need for 'DIM' in addition to '$' for variables.
I always understood "$" to be defining a variable for the context of the script. What does "DIM" add to the statement of variables? I'm not sure I understand the distinction of a 'local variable' as the manual defines it.
much thanks as always
TS
|
Top
|
|
|
|
#197492 - 2010-01-21 06:09 PM
Re: Couple script issues.
[Re: Tsguy]
|
Glenn Barnas
KiX Supporter
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
|
I say "can't run" because if you DON'T use quotes, Kix will mangle the command.
will implicitly declare the variable "Var" as a global, and define it to contain the number "4". Global variables have scope (are visible) to all parts of your program, including functions.performs nearly the same thing, except that the variable is first Declared as a local var and then Defined to contain a value. Using Dim to declare local vars and Global to declare global vars gives you control over the visibility.
Imagine that you use the variable "x" as a simple index counter inside a for/next loop. Inside that loop, you call a function that also uses "x" as a temporary variable. Without this restriction of scope, the function will modify the global value, affecting the loop in your main program. Using Dim to define a local var makes Kix treat the $X in the main program and $X in the function as totally different variables, even though they seem to have the same name.
You'll see that the UDFs that I write take this one step further. Any variable used in my functions uses the form $_Name. Introducing the "_" char serves two purposes - if someone uses the function without declaring the Explicit option, conflicts in variable names would be unlikely. Further, it provides a visual distinction - my brain sees $_X and knows it's different from $X in the main program.
I can appreciate your feelings for 10 Dim lines for 5 lines of code, so to speak. You can Dim $X, $Y, $Z, $A, $B, $C declare many variables on one line, but I find that challenging when I come back after 6 months to update my code and have to wonder "what the @#$@ is that var used for. I try to declare and comment each major var in my code - it's more work, but I find it worth the effort. I have Kix projects with over 15,000 lines of code, so you can see how that can get difficult to maintain without strict habits. I do, however, declare multiple vars on one line when they are related to each other.Dim $I, $J, $K ; index pointers
Dim $aFiles, $File ; array of files & enumerator
Dim $lblField1, $lblField2 ; KixForms field labels for form 1 I also use liberal amounts of comments. KGen allows you to strip off all of the comments from the finished script, so I can compare the commented and uncommented scripts.. this shows that my large projects have a 1.3:1 comment to code ratio. Yes, there are more comment than code characters!
Hope that clears things up.
Glenn
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 369 anonymous users online.
|
|
|