Page 2 of 2 <12
Topic Options
#161227 - 2006-04-28 01:06 AM Re: How to order the startup of programs after login
StarwarsKid Offline
Seasoned Scripter
*****

Registered: 2005-06-15
Posts: 506
Loc: Oregon, USA
I plugged in some test executable names "calc.exe" "notepad.exe" and the appropriate paths to test the script. It seems to hang at the While - Loop section of the script.

Any suggestions?
_________________________
let the wise listen and add to their learning,
and let the discerning get guidance- Proverbs 1:5

Top
#161228 - 2006-04-28 01:22 AM Re: How to order the startup of programs after login
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
while it's "hanging" have you verified that the original process you're trying to kick off is even running or not? you may not even be calling the exe correctly. i don't believe sleep returns an exit code but i'm not sure.

also, paste the code for us so we might be able to catch any coding errors.


Edited by thepip3r (2006-04-28 01:24 AM)

Top
#161229 - 2006-04-28 01:37 AM Re: How to order the startup of programs after login
StarwarsKid Offline
Seasoned Scripter
*****

Registered: 2005-06-15
Posts: 506
Loc: Oregon, USA
Code:
 $prog1 = 'calc.exe'
$prog2 = 'notepad'

SHELL "%SystemRoot%\system32\"+$prog1

While @ERROR <> 0
EnumProc($prog1)
Sleep 1
Loop

sleep 5
;; Now you may want to set a sleep offset depending on
;; whether the condition exists where the program, even
;; though it may be started, needs to wait a few secs
;; before kicking off the other...

SHELL "%SystemRoot%\system32\"+$prog2

Function EnumProc($exe)
Dim $winmgmts, $ExecQuery, $Proc, $ID, $GetObject
$winmgmts = "winmgmts:{impersonationLevel=impersonate}!//" + @wksta
$ExecQuery = "select * from Win32_Process where Name='" + "$exe'"
$GetObject = GetObject($winmgmts).ExecQuery($ExecQuery)
For Each $Proc in $GetObject
$ID = $Proc.ProcessID
$EnumProc = $ID + "|" + $EnumProc
Next
$EnumProc = Left($EnumProc,Len($EnumProc)-1)
$GetObject = ''
EndFunction



Calc.exe launches, WHen checking Task Manager there is a process called, "calc.exe".

I launch the code from a command prompt and I do get a '1' displayed after calc.exe launches.
_________________________
let the wise listen and add to their learning,
and let the discerning get guidance- Proverbs 1:5

Top
#161230 - 2006-04-28 01:37 AM Re: How to order the startup of programs after login
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Well I wouldn't just blindly LOOP either. If the app hangs or otherwise has an issue you will stay in that loop forever.

I'd add time or other method to exit out of the loop if things didn't go as planned.

Yes, adding error checking would be a good thing.
 

Top
#161231 - 2006-04-28 01:41 AM Re: How to order the startup of programs after login
StarwarsKid Offline
Seasoned Scripter
*****

Registered: 2005-06-15
Posts: 506
Loc: Oregon, USA
What would I place in the loop to exit if the application hangs?
_________________________
let the wise listen and add to their learning,
and let the discerning get guidance- Proverbs 1:5

Top
#161232 - 2006-04-28 01:59 AM Re: How to order the startup of programs after login
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
a timer...

something like:

Code:

$timer = 0
While @ERROR <> 0 AND $timer < 100
$timer = $timer + 1
EnumProc($prog1)
Sleep 1
Loop


Top
#161233 - 2006-04-28 02:02 AM Re: How to order the startup of programs after login
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
and if it's returning a 1 on success, verify that is the correct exit code for a success on the UDF and modify the loop so it reflects the proper code. it sounds like it's excuting correctly but @ERROR <> 0 is not the correct condition to check for
Top
#161234 - 2006-04-28 05:43 PM Re: How to order the startup of programs after login
StarwarsKid Offline
Seasoned Scripter
*****

Registered: 2005-06-15
Posts: 506
Loc: Oregon, USA
I ran a small test. Apparently the error code is '0' whether or not the specified executable is running. Also, if I do a "? $proc" it will return the PID if the application is running. If the exe isn't running, there is no PID returned.

