开发者

.NET - get processes running on a machine in another domain

开发者 https://www.devze.com 2023-04-12 13:39 出处:网络
I am looking for a way, from .NET code, to get the list of running processes on a remote machine, which is on a different domain.

I am looking for a way, from .NET code, to get the list of running processes on a remote machine, which is on a different domain.

I have the username and password for开发者_Python百科 the machine. However, the System.Diagnostics.Process.GetProcesses() function only has two overrides. One of them accepts a remote machine's name, but no way to send the username and password to that machine. In order for the punciton to work, it would have to be a machine on your domain that you already have access to.

I'm not 100% sure if this can be done, but I suspect it could be done via Interop and WMI.

If it matters, the .NET app is running on a Windows 7 monitoring machine that needs to connect to a Windows XP machine out in one of our retail locations.

I have tried searching for previous questions and tried Google before posting this question. If there is a duplicate question out there, please let me know and I'll delete this question.


I hate answering my own questions, but for the next person to face this challenge, I did find an easier way to do this via the System.Management objects.

There's a full working application with source code at codeproject.com.

http://www.codeproject.com/KB/cs/Remote_process_controller.aspx

I modified it quite a bit for my specfic purpose, and got the smallest possible funciton to do this. I'm sharing it for the next person to come along.

Note, that you will need to add a reference to System.Management to your project.

    private bool CheckForExpectedProcess(string userName, string password, string machineName, string logonDomain, string PartialProcessname)
    {
        bool ReturnValue = false;
        System.Management.ManagementScope managementScope;
        try
        {
            System.Management.ConnectionOptions connOptions = new System.Management.ConnectionOptions();

            connOptions.Impersonation = System.Management.ImpersonationLevel.Impersonate;
            connOptions.EnablePrivileges = true;
            connOptions.Username = logonDomain + "\\" + userName;
            connOptions.Password = password;
            managementScope = new System.Management.ManagementScope(@"\\" + machineName + @"\ROOT\CIMV2", connOptions);

            managementScope.Connect();
            System.Management.ManagementObjectSearcher objSearcher = new System.Management.ManagementObjectSearcher("SELECT Name FROM Win32_Process WHERE NAME LIKE'%" + PartialProcessname + "%'");
            System.Management.ManagementOperationObserver opsObserver = new System.Management.ManagementOperationObserver();
            objSearcher.Scope = managementScope;
            string[] sep = { "\n", "\t" };

            System.Management.ManagementObjectCollection  objects = objSearcher.Get();
            ReturnValue = objects.Count > 0;

        }
        catch (Exception ex)
        {
           // handle error - we log it to our own system, but it's up to you.
        }

        return ReturnValue;
    }


What you are looking for is implementing an equivalent to runas /networkonly where the process identity on the local machine is unchanged, while any outbound network access is done using specified credentials.

According to an SO post How to build RUNAS /NETONLY functionality into a (C#/.NET/WinForms) program? you need to use CreateProcessWithLogonW with LOGON_NETCREDENTIALS_ONLY.


You could try this tool , which exactly fulfills your requirement. You could monitor remote computers from any domain. This tool allows to monitor the remote machine processes, installed software, devices, services, sessions, etc...

0

精彩评论

暂无评论...
验证码 换一张
取 消