solitaryrpr has asked for the wisdom of the Perl Monks concerning the following question:

Greetings O Wise and Knowledgable,

I'm stumped...I've tried every example I've found for getting the owner of a process under Windows (within the walls of the monastery as well as the world at large). The only example I've found that acually works uses the 'tasklist' command...I wanted to keep it within perl without relying on an external system call. I've found several examples that use Win32::OLE. Here is the sample code I am using:
use Win32::OLE; # PID of the target process $intPID = 1468; # ------ END CONFIGURATION --------- print "Process PID: $intPID\n"; $objWMIProcess = Win32::OLE->GetObject('winmgmts:\root\cimv2:Win32_Pro +cess.Handle=\'' . $intPID . '\''); print 'Name: ' . $objWMIProcess->Name, "\n"; print 'Command line: ' . $objWMIProcess->CommandLine, "\n"; print 'Description: ' . $objWMIProcess->Description, "\n"; print 'Exe Path: ' . $objWMIProcess->ExecutablePath, "\n"; print 'Parent Process ID: ' . $objWMIProcess->ParentProcessId, "\n"; $objWMIProcess->GetOwner($strUser, $strDomain); print 'Owner: ' . $strDomain . '\\' . $strUser, "\n";
Unfortunately, the Owner is ALWAYS empty (except for the backslash)...for my sanity, please help!

-sol

Replies are listed 'Best First'.
Re: Why doesn't GetOwner work?
by bsdz (Friar) on Jan 07, 2007 at 11:33 UTC
    These methods usually return string variants. Something like this should work: -
    my ($strUser, $strDomain) = ( Variant(VT_BSTR|VT_BYREF, '<undef>'), Variant(VT_BSTR|VT_BYREF, '<undef>') ); $objWMIProcess->GetOwner($strUser, $strDomain); print 'Owner: ' . $strDomain->Value . '\\' . $strUser->Value, "\n";
      Awesome...I had tried something "identical" from a different example but your code actually works...my sanity is saved.