Page 1 of 2 12>
Topic Options
#197417 - 2010-01-18 07:45 PM Couple script issues.
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
Hello Kix folks.

Am trying a fairly simple check in script for the existence of two files, I get the feeling my syntax is wrong as the script just runs right through it, or if $variable1 exists it displays the messagebox.

Script sample:

$variable1="c:\@userid1.txt"
$variable2="c:\@userid2.txt"
$Message="You have both files"

IF EXIST ("$variable1") AND ("variable2")
Mesasgebox ("$message", "***You have both files***",0,10)
Exit
ENDIF

END script sample

There's a variety of other things going on in this script, but the result of this file check is important for the rest of the script processing.

Additionally, I have another minor problem with trying to use "RUN" to launch a CMD script. The CMD script has a "wait" in it, and that in turn is keeping the Kix script which launched it open.

To quote the manual....

This behavior is different from the MS‑DOS – based version of KiXtart, where the RUN command also terminates the script. If you want to emulate the MS‑DOS – based version, you must add an EXIT command after the RUN command


So if I'm using:

RUN "c:\scripts\file.cmd"
Exit

where does the Exit fit in? I have it the line after and it's not being picked up.

Thanks much,

TS

Top
#197419 - 2010-01-18 08:18 PM Re: Couple script issues. [Re: Tsguy]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
Of course as soon as I post this, I sorted out the logic needed for my first issue to go away.

Logic was changed from:

$variable1="c:\@userid1.txt"
$variable2="c:\@userid2.txt"
$Message="You have both files"

IF EXIST ("$variable1") AND ("variable2")

TO:
$variable1="c:\@userid1.txt"
$variable2="c:\@userid2.txt"
$variable3=EXIST "C:\@userid1.txt"
$variable4=EXIST "C:\@userid2.txt"
$Message="You have both files"

IF $variable3 = 1 AND $variable4 = 1

Works fine now.

Any thoughts on my RUN issue?

much thanks,

TS

Top
#197420 - 2010-01-18 08:32 PM Re: Couple script issues. [Re: Tsguy]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Welcome to KORG!

Your AND logic is wrong in your first post, and overly complex in your second..

You need to qualify each item in the AND
 Code:
If Exist($File1) And Exist($File2)
 'both files are present' @CRLF
EndIf
The Kix Shell command runs an external command and waits for it to complete. The RUN command runs it and continues processing the script.

The quote from the manual is for compatibility with now ancient versions of Kixtart.. the original 16-bit versions would end the script if you used the RUN command. Basically, if you want to emulate the behaviour of the 16b version, your would have to do this
 Code:
$Cmd = 'dos command to run'
Run $Cmd
Exit 0
This is probably NOT what you want to do. Show us the command you are trying to run and we can provide more help.

Glenn

PS - use "code" tags - [ code ] your script[ /code ] to frame your scripts.. the tags are as shown, but with no spaces inside the [ ].
_________________________
Actually I am a Rocket Scientist! \:D

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

Registered: 2010-01-18
Posts: 67
Loc: Oregon
Thanks for the feedback Glenn.

The brief background on what I'm trying to accomplish:

We launch an application for end users via a Kix script which makes environment (prod / qa / dev) type modifications to an INI file before running the app EXE.

We've found out that several staff are running up to 4 copies of the application EXE, and I've gotten authorization to limit them to only 2 copies of the EXE.

So what I'm looking to do with the Kix script mods is essentially EXE count tracking. By creating a txt file each time a user launches the application EXE, and deleting the created txt file each time a user closes the EXE.

The script logic for creation of the txt files and checking for existing txt files prior to launching the application EXE was what I was looking to get sorted out with the 'EXIST / AND' statements. I'm actually going to redo that section in 'select - case' format as it seems a better fit than 'IF'.

With regards to the RUN issue, my problem is this logic:

 Code:
If $File1 = 1 AND $File2 = 0
copy "c:\scripts\session.txt" "$variable2"
RUN "c:\scripts\session2.cmd" EXIT
ENDIF


The CMD file being referenced has the following in it:

 Code:
Start "Application" /wait C:\application\app.exe \\servername\share\app.ini
del \\servernameb\share\session2.txt /q
exit


I'm using the '/wait' in order to keep the CMD file running so that once the EXE is closed the DEL will trigger and clear the session tracker txt.

I know a CMD will be open under the user session because of this, what I've found in /debug however is that using RUN to call the CMD file doesn't exit out the kix script as I would have expected. The kix script is instead waiting for the CMD file that's been called complete before it exits.

This is not a huge problem, however I would prefer that the kix script call the CMD file used the run the app, and then close out without waiting for it to end.

Thanks,

TS

Top
#197422 - 2010-01-18 11:52 PM Re: Couple script issues. [Re: Tsguy]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
By the way I've tried the modification you mentioned and am seeing the same behavior in /debug. The RUN command calls the script properly, but the Kix script waits for the batch script to finish processing before it closes out.

 Code:
