[RESOLVED] Check if a process is running on a remote computer.
Hi.
I need to check if the explorer.exe process is running on a remote computer to verify that there is a user logged on the computer. I Also want to get the Process Owner of the explorer.exe process to check which user is logged on. I have found some VBscript examples online, but i can't seem to get them to work. If anyone knows of a better way of checking if and what user is logged on to a computer, please give me a reply also. I have tried System.Diagnostics.Process.GetProcesses, but i can't seem to get the owner of the process, maybe WMI will do the trick ? Anyone have som VB code for this task....
:)
Re: Check if a process is running on a remote computer.
Checking to see if explorer.exe is running is not the best way to check if someone is logged on really... I'm not a fan of WMI but its probably the easiest way to do it. I found this code on google (did you not try searching there first?) http://snippets.dzone.com/posts/show/5478 and tidied it up a bit to come up with this, which seems to work fine on the 3 servers I just tested it on:
vb Code:
Dim Strcomputer As String = "ENTER COMPUTER NAME HERE"
Dim WMIConnectionOptions As New ConnectionOptions
WMIConnectionOptions.Impersonation = System.Management.ImpersonationLevel.Impersonate
WMIConnectionOptions.Authentication = System.Management.AuthenticationLevel.Packet
Dim WMIScope As New Management.ManagementScope("\\" & _
Strcomputer & "\root\cimv2", WMIConnectionOptions)
Dim oq As New System.Management.ObjectQuery("SELECT UserName from Win32_ComputerSystem")
Dim query As New ManagementObjectSearcher(WMIScope, oq)
Dim queryCollection As ManagementObjectCollection = query.Get()
For Each oReturn As ManagementObject In queryCollection
If Not oReturn("UserName") Is Nothing Then
MessageBox.Show(oReturn("UserName").ToString)
Else
MessageBox.Show("Not logged in")
End If
Next
Re: Check if a process is running on a remote computer.
Hi Chris, and thank you for your reply. I found some examples online, but i guess i do not have the required knowledge to get it to work. Thank again for your effort, i will try it first thing on monday when i'm back at work :)
1 Attachment(s)
Re: Check if a process is running on a remote computer.
Hi Again.
This code did not work i'm affraid :(. It reports back my username as logged in even if i log out the RDP session i'm using to test the code. I am creating an application that checks which of our HP ProLiant xw460c Blade Workstation is available for a HP RGS connection. When the user connects to a available Workstation, the RGS receiver software will make a connection in the background. Everything seems to work using different WMi queries, except the "check if a user is logged in" part.
Re: Check if a process is running on a remote computer.
Are you sure you are actually logging the RDP connection off and not just closing the RDP window (which leaves your session running) ?
Re: Check if a process is running on a remote computer.
Hi.
Yes i log off the RDP session. I have managed to get together some code which invokes the "GetOwner" method of the Win32_Process. but i now get the following exception :
System.InvalidOperationException: Operation is not valid due to the current state of the object.
at System.Management.ManagementObject.InvokeMethod(String methodName, Object[] args)
at GeoConnect.MainWindow.CheckLoginState(String strHostName) in D:\GeoConnect\GeoConnect\MainWindow.xaml.vb:line 123
It breaks on the following line: ExProcess.InvokeMethod("GetOwner",CType(ProcReturn, Object()))
I guess i'm doing something wrong with the ProcReturn variable.
Here is my current code:
GetUserName Code:
' Check if a user is logged on to a remote host. Return the user name if true.
Private Function CheckLoginState(ByVal strHostName As String) As String
Try
' WMI Connection Options.
Dim WMIConnectionOptions As New ConnectionOptions
WMIConnectionOptions.Impersonation = System.Management.ImpersonationLevel.Impersonate
WMIConnectionOptions.Authentication = System.Management.AuthenticationLevel.Packet
' WMI Query.
Dim WMIScope As New Management.ManagementScope("\\" & strHostName & "\root\cimv2", WMIConnectionOptions)
Dim MyQuery As New System.Management.ObjectQuery("Select Name from Win32_Process Where Name='explorer.exe'")
Dim MySearcher As New ManagementObjectSearcher(WMIScope, MyQuery)
Dim MyResults As ManagementObjectCollection = MySearcher.Get()
If Not MyResults.Count = 0 Then
For Each ExProcess As ManagementObject In MyResults
Dim ProcReturn(1) As String
ExProcess.InvokeMethod("GetOwner", CType(ProcReturn, Object()))
Return (ProcReturn(1)).ToString
Next
End If
Return "None"
Catch ex As Exception
MessageBox.Show(ex.ToString)
Return "Error"
End Try
End Function
Re: Check if a process is running on a remote computer.
Change this line:
Code:
Dim MyQuery As New System.Management.ObjectQuery("Select Name from Win32_Process Where Name='explorer.exe'")
to this:
Code:
Dim MyQuery As New System.Management.ObjectQuery("Select * from Win32_Process Where Name='explorer.exe'")
and change this line:
Code:
ExProcess.InvokeMethod("GetOwner", CType(ProcReturn, Object()))
to this:
Code:
ExProcess.InvokeMethod("GetOwner", ProcReturn)
annnd this line:
Code:
Return (ProcReturn(1)).ToString
to this:
Code:
Return (ProcReturn(0)).ToString
because ProcReturn(1) will be the user's domain - ProcReturn(0) will be the username.
worked most of that out by looking at the MSDN page for the method you are trying to invoke: http://msdn.microsoft.com/en-us/libr...60(VS.85).aspx
Re: Check if a process is running on a remote computer.
Thank you Chris. That worked perfectly. I can see the changes you made makes sense. I guess i was getting blind while tring to get it to work. Once again Big thanks :)
Re: [RESOLVED] Check if a process is running on a remote computer.