找出谁在远程机器上运行进程

Posted

技术标签:

【中文标题】找出谁在远程机器上运行进程【英文标题】:Find out who is running the process on the remote machine 【发布时间】:2012-02-26 12:54:43 【问题描述】:

我正在尝试获取远程计算机上运行的进程列表以及运行它们的用户名。到目前为止,我得到了:

Dim ps As System.Diagnostics.Process
For Each ps In System.Diagnostics.Process.GetProcesses("myserver")
    ListBox1.Items.Add(ps.ProcessName)
Next

【问题讨论】:

运行代码时会发生什么?你有权限访问 myserver 吗? 代码运行良好!我只需要知道哪个属性给了我运行进程的用户名 【参考方案1】:

How to get permissions for using System.Diagnostics.Process.GetProcess(string)? - 这可能是一个更好的方法。它是用 C# 编写的,如果你愿意,我可以翻译它。

''' using System.Management;  
' don't forget! in VS you may have to add a new reference to this DLL
 Dim op As New ConnectionOptions()
 op.Username = "REMOTE_USER"
 op.Password = "REMOTE_PASSWORD"

Dim sc As New ManagementScope("\\REMOTE_COMPUTER_NAME\root\cimv2", op)

Dim query As New ObjectQuery("Select * from Win32_Process")

 Dim searcher As New ManagementObjectSearcher(sc, query)
 Dim result As ManagementObjectCollection = searcher.[Get]()

 For Each obj As ManagementObject In result
     If obj("Caption") IsNot Nothing Then
         Console.Write(obj("Caption").ToString() & vbTab)
     End If
     If obj("CommandLine") IsNot Nothing Then
         Console.WriteLine(obj("CommandLine").ToString())
         End If
Next



 Public Function GetProcessOwner(processId As Integer) As String
    Dim query As String = "Select * From Win32_Process Where ProcessID = " & processId
         Dim searcher As New ManagementObjectSearcher(query)
     Dim processList As ManagementObjectCollection = searcher.[Get]()

     For Each obj As ManagementObject In processList
         Dim argList As String() = New String() String.Empty, String.Empty
         Dim returnVal As Integer = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList))
         If returnVal = 0 Then
        ' return DOMAIN\user
             Return argList(1) & "\" & argList(0)
         End If
     Next

     Return "NO OWNER"
 End Function

【讨论】:

以上是关于找出谁在远程机器上运行进程的主要内容,如果未能解决你的问题,请参考以下文章

验证远程主机以运行 ms-mpi 进程

如何使用 C# 在远程机器上启动/停止进程?

如何找出CMD.exe进程在远程计算机上运行的脚本?

如何最好地更新远程机器上正在运行的应用程序

visualvm 可以通过 JMX 自动连接到远程进程吗?

直接在机器上启动进程和从远程桌面连接启动进程之间的区别