efarayenkay
(Just in Town)
2015-05-29 04:31 AM
WMI Query for Win32_Process - getting owner

Hi all

Long time KiXtart user, first time poster. I'm writing a script that lists people using MYOB (an accounting package for those unaware, and yes it does stand for that) because their data file has gotten to the point where it crashes regularly, locking out users and requiring a manual flush of the lock files before they can get in. To make matters worse, sometimes some goose leaves MYOB open and goes to lunch, leaving everyone else in the lurch.

To resolve this problem, I'm writing a script to allow the users to handle this process themselves (as an ASE executable running as an administrator user to give them the access they need without giving them full administrator rights) - not (just) because we're lazy, but because sometimes it happens outside our office hours and if it happens to someone on the west coast (we're east) after 5pm local time, they're stuck until tomorrow. It sort of came to a head when it happened to someone in Perth while they were processing pays...

To this end, I have the following code to look for anyone using that particular data file:

 Code:
Dim $sUser, $sDomain
	
$sFileName = "SNR Dodgy Constructions Pty Ltd.MYO"

$oWmi = GetObject("winmgmts://./root/cimv2")
	
$oEnum = $oWmi.ExecQuery("SELECT * From Win32_Process WHERE CommandLine LIKE '%%$sFileName%%'")

For Each $oInst in $oEnum
	$ = $oInst.getOwner($sUser, $sDomain)
	? "User: " $sUser
Next


Which works great, except it doesn't get the owner. Since KiX doesn't support passing by reference, what (if any) other options do I have for getting this information?

EDIT: KiXtart 4.64, running Windows Server 2008 R2


AllenAdministrator
(KiX Supporter)
2015-05-29 05:29 AM
Re: WMI Query for Win32_Process - getting owner

If you are trying to get the owner of the process there are a couple of known options. The following udf works as long as it has admin privs.

GetProcessOwner()
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=204086#Post204086

How to use UDFs -
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=81943#Post81943

The rest of the UDFs are here -
http://www.kixtart.org/forums/ubbthreads.php?ubb=postlist&Board=7&page=1


The other links that might be useful:
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=203743#Post203743
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=83278

Please post your results.


efarayenkay
(Just in Town)
2015-05-29 06:16 AM
Re: WMI Query for Win32_Process - getting owner

Huh. Didn't even think of using ExecMethod. New script is:

 Code:
Dim $sUser, $sDomain
	
$sFileName = "SNR Dodgy Constructions Pty Ltd.MYO"

$oWmi = GetObject("winmgmts://./root/cimv2")
$iIndex = 3
	
$oEnum = $oWmi.ExecQuery("SELECT * From Win32_Process WHERE CommandLine LIKE '%%$sFileName%%'")

For Each $oInst in $oEnum
	$oParams = $oWMI.ExecMethod($oInst.path_, "getOwner")
	? "User: " $oParams.user
Next


Did exactly what I wanted it to do. Thanks, greatly appreciated. Now to work out how to log off the session for the users it finds...