Link to home
Start Free TrialLog in
Avatar of baz_can_fix_it
baz_can_fix_it

asked on

VB .NET WMI/API Current User logged on a remote Machine via Domain

Hi Experts

I am trying to get a userid or username of the current user logged in to remote Machin via Domain, with no local profile....Any one can help...I have tried all these, but all i get is list of users that have logged in to that machine and I don't know which is the current one...


'ColX = objWMI.InstancesOf("Win32_NetworkLoginProfile")
'ColX = objWMI.InstancesOf("Win32_ComputerSystem")
'ColX = objWMI.InstancesOf("Win32_NTLogEvent")
'ColX = objWMI.InstancesOf("Win32_NetworkLoginProfile")
'ColX = objWMI.ExecQuery("Win32_NetworkLoginProfile")
'ColX = objWMI.InstancesOf("Win32_Process")
'ColX = objWMI.InstancesOf("WIN32_Desktop")
'ColX = objWMI.InstancesOf("WIN32_Registry")
'ColX = objWMI.InstancesOf("WIN32_StartupCommand")
'ColX = objWMI.InstancesOf("WIN32_UserAccount")
'ColX = objWMI.InstancesOf("WIN32_Account")


If the above is impossible is there a flag to find out if there is a person logged in to that Remote Machine?

Thank you in advance
Avatar of mail2dolly
mail2dolly

Hi,

You can use the environment variable USERNAME to read the currently logged in user name.
[try typing set username in your command prompt]

This is easier than using WMI script ;)

Regards,
Dolly
Change the variable with an IP and you have to be a local admin on the machine you are running this against.

strComputer = "RemoteComputer"

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 
For Each objComputer in colComputer
    Wscript.Echo objComputer.UserName
Next
 
Avatar of Bob Learned
What is wrong with this?  Does it not work for users without local profiles?  I don't not have the ability to test this, but I know that it works for me.

(1) Add a System.Management.dll reference to the project.

(2) Add the following code:

 Private Function CurrentLoginForMachine(ByRef machineName As String) As String

      Dim pathMachine As New Management.ManagementPath("\\" & machineName & "\root\cimv2")
      Dim scopeRemoteMachine As New Management.ManagementScope(pathMachine)
      Dim queryUser As New Management.ObjectQuery( _
         "SELECT * FROM Win32_ComputerSystem")

      Dim userCurrent As String = String.Empty

      Dim searcherUser As New Management.ManagementObjectSearcher( _
         scopeRemoteMachine, queryUser)

      Dim objectUser As Management.ManagementObject

      For Each objectUser In searcherUser.Get

         If Not objectUser("UserName") Is Nothing Then
            userCurrent = objectUser("UserName").ToString
         End If

      Next

      If userCurrent Is String.Empty Then
         userCurrent = "None"
      End If

      Return userCurrent

   End Function

(3)  Usage for the call is CurrentLoginForMachine("MACHINE"), where machine is the computer name (without the leading '\\'.
Avatar of baz_can_fix_it

ASKER

mail2dolly

Hi, I need to find this for 800 Machines on the network, not just localhost....I have no problems getting this using wmi for localhost, its the remote machines that are giving me multiple usernames....

unless you can think of a way to run SET remotley to all machines...

Cheers
Hi TheLearnedOne
I have tried similar solutions to what you gave me and the problem is that since the query is on a remote host on a different domain once we are connected to the remote host and query the win32_computersystem or network login profile the result is multiple users for one host. Now we know that most of the usernames that we retrieve are previous users who logged of but we could not pin point whether there is currently a user logged on or not. :)
This is the way that I tested the procedure:

(1) Log into a machine, with a local profile, that has multiple local profiles.
(2) Run the test (result = Name).
(3) Log out of the machine
(4) Run the test (result = None).

It is interesting that you are getting multiple results from this query.

Bob
From MSDN on Win32_ComputerSystem:

UserName
Name of a user that is logged on currently. This property must have a value. In a terminal services session, UserName returns the name of the user that is logged on to the console—not the user logged on during the terminal service session.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_computersystem.asp

I don't see how you can get multiple results.  Verrry interrresting!:)
RE:

