tags:

views:

1208

answers:

2

I work off of a multi-user Windows Server, and the rdpclip bug bites us all daily. We usually just open task manager and kill then restart rdpclip, but that's a pain in the butt. I wrote a powershell script for killing then restarting rdpclip, but no one's using it because it's a script (not to mention the execution policy is restricted for the box). I'm trying to write a quick and dirty windows app where you click a button to kill rdpclip and restart it. But I want to restrict it to the current user, and can't find a method for the Process class that does this. So far, here's what I have:

Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist)
{
    if (theprocess.ProcessName == "rdpclip")
    {
      theprocess.Kill();
      Process.Start("rdpclip");
    }
}

I'm not certain, but I think that's going to kill all the rdpclip processes. I'd like to select by user, like my powershell script does:

taskkill /fi "username eq $env:username" /im rdpclip.exe
& rdpclip.ex

I suppose I could just invoke the powershell script from my executable, but that seems fairly kludgy.

Apologies in advance for any formatting issues, this is my first time here.

UPDATE: I also need to know how to get the current user and select only those processes. The WMI solution proposed below doesn't help me get that.

UPDATE2: Ok, I've figured out how to get the current user, but it doesn't match the process user over Remote Desktop. Anyone know how to get username instead of the SID?

Cheers, fr0man

A: 

Read the following CodeProject article, it has all the information you need:

How To Get Process Owner ID and Current User SID

0xA3
Looks good, except I can't get VS to recognized ObjectQuery, even with a Using System.Management. This is an area I'm wholly unfamiliar with.
fr0man
Ah, nevermind. That's only supported in .NET 2.0 and below. Fixed now.
fr0man
Personally, I would go the Win32/PInvoke route rather than WMI in this case. However, that's just preference, I think the WMI method would work just as well.
BobbyShaftoe
+3  A: 

Ok, here's what I ended up doing:

           Process[] processlist = Process.GetProcesses();
            bool rdpclipFound = false;

            foreach (Process theprocess in processlist)
            {
                String ProcessUserSID = GetProcessInfoByPID(theprocess.Id);
                String CurrentUser = WindowsIdentity.GetCurrent().Name.Replace("SERVERNAME\\",""); 

                if (theprocess.ProcessName == "rdpclip" && ProcessUserSID == CurrentUser)
                {
                    theprocess.Kill();
                    rdpclipFound = true;
                }

            }
            Process.Start("rdpclip");
            if (rdpclipFound)
            {
               MessageBox.Show("rdpclip.exe successfully restarted"); }
            else
            {
               MessageBox.Show("rdpclip was not running under your username.  It has been started, please try copying and pasting again.");
            }

            }
fr0man
I had to replace WindowsIdentity.GetCurrent().Name with WindowsIdentity.GetCurrent().User.Value for this to work.
Mauricio Scheffer
+1 thanks....that was what i was looking for
Pankaj Kumar