#161227 - 2006-04-2801:06 AMRe: How to order the startup of programs after login
StarwarsKidStarwarsKid
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
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.
#161229 - 2006-04-2801:37 AMRe: How to order the startup of programs after login
StarwarsKidStarwarsKid
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
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
#161234 - 2006-04-2805:43 PMRe: How to order the startup of programs after login
StarwarsKidStarwarsKid
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
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???
#161236 - 2006-04-2807:04 PMRe: How to order the startup of programs after login
StarwarsKidStarwarsKid
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
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.
#161238 - 2006-04-2809:28 PMRe: How to order the startup of programs after login
NTDOCNTDOC 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...
#161240 - 2006-05-0207:48 PMRe: How to order the startup of programs after login
StarwarsKidStarwarsKid
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