|
-
Dec 4th, 2009, 06:46 AM
#1
Thread Starter
Junior Member
[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....
Last edited by StianR; Dec 4th, 2009 at 06:53 AM.
-
Dec 5th, 2009, 10:27 AM
#2
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
-
Dec 5th, 2009, 12:53 PM
#3
Thread Starter
Junior Member
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
-
Dec 7th, 2009, 02:56 AM
#4
Thread Starter
Junior Member
-
Dec 7th, 2009, 04:24 AM
#5
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) ?
-
Dec 7th, 2009, 08:02 AM
#6
Thread Starter
Junior Member
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
Last edited by StianR; Dec 7th, 2009 at 08:05 AM.
-
Dec 7th, 2009, 08:42 AM
#7
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
-
Dec 7th, 2009, 09:23 AM
#8
Thread Starter
Junior Member
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
-
Dec 7th, 2009, 09:28 AM
#9
Re: [RESOLVED] Check if a process is running on a remote computer.
no worries
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|