Page 2 of 2 <12
Topic Options
#197460 - 2010-01-20 06:21 PM Re: Couple script issues. [Re: Tsguy]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
Just realized that I hadn't filled in the variables of %userdomain% and %userid% in the code. /sigh not enough coffee yet.

I updated that content, confirmed it worked as expected from command line, but same results from running the script, 3 app sessions are launched.

TS

Top
#197461 - 2010-01-20 06:34 PM Re: Couple script issues. [Re: Tsguy]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
hoookay...bout time I got more coffee.

Just realized WSHpipe is another UDF script being referenced, which isn't being referenced within my script. I'm updating now, and will be trying again shortly.

/sigh.

Top
#197462 - 2010-01-20 06:41 PM Re: Couple script issues. [Re: Tsguy]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Yeah, then have another sip and replace the %USERDOMAIN% and %USERID% vars - they should dynamically insert the environment values of the current domain and userid. \:\)

Type "SET U" at a command prompt on the TS to confirm.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197463 - 2010-01-20 06:43 PM Re: Couple script issues. [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
PS - sorry about not giving the heads-up about the missing UDF... with 220+ UDFs in my library, using them is second-nature.

I'm taking a closer look at your code over lunch..

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197464 - 2010-01-20 06:55 PM Re: Couple script issues. [Re: Glenn Barnas]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
 Quote:

Yeah, then have another sip and replace the %USERDOMAIN% and %USERID% vars - they should dynamically insert the environment values of the current domain and userid. \:\)


derp. Should have realized that one, I instead opt'd to use the Kix variables of @domain, and @userid.

No worries about the UDF, at least I know to be on the look out for them now ;).

TS

Top
#197465 - 2010-01-20 07:06 PM Re: Couple script issues. [Re: Tsguy]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
OK - try this one
 Code:
SetConsole('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! \:D

Top
#197466 - 2010-01-20 07:08 PM Re: Couple script issues. [Re: Tsguy]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
derp?

Either environment var or Kix Macro should work.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197467 - 2010-01-20 07:13 PM Re: Couple script issues. [Re: Tsguy]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
IMHO something is not right with this line

 Code:
IF UBound($aResult) < ($MaxSess + 1)


You made the comment that:
 Quote:
2 header lines, plus max 2 sessions


If $MaxSess = 2, and you +1, that's not quite the 4 lines involved with '2 header lines, plus 2 exe lines'. Or am I missing something with that?

In just toying around with it (changing the $MaxSess value, changing the + value) I am unable to get the script to ever hit it's "ELSE" value.

TS

Top
#197468 - 2010-01-20 07:35 PM Re: Couple script issues. [Re: Tsguy]
Tsguy Offline
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
#197469 - 2010-01-20 08:34 PM Re: Couple script issues. [Re: Tsguy]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Those are just suggestions from a programming class I teach.. good habits is all. They become more important as projects get larger and more complex.

I'll take a look again to see why you're only getting one session.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197470 - 2010-01-20 08:52 PM Re: Couple script issues. [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Found the problem.. Using this code to test on my PC
 Code:
Break 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 returns
 Code:
TECHSERV14 - 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
 Code:
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! \:D

Top
#197471 - 2010-01-20 09:14 PM Re: Couple script issues. [Re: Glenn Barnas]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
Looks like that did it Glenn.

Existing script now limits a user to only 2 app sessions, message pops up on third attempt.

I'm going to re-write the whole thing in keeping with your suggestions.

One further question with that however. You mention that
 Quote:
Embedding vars and macros in strings is bad


Wouldn't that mean that this particular VAR is 'bad' as well?

 Code:
$cer = '\\root\userconfigs$$\@userid\production'


I would presume that I should instead be stating this as

 Code:
$cer = '\\root\userconfigs$$\' + @userid + '\production'


Can't say thanks enough for all your help on this . .but I'll try \:\) thanks again,

TS

Top
#197472 - 2010-01-20 09:22 PM Re: Couple script issues. [Re: Tsguy]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
no problem!

Yes, that's almost correct, but - if you've taken my example with the NoVarInStrings setting, then drop the double-$. That setting eliminates the need to double up on $ inside of quotes.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197473 - 2010-01-20 09:33 PM Re: Couple script issues. [Re: Glenn Barnas]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
Interesting, I've always understood it that Kix requires the double - $ to properly process hidden shares. But I guess that's just due to the way my statements were written.

Re-working it now, will post it when done.

TS

Top
#197475 - 2010-01-20 09:58 PM Re: Couple script issues. [Re: Tsguy]
Glenn Barnas Administrator Offline
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! \:D

Top
#197477 - 2010-01-21 12:26 AM Re: Couple script issues. [Re: Glenn Barnas]
Tsguy Offline
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:

 Code:
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.

 Code:
Run "$App $cer4"


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
#197480 - 2010-01-21 01:49 AM Re: Couple script issues. [Re: Tsguy]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
You can't Run $App $Cer4 because those are variables, and kix will simply mash them together. The result is trying to run
 Code:
c:\appname\app\app.exe\\root\userconfigs$\<UserID>\Production\cer.ini
Note that there's no space between the command and the arg. Try this:
 Code:
$Cmd = $App + $Cer4
'Running: ' $Cmd @CRLF
Run $Cmd
Adding the debug statement shows that the command has no space, so change it to
 Code:
$Cmd = $App + ' ' + $Cer4
and it will work (and not violate the NoVarsInStrings rule).

Simple concept is to not Run 'A complex command string with args...', but to build $Cmd, bit by bit if necessary, then display it during development before actually running it. You could then copy it off the screen and paste it back into the command prompt to see what it chokes on. After fixing it (adding the space between command and arg in this case) you can comment-out the debug message.

In many of my scripts, I include the Msg() UDF library, which includes a Dbg() function. I define a Global var $DEBUG, and setting it to a non-zero value allows the debug messages to be displayed:
 Code:
Dbg('About to run: ' + $Cmd)
for example.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197491 - 2010-01-21 05:43 PM Re: Couple script issues. [Re: Glenn Barnas]
Tsguy Offline
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

 Code:
Run $App + ' ' + $cer4


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 Administrator Offline
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.

 Code:
$Var = 4
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.
 Code:
Dim $Var
$Var = 4
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
 Code:
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.
 Code:
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! \:D

Top
Page 2 of 2 <12


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

Who's Online
0 registered and 515 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.071 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

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