1

I need to get some procs by the owner. My demo script below will first look for procs by owner locally, then it will do the same thing, but it invokes the command on the same box:

cls
write-host 'LOCAL CALL: '
$procs = @(Get-WmiObject win32_process |? {($_.getowner().user -eq 'APP_ACCOUNT') })
write-host $procs.count

$func = {
$procs = @(Get-WmiObject win32_process |? {($_.getowner().user -eq 'APP_ACCOUNT') })
write-host $procs.count
}

write-host 'REMOTE CALL: '
$session = New-PSSession -ComputerName 'SERVER'
$job = Invoke-Command -Session $session -ScriptBlock $func -AsJob
Wait-Job -Job $job    
$job | Receive-Job
$job | Remove-Job
Remove-PSSession -Session $session

Most of the time when I run my script it errors with the following output:

LOCAL CALL:
38
REMOTE CALL: 

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
26     Job26           RemoteJob       Completed     True            SERVER               ...                      
Exception calling "GetOwner" : "Not found "
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException
    + PSComputerName        : SERVER

38

So that first 38 is the number of procs it found for the owner running locally. It finds 38 the second time as well, but errors calling getowner. I don't understand why since it worked the first time. Is it operating in some kind of "bubble" when I invoke the command? In my larger script this is causing me more severe issues as the job state goes to failed and execution halts even though it is throwing the same error. One problem at a time though.

JDH
  • 2,618
  • 5
  • 38
  • 48

2 Answers2

2

Seems I needed to do a better job of making sure my processes still exist before filtering by owner:

    $procs = @()
    $allProcs = @(Get-WmiObject win32_process)
    foreach($proc in $allProcs)
    {
        $procActive = get-process -Id $proc.processId -ErrorAction SilentlyContinue
        if($procActive)
        {
            if($proc.getowner().user -eq 'jdholbrook')
            {
                $procs += $proc
            }
        }
    }
    write-host $procs.count
JDH
  • 2,618
  • 5
  • 38
  • 48
  • 1
    This is a good solution, but I think using `try/catch` on `$_.getowner()` would be even better/safer, and probably more elegant. – Davor Josipovic Jun 18 '13 at 15:29
  • Maybe I'll change my example to that as time allows. I wanted to document a working example in case someone else comes across this topic. Feel free to add that to your answer and I'll assign that as the solution. – JDH Jun 18 '13 at 16:44
1

This is probably because the process for which you want to query the owner doesn’t exist anymore.

You can simulate this behaviour on your local PC as follows:

Start some application, like notepad.exe for example. Now run:

$w = (Get-WmiObject win32_process) # Your notepad process will now be the last in the `$w` array.

Close the notepad.exe process.

Now pipe the contents of $w to get the owners:

$w | % {$_.getowner()}

For the last object you will get:

Exception calling "GetOwner" : "Not found "
At line:1 char:20
+ $w | % {$_.getowner <<<< ()}
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

To make sure this is the notepad.exe you just closed you can double-check:

$w[-1]; # last object
$w[-1].getowner(); # error

So, now you know what is causing, you can start thinking about how to handle it...

Davor Josipovic
  • 5,296
  • 1
  • 39
  • 57
  • davor there is one piece here confusing. I run my demo script multiple times and repeatedly get 38 procs. I don't really see the count fluctuating at all. Also, I'm not delaying from the time I find the procs to when I call getowner on them, I'm piping the results of get-wmiobject right to my getowner call so I would expect negligible delay. Certainly not the kind of delay where this would be reproducible. Also, the fact that it only seems to happen during the remote call is very troubling. – JDH Jun 17 '13 at 12:46
  • @JustinHolbrook _I'm piping the results of get-wmiobject right to my getowner call so I would expect negligible delay._ I am not so sure about the inner working of `Get-WmiObject win32_process`. It might as well cache the process list before starting to pipe. Anyway, you can always log the processes and their getowners. I think you will learn much once you discover which process causes this error. I am curious abut your findings. – Davor Josipovic Jun 17 '13 at 16:19
  • I just realized its probably not my processes that are the culprit for the error. I'm getting all processes then piping them to getowner. Since it takes longer when I call this remotely it probably introduces enough delay for some of the processes to terminate. This explains why the count on the processes I'm interested in remains constant. – JDH Jun 18 '13 at 13:15