This is the way that I tested the procedure:

(1) Log into a machine, with a local profile, that has multiple local profiles.

The Profile are not Local they are Roaming Profile...That where are problem is...I have tested it on two local Machines and it works but when it comes to users on Domain with Roaming profile it gives us multiple users.... i.e. when I use a FOR LOOP if I don't use a FOR LOOP it gives me a single User but it's not the person who is logged in, and if there is no one logged in it still gives me multiple users, my guess is either there is an specific way to do this or the method or policies used for the roaming users are different to standards...

I have just about tried every thing with WMI, would there be any different way of doing this like SET or using an API to connect to the Security side of windows instead of Management side....I will increase the point to 250 hence its getting a bit harder... :)


Cheers
Ok

I am getting a bit closer using Win32_NetworkLoginProfile again I have noticed if I use LastLogon option it uses a flag like this **************.******+*** when the PC still is in use by a user...Its a time consuming way but seems to be working...I will still accept any better ideas...


Cheers
How is it coming?

Bob
hi TheLearnedOne

I have found a diffrent approach using NBTSAT -a Hostname, i am reading from a list of hosts and creating a btch file on the fly...I am just having a hard time to read from the results...

I am still open to any other suggestions....


Cheers
Is there anything from Active Directory services that might help?

Bob
We do use the LDAP to authenticate as well but how would that help?

Cheers mate
I don't know much about ADSI, but I thought maybe that it might help.  I tend to stick to using WMI for a lot of things here.  I usually like to take the WMI questions, especially this one.  I can really learn something here.  If you ever figure out a solution let me know.

Bob
Hi TheLearnedOne

How about you help me out on this instead and I'll give you the point....

As i mention NBSTAT is working great, I ping the host first, if its alive I get my info, I am doing this for about 400 PC's to begin with... Takes about 3 min, which is not bad...The problem I am having is using Regular Expression to extract the data I need here is what the file looks like...

Local Area Connection:
Node IpAddress: [100.90.16.22] Scope Id: []



           NetBIOS Remote Machine Name Table



       Name               Type         Status

    ---------------------------------------------

    1-1            <00>  UNIQUE      Registered

    STUDENT      <00>  GROUP       Registered

    1-1            <03>  UNIQUE      Registered

    1-1            <20>  UNIQUE      Registered

    STUDENT      <1E>  GROUP       Registered



    MAC Address = 00-D0-B7-B2-D3-12


Local Area Connection:
Node IpAddress: [100.90.16.22] Scope Id: []



           NetBIOS Remote Machine Name Table



       Name               Type         Status

    ---------------------------------------------

    1-5            <00>  UNIQUE      Registered

    STUDENT      <00>  GROUP       Registered

    1-5            <03>  UNIQUE      Registered

    1-5            <20>  UNIQUE      Registered

    STUDENT      <1E>  GROUP       Registered

    U111213        <03>  UNIQUE      Registered



    MAC Address = 00-D0-B7-B2-D3-11

and so on...

If a user is logged on it will have a extra line at the end like the second one  "U111213        <03>  UNIQUE      Registered" that means the user U111213 is currently logged on that machine...

The only way i can think of is to use an expression sayin look for a tag <1E> ignore the words  "GROUP"   and   "Registered"
if next word is  "MAC Address"  then computer is available if its not pickup the next word in (second case would be) "U111213" until you reach the <03> tag and so on....

Any ideas how i would do this....

Cheers


   
strFileCont = ReadFile("temp2.txt")
                Dim strF() As String = Split(strFileCont, "<1E>")
                Dim strF2() As String = Split(strF(1), vbCrLf)
                If strF2(1).Length < 2 Then
                    DT.Rows.Add(New Object() {strPcName, "FREE"})
                Else
                    Dim tempstr() As String = Split(strF2(1), " ")
                    DT.Rows.Add(New Object() {strPcName, tempstr(4)})
                End If
Hi TheLearnedOne

I used the above...seems to work...any way I am going to close the question....Thanx for trying to help...

Cheers mate
ASKER CERTIFIED SOLUTION
Avatar of PashaMod
PashaMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial