ltwally
ltwally

Reputation: 249

powershell get PID for specific app running for calling user

We have an ERP application with restrictive licensing, which we access via RemoteApp. We need to only allow one instance per user. So, I've been experimenting with PowerShell to try to do this.

What the script has to do is check and see if "pxxi.exe" is running for the calling user.

My first attempt used WMI. It worked, but it turns out WMI is very very slow.

(gwmi win32_process -Filter "name='pxxi.exe'" | ? {$_.getowner().user -eq $env:username}).pid

My second attempt used Get-Process. This works great, but unfortunately requires elevated rights.

Get-Process pxxi -IncludeUserName | ? {$_.username -match $env:username}).id

My third attempt focused on the win32 command line program Tasklist.

$result = Invoke-Command { c:\windows\system32\tasklist.exe /fi "username eq $env:username" /fi "imagename eq pxxi.exe"}

This works! And thanks to EBGreen's code, I can extract just the PID.

($result[3] -split '\s+')[1]

And while that runs really quick as an Administrator, for regular users, Tasklist runs as slow as WMI, above...

So, that puts me back to square one.

Does anyone out there know of a bit of code to quickly check and see if a user XYZ is running a process with name ABC.EXE ?? This needs to work for regular users (but regular users don't need to see other user's processes) and it needs to not take 30+ seconds to run.

I'm not married to powershell. VBScript would be fine. Or any little tool available on the internet.

Thanks!

Upvotes: 0

Views: 456

Answers (2)

ltwally
ltwally

Reputation: 249

I gave up trying to find a way to do it in Powershell. Either the method was too slow, or required admin.

So I wrote something in C#: c# - check if program is running for current user

Upvotes: 0

EBGreen
EBGreen

Reputation: 37720

For the example that you have:

($result[3] -split '\s+')[1]

Be aware that this works for just one result being returned. If you expect more than one instance then you should write a loop to iterate from the 4th item in the array on splitting each item to get that process's PID.

Upvotes: 0

Related Questions