Soo, what I need to do is make this script check for the existence of a PID associated to my enumerated process. Could I get some help with that part of the code?

Thanks!
_________________________
let the wise listen and add to their learning,
and let the discerning get guidance- Proverbs 1:5

Top
#161235 - 2006-04-28 06:46 PM Re: How to order the startup of programs after login
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
Quote:

Also, if I do a "? $proc" it will return the PID if the application is running. If the exe isn't running, there is no PID returned.



You just answered your own question. Think about it. If @ERROR isn't the answer, then what logic would you used based off of the above quote to run a loop the way you need to???

Top
#161236 - 2006-04-28 07:04 PM Re: How to order the startup of programs after login
StarwarsKid Offline
Seasoned Scripter
*****

Registered: 2005-06-15
Posts: 506
Loc: Oregon, USA
Okay, Well, let me wing it then. My problem is, I don't know the syntax and have no programming background. KiXtart is the first programming I've ever done, and the only training I've had is kixtart.org. This method of self-learning is excruciatingly slow.

Is this the correct use of While/Loop and a NULL value?

Code:
 
$prog1 = 'calc.exe'
$prog2 = 'notepad'

$Proc = EnumProcess($prog1)

SHELL "%SystemRoot%\system32\"+$prog1

While $proc = NULL
; $proc
Sleep 1
Loop

sleep 5

SHELL "%SystemRoot%\system32\"+$prog2

FUNCTION EnumProcess($exe, optional $terminate, optional $Computer)
Dim $winmgmts, $ExecQuery, $Process, $id, $getobject, $
if not $computer $computer=@wksta endif
$winmgmts="winmgmts:{impersonationLevel=impersonate}!//$COMPUTER"
select
case val($exe)>0
$ExecQuery="select * from Win32_Process where ProcessId='$exe'"
$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
For each $Process in $GetObject
if $terminate $=$Process.Terminate endif
$EnumProcess = $Process.name
next
$GetObject=''
case vartype($exe)=8
$ExecQuery="select * from Win32_Process where Name='$exe'"
$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
For each $Process in $GetObject
if $terminate $=$Process.Terminate endif
$id=$Process.ProcessId
$EnumProcess = "$Id" + "|" + "$EnumProcess"
Next
$EnumProcess=left($EnumProcess,len($EnumProcess)-1)
$GetObject=''
case 1
exit 1
endselect
ENDFUNCTION

_________________________
let the wise listen and add to their learning,
and let the discerning get guidance- Proverbs 1:5

Top
#161237 - 2006-04-28 07:48 PM Re: How to order the startup of programs after login
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
test is out using NULL and if that doesn't work, try replacing NULL with '' but yes... you are on teh right track. i have about as much programming exp as you so i'm just recommending things that i've done in the past.
Top
#161238 - 2006-04-28 09:28 PM Re: How to order the startup of programs after login
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Okay StarWarsKid - here is the general idea to get you going
along with some explanation of what's going on.

There are other ways to code this, but this should get you going.

You might want to add a bit more logic or error checking as required for a production script to
include logging and checking that the logs did write, etc...

Break On
Dim $SO
;Enable global settings of the KiXtart interpreter
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('NoMacrosInStrings','On')

;Declare our vars
Dim $ParentApp, $ChildApp
Dim $WMICheck, $Timer
Dim $CheckParent, $CheckChild

;Set our application process names for use with process check udf
$ParentApp = 'calc.exe'
$ChildApp = 'notepad.exe'