SELECT
     Case $File1 = 1 AND $File2 = 0
           Copy "c:\scripts\session.txt" "$variable2"
           RUN $CMD2
           Exit 0
END SELECT


The rest of the script works very well. \:\) Thanks for the assistance.

TS

Top
#197423 - 2010-01-19 01:29 AM Re: Couple script issues. [Re: Tsguy]
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Yo TS,

Well.. There is somebody from my neck of the woods..

For one conditional check, why are you using a SELECT..CASE?

If you need to do a conditional check and need to to find the first true condition, then I can see the point.. This should work for you..
 Code:
IF $File1 = 1 AND $File2 = 0
	Copy "c:\scripts\session.txt" "$variable2"
	RUN $CMD2
	Exit 0
ENDIF


HTH,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#197425 - 2010-01-19 02:33 AM Re: Couple script issues. [Re: Kdyer]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
In a word.. ugh!

Using files to track system processes is like herding cats - possible, but you don't really want to.

I'll dig up some code I have and post it in a bit, but here's something to consider:

  • Use a process-list UDF to determine how many processes are running
  • If the number of processes is 0 or 1, allow the new process to start
  • If the number of processes is 2, display a warning and exit
  • No file cleanup, no mess if the ap terminates abnormally, and totally dynamic
I have an application that allows up to 25 sub-processes to run to communicate with remote systems. Any number of the 160+ servers can requests service, but only 25 at a time are allowed to run. I take it a step further and correlate which process is used to communicate with which server, and check every few seconds to see if a process has ended. If it is no longer in the list, I perform some wrap-up, and allow another process to start if any are in the queue. It sounds like what you want to do is 23 times simpler. ;\)
 Code:
$aProcessList = WMIProcessList('appname')
If UBound($aProcessList) < 1
  Run 'app.exe args'
Else
  'Sorry - can only run two instances of "appname"!' @CRLF
EndIf
is really about all you'd need.

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

Top
#197434 - 2010-01-19 05:24 PM Re: Couple script issues. [Re: Glenn Barnas]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
Glenn you da man! \:\)

I was initially looking to do a process count directly but couldn't see a way to do it, so I came up with the text file creation / deletion plan.

I'll get the posted code tested shortly.

Kyder, sorry for any confusion. I posted only the first portion of the select / case structure.

TS

Top
#197435 - 2010-01-19 05:37 PM Re: Couple script issues. [Re: Tsguy]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
K well that didn't work so well on first test.

 Code:
$AppTracking = WMIProcessList ('appname.exe')
If UBound ($AppTracking) < 1
	Run "C:\appname\app\app.exe $app\app.ini"
	exit
Else
	MessageBox ("$Message","   ***You are not allowed to run more than two App sessions***   ",0,10)
ENDIf


allowed me to launch 3 exes.

TS

Top
#197436 - 2010-01-19 05:40 PM Re: Couple script issues. [Re: Tsguy]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Did you download and include the WMIProcessList UDF?

Minor syntax - don't put spaces between function names and the parens. Kix won't mind, but other languages will throw fits. Best to be consistent.

Glenn

PS - WMIProcessList - latest rev is on the Resources/Kix UDF Lib on my web site. Might be a less-than-current version posted here.
_________________________
Actually I am a Rocket Scientist! \:D

Top
#197437 - 2010-01-19 05:59 PM Re: Couple script issues. [Re: Glenn Barnas]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
Ok, I'm on your site and see the WMIProcessList UDF. I'm new to the UDF stuff.

So your WMIProcessList kix script is a "user developed feature" = UDF? Should I be looking to have my script call that one? Or should I be posting that code into my script to be able to reference it better?

I've been a kix user for years btw, but my work with it has largely involved file moves, copies, ini adjustments, reg key stuff that's pretty simple to piece together.

Thanks again,

TS

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

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Copy the text from the web page and paste it into notepad, saving it as "WMIProcessList.udf" - now you have the beginning of a UDF library.

I prefer to embed the UDFs in my code - so, you can paste it into your script, or you can put the UDF library folder on a network share and reference it in your script as
 Code:
Call '\\server\KixUdfLib\WMIProcessList.udf'
This will load the current copy each time the script runs. Put your Call statements near the top of your script. Embedded UDFs can occur anywhere, but we usually put them at the end.

As I said, I prefer embedding the UDFs in my scripts, as this insures that the script will continue to function if the library is unavailable or I make a change to the UDF that isn't compatible with that script.

There's a KixDev package on my site that includes KGen. This tool reviews your script for dependent UDFs, locates them in your UDF library, and generates a finished script with all dependencies included.

BTW - UDF means User Defined Function.

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

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

Registered: 2010-01-18
Posts: 67
Loc: Oregon
K, saved as WMIProcessList.udf. Placed call to it. Under debug I see it run as soon as the statement for $apptracking goes.

WMIProcessList.udf errors out under debug @ line 130.

Top
#197440 - 2010-01-19 06:27 PM Re: Couple script issues. [Re: Tsguy]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
Does the UDF require a newer version of Kix? I'm running 4.53 on the server this is testing on.

TS

Top
#197441 - 2010-01-19 06:50 PM Re: Couple script issues. [Re: Tsguy]
Tsguy Offline
Getting the hang of it

Registered: 2010-01-18
Posts: 67
Loc: Oregon
I have tried the newest release of Kix and receive the same line error.

I should probably mention that the server this is being tested on is a windows 2003 terminal server. So the EXE being looked for will be running under multiple user sessions:

If you specify a name, all processeses matching that name will be returned,

That comment from the remarks of the UDF concerns me, as it sounds like the script would return all EXE's under all user sessions, vs just the EXE's run by the user executing the script.

From command line the following does get the output specific to the EXE I want and only the user that runs it, I just couldn't figure out how to parse the results within Kix.

 Code:
tasklist /FI "Imagename eq app.exe"

Top
#197444 - 2010-01-19 08:42 PM Re: Couple script issues. [Re: Tsguy]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Is WMI Enabled? Without posting your entire script, we can't tell what's on line 130.

You're right - it isn't user specific. It's possible to track processes per user, but it's a bit more complex. Take a look at WshPipe() UDF to run your TaskList.exe command. The version here on KORG returns a string of all output that you'll need to parse. The enhanced version on my web site returns a 2 element array of StdOut and StdErr, each with an array of lines returned from that data stream.

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

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

Registered: 2010-01-18
Posts: 67
Loc: Oregon
Hi Glenn,

WMI is enabled. The error @ 130 is not coming from my script (it doesn't have 130 lines), it's coming from the WSHProcessList.udf. I will take a look in a bit to see what line is generating the error and post it.

I will take a looksie also at the WshPipe UDF.

thanks,

TS

Top
#197448 - 2010-01-19 11:25 PM Re: Couple script issues. [Re: Tsguy]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
This is probably the problem:
 Code:
;;DEPENDENCIES   WMI, TimeDiff() external UDF 
Download that UDF and include it in your script. It is necessary to calculate the process runtimes.

It's important to read the headers, as they list dependencies such as this. It's why I use KGen to create even simple scripts - it resolves all these dependencies.

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

Top
#197457 - 2010-01-20 04:36 PM Re: Couple script issues. [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
OK - gave an alternative a whirl..
 Code:
$MaxSess = 2
$UsrCmd = 'cmd.exe'
$CmdArg = ' arguments...'
$Cmd = 'tasklist /fi "Username eq %USERDOMAIN%\%USERID%" /fi "Imagename eq ' + $UsrCmd + '"'
$aResult = WshPipe($Cmd)
If UBound($aResult) < ($MaxSess + 1) ; 2 header lines, plus max 2 sessions
  Run $UsrCmd + ' ' + $CmdArg
Else
  'Sorry - already running ' $MaxSess ' instances of ' $UsrCmd '!' @CRLF
EndIf
Tried this on our TS system and it works.

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

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

Registered: 2010-01-18
Posts: 67
Loc: Oregon
Glenn,

Thanks again for the major assist here, feels like we're close. Tried the last bit of code and it steps right through debug no probs, but after two exe's are running it doesn't hit the "ELSE" condition, it just launches a third.

Full script code below (some comments removed, all code posted). The latest code is added at the bottom of the script. I did make some minor adjustments.

 Code:
SetConsole("HIDE")
; Declaring variables
;
; PZB = defines what PZB path will be set within CER.ini
; CER = defines where our user's CER.ini file is saved too, and looked for
; PFX = defines the value for PFX for claimsview
; MENU = defines the value for Menu for Claimsview
;
$pzb="\\root\Production$$"
$cer="\\root\userconfigs$$\@userid\Production"
$pfx="0001"
$Menu="Y"
;
;
$Message="	     You are not allowed to run more than two copies of OurApp.	     "
; Checking for Cer.ini, copying it if it's not there.
;
IF NOT Exist ("\\root\userconfigs$$\@userid")
	MD "\\root\userconfigs$$\@userid"
EndIf
;
IF NOT Exist ("$cer\cer.ini")
	MD "$cer"
	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")
;
;
$MaxSess=2
$Tasklist='tasklist /FI "Username eq %USERDOMAIN%\%USERID%" /FI "Imagename eq App.exe"'
$aResult=WshPipe($Tasklist)
;
If UBound($aResult) < ($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
;
Exit



TS

Top
Page 1 of 2 12>


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

Who's Online
1 registered (Allen) and 382 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.074 seconds in which 0.025 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