Page 1 of 1 1
Topic Options
#85708 - 2002-05-17 07:42 PM Why doesn't this work?
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
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 ]

Top
#85709 - 2002-05-17 08:01 PM Re: Why doesn't this work?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
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.
_________________________
There are two types of vessels, submarines and targets.

Top
#85710 - 2002-05-17 08:06 PM Re: Why doesn't this work?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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

Top
#85711 - 2002-05-17 08:15 PM Re: Why doesn't this work?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
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 ]
_________________________
There are two types of vessels, submarines and targets.

Top
#85712 - 2002-05-17 09:04 PM Re: Why doesn't this work?
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
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


Top
#85713 - 2002-05-17 09:46 PM Re: Why doesn't this work?
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
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

Top
#85714 - 2002-05-17 10:16 PM Re: Why doesn't this work?
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
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 ]

Top
Page 1 of 1 1


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

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

Generated in 0.058 seconds in which 0.023 seconds were spent on a total of 12 queries. Zlib compression enabled.

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