;Check to make sure the application file exists and if so run it
If Exist('%SystemRoot%\system32\'+$ParentApp)
Run '%SystemRoot%\system32\'+$ParentApp
Else
;Add some code to log the error and quit the script or alert the user
Quit @ERROR
EndIf
;Check if WMI is working or not on the system
$WMICheck = WMIConfirm()
If @ERROR
;Add some code to log the error and quit the script or alert the user
Quit @ERROR
Else
If $WMICheck > 0
;WMI tests seem okay continue with script
EndIf
EndIf

;Set a timer so we can get out of the loop if the parent app never runs
$Timer = 0
While @ERROR = 0 And $Timer < 30
$Timer = $Timer + 1
$CheckParent = EnumProcess($ParentApp)
Sleep 1
If $CheckParent > 0
;The parent process was found so
;Set the timer higher to end the loop
$Timer = 60
EndIf
Loop

;Okay we assume that either CheckParent found the app running or the
;timer ran out, so lets check which one happened
$CheckParent = EnumProcess($ParentApp)
If $CheckParent > 0
;Sleep for 5 seconds to give the Parent Application time to startup all the way
Sleep 5
;Now run the Child Application
Run '%SystemRoot%\system32\'+$ChildApp
Else
'Parent process not found to be running' + @ERROR + ' - ' + @SERROR ?
Quit 10
EndIf

;Now check that the Child Application is running
$CheckChild = EnumProcess($ChildApp)
If $CheckChild = ""
'ERROR running child application: ' + @ERROR + ' - ' + @SERROR ?
Else
'Child Application running with process id of: ' + $CheckChild ?
EndIf

;Now exit out of the script
Exit 0

;UDF to confirm that WMI is working correctly
Function WMIConfirm(optional $sComputer)
Dim $objWMIService, $objWMISetting, $colWMISettings
$sComputer = IIf(Not $sComputer,'','\\'+Join(Split($sComputer,'\'),'',3)+'\')
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!"+$sComputer+'root\cimv2')
; Failed - return 0 and exit with error value
If @ERROR
$WMIConfirm = 0
Exit Val('&' + Right(DecToHex(@ERROR), 4))
EndIf
$colWMISettings = $objWMIService.ExecQuery("Select * from Win32_WMISetting")
For Each $objWMISetting In $colWMISettings
$WMIConfirm = $objWMISetting.BuildVersion
Next
Exit 0
EndFunction

;UDF to find the process ID of the applications
Function EnumProcess($exe, optional $terminate, $sComputer)
Dim $winmgmts, $ExecQuery, $Process, $id, $GetObject, $Kill
$sComputer = IIf(Not $sComputer,'','\\'+Join(Split($sComputer,'\'),'',3)+'\')
$winmgmts="winmgmts:{impersonationLevel=impersonate}!"+$sComputer+'root\cimv2'
Select
Case Val($exe)>0
$ExecQuery="select * from Win32_Process where ProcessId=" +'"'+$exe+'"'
$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
For Each $Process In $GetObject
If $terminate $Kill=$Process.Terminate EndIf
$EnumProcess = $Process.name
Next
Case VarType($exe)=8
$ExecQuery="select * from Win32_Process where Name=" +'"'+$exe+'"'
$GetObject=GetObject($winmgmts).ExecQuery($ExecQuery)
For Each $Process In $GetObject
If $terminate $Kill=$Process.Terminate EndIf
$id=$Process.ProcessId
$EnumProcess = ""+$Id + "|" + $EnumProcess
Next
$EnumProcess=Left($EnumProcess,Len($EnumProcess)-1)
Case 1
Exit 1
EndSelect
EndFunction

 

Top
#161239 - 2006-04-29 09:39 AM Re: How to order the startup of programs after login
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
So was this enough information to get you going StarwarsKid ?
Top
#161240 - 2006-05-02 07:48 PM Re: How to order the startup of programs after login
StarwarsKid Offline
Seasoned Scripter
*****

Registered: 2005-06-15
Posts: 506
Loc: Oregon, USA
Extensive. Thanks for the extensive code solution and explanations. I certainly wouldn't have been able to create that on my own. Not even with the best Socratic nudges from the best of the board.

I've been away from the board for a while due to a server crash. Things are back to normal, I hope to test this script against my MDT implementation soon.

Thanks!
_________________________
let the wise listen and add to their learning,
and let the discerning get guidance- Proverbs 1:5

Top
#161241 - 2006-05-10 05:24 AM Re: How to order the startup of programs after login
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
So were you ever able to get around to tesing this? How did it go?
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 507 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

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