BrianTX
|
(Korg Regular)
|
2002-05-17 07:42 PM
|
|
|
|
|
Why doesn't this work?
|
|
I've been fiddling with detecting an open application.. doesn't seem to work.. what's wrong?
code:
break on
$obj = GetObject("winmgmts:Win32_Process") $ok = $obj.Create("Calc")
$obj = ""
$obj2 = GetObject("Winmgmts:").ExecQuery("Select * FROM Win32_Process Where Name = 'calc.exe'") For each $n in $obj2 $name = $n.name Next If $name <> "" "Calculator is active" ? Endif
"Close Calculator here. Press a key to continue." ? get $_
$name = "" $obj3 = GetObject("Winmgmts:").ExecQuery("Select * FROM Win32_Process Where Name = 'calc.exe'") For each $n in $obj3 $name = $n.name Next If $name <> "" "Calculator is active" ? Endif
I also notice that I have to press a key TWICE for this to complete. I'm slightly confused..
Brian [ 17 May 2002, 19:45: Message edited by: BrianTX ]
|
|
Re: Why doesn't this work?
|
|
Are there any errors generated in your script? I'd put an error startement behind every COM line just to be sure that it all works correctly, like code:
break on
$obj = GetObject("winmgmts:Win32_Process") ? ''+@ERROR+' - '+@SERROR $ok = $obj.Create("Calc") ? ''+@ERROR+' - '+@SERROR
$obj = ""
$obj2 = GetObject("Winmgmts:").ExecQuery("Select * FROM Win32_Process Where Name = 'calc.exe'") ? ''+@ERROR+' - '+@SERROR For each $n in $obj2 $name = $n.name ? $name ? ''+@ERROR+' - '+@SERROR Next If $name <> "" ? "Calculator is active" Endif
? "Close Calculator here. Press a key to continue." get $_
$name = "" $obj3 = GetObject("Winmgmts:").ExecQuery("Select * FROM Win32_Process Where Name = 'calc.exe'") ? ''+@ERROR+' - '+@SERROR For each $n in $obj3 $name = $n.name $name ? ''+@ERROR+' - '+@SERROR Next If $name <> "" ? "Calculator is active" Endif
The code posted above works like a charm for me.
|
Shawn
|
(KiX Supporter)
|
2002-05-17 08:06 PM
|
|
|
|
|
Re: Why doesn't this work?
|
|
Might try checking for the returned count of processes from your second enumeration - instead of running through the list, eg:
if $obj3.count ; instances of calc exist else ; not endif
-Shawn
|
|
Re: Why doesn't this work?
|
|
Wouldn't this one be even easier? code:
SELECT Count(Name) FROM Win32_Process WHERE Name = 'calc.exe'
This should return only the number of CALC.EXE processes. [ 17 May 2002, 20:15: Message edited by: sealeopard ]
|
Chris S.
|
(MM club member)
|
2002-05-17 09:04 PM
|
|
|
|
|
Re: Why doesn't this work?
|
|
Doesn't WMIQuery2 already do this?
code:
? WMIQuery2("Processes on","Win32_Process") FUNCTION WMIQuery2($what,$where,) $wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!//" + @wksta + "/root/cimv2") $list = "" $objs = $wmi.instancesof($where) for each $obj in $objs $list = $list + $obj.description + chr(13) + chr(10) next $list=Left($list, Len($list)) ?$what +" "+ @wksta + chr(13) + chr(10) + $list ENDFUNCTION
|
BrianTX
|
(Korg Regular)
|
2002-05-17 09:46 PM
|
|
|
|
|
Re: Why doesn't this work?
|
|
Yes, it does enumerate processes, but what I was trying to do is track a specific process. I'm still fiddling around with it.
Brian
|
BrianTX
|
(Korg Regular)
|
2002-05-17 10:16 PM
|
|
|
|
|
Re: Why doesn't this work?
|
|
Here is the easy way to monitor if the application is open and the number of instances open:
code:
Function AppInstances($appname) $countproc = 0 For Each $process in GetObject("Winmgmts:").ExecQuery("Select Name FROM Win32_Process Where Name = '" + $appname + "'") $countproc = $countproc + 1 Next $AppInstances = $countproc EndFunction
Call the function:
code:
$app = "Calc.exe" $app + " has " + AppInstances($app) + " active process(es)." ?
This could possibly be useful if using task scheduler via logon script and wanting to wait for a task to complete before continuing...
Brian
P.S....
After thinking about it, I believe with a client and serverside script, it would be able to create a UDF called
RunAsAdministrator($commandline)
1. Client script passes the $commandline to server script. 2. Server script generates a scheduled task on the PC from the $commandline sent. 3. Client waits until program complete before continuing. [ 17 May 2002, 22:31: Message edited by: BrianTX ]
|