Link to home
Start Free TrialLog in
Avatar of dgore1
dgore1

asked on

Determining who is logged onto a remote workstation using vbs script

The little VBS script below works like a charm for finding a remote workstation and listing the user that is logged on.

strComputer=InputBox("What is your computer name? ie WSCI320CXXX:")
WScript.echo "The name you entered was " &strComputer
'WScript.echo "The user name will appear in seconds!"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 
For Each objComputer in colComputer
    Wscript.Echo "Logged-on user: " & objComputer.UserName
Next

However, If the computer is not on the network I get this error:
Microsoft VBScript runtime error '800a01ce'
The remote server machine does not exist or is unavailable: 'GetObject'

Also, sometimes the machine is on the network and the above error is displayed.
Is there any code that can be added to just say on error the machine does not exist?

Thanks,
Dale

Avatar of sirbounty
sirbounty
Flag of United States of America image

You could skip the error:

On Error Resume Next 'continue when an error occurs
strComputer=InputBox("What is your computer name? ie WSCI320CXXX:")
WScript.echo "The name you entered was " & strComputer
'WScript.echo "The user name will appear in seconds!"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If Err.Number <> 0 Then
  wscript.echo "There was a problem locating the computer."
  wscript.quit
End If
Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 
For Each objComputer in colComputer
    Wscript.Echo "Logged-on user: " & objComputer.UserName
Next
Avatar of dgore1
dgore1

ASKER

I noticed 2 problems...1 the script only finds my computer, no remote computers at all, even though I have them turned on and can ping them by name or IP...

After adding the code above I get the same error, it seems to skip the if Err.Number

Any ideas?

thanks,
Dale
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

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
Avatar of dgore1

ASKER

That worked great!  Except for one problem...I noticed I'm not really trying to find the remote computer but the person who is logged on.  So, is the error I'm getting not able to find the computer or the user..if no one is logged on , it might return an error...hmmmm...ping the machines and they are up and running...maybe logon another machine and make sure...otherwise I'm at a loss...works great on my machine though...tells me I'm logged on!!!

Any other ideas?  Did I miss something in the code?
Hmm - I'm confused...did this script really "work like a charm" as you claimed?
Cause I've not modified your processing.
I don't mind helping - but I need know what we're doing here...

Initially it seemed that you were only requesting help for the systems that were not turned on...now you're telling me that it doesn't work at all for remote processing?  Which is it?
Avatar of dgore1

ASKER

OK...here is what it was supposed to do:

Give me the person who was logged onto a remote computer.

I was wrong, I thought it was getting the remote computer name.
The script seemed to be perfect and work but I was only testing my logon.
When I tried a different machine, your modifications worked perfect.  It no longer bombs, and it gives the correct error message.
So you have accomplished the task and quickly I might add.  

Now what I need to know is what did I do wrong so that the script does not find any remote person logged onto a computer.  Did I miss code something, wrong expression, etc.

And you will be awarded all the points since you got it work perfectly based upon the orignal question.
If you need, I can award you the poinst now on this question and open a second one up if you prefer.

Hope that helps,
Dale
I just tried this against a remote server where I 'know' my counterpart is logged onto the console...the .Username property returned "null", so I'm not sure this method works?  Where'd you get this?

How about good ol' pstools?

psloggedon /l \\Servername
will tell you who's logged in at the console.... :^)
Avatar of dgore1

ASKER

Actually got this from Microsoft scripting site...

Already did the pstools...looking for some way to automate the process if it's possible!!

Know of any other code?
automate 'through' vbs?  Cause this would be a batch version:

@echo off
set /p PC=Enter the computer name
psloggedon /l \\%pc%
::end

Can you post the link to the MS script?
Avatar of dgore1

ASKER

Boy...that was awhile back...but I'll try and look it up...

hmmm...cool idea!!  I'll give that a try as well...
Good luck - and thanx! :^)
Avatar of dgore1

ASKER

Thanks for all the help!!
Much obliged! :^)
I ran into the same issue in a script I wrote.  This works fine for me.  PINGhost is another function I have that pings the host computer first to see if it's available on the network.  If it's not available, then no point in attempting the WMI connection.  Other than that the only difference I see between my version and sirbounty's version is the added IsNull test.

Private Function GetCurrentUser(strHostname)
    Dim objWMIService, _
        colComputer, _
        objComputer
    If PINGhost(strHostname) = "Yes" Then
        Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strHostname & "\root\cimv2")
        Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
        For Each objComputer In colComputer
            GetCurrentUser = IIf(IsNull(objComputer.UserName), "Not Logged In", objComputer.UserName)
        Next
    End If
    Set objComputer = Nothing
    Set colComputer = Nothing
    Set objWMIService = Nothing
End Function
Hey BDF!  I tried it in my VB app and stepped through it...I know psloggedon will return bogus entries sometimes (under the LSA), along with the actual logged on user, but using this code, and trying your mod (to test for null), still failed for me...
IsNull still returned 'false', when the .Username was still reporting as Null...weird.
Not only that but the 'next' part of the loop failed to find anything further...
Not sure why it isn't working...
Evening, SB.  

I can't explain why the returned value would be null yet not be detected properly by IsNull().  I haven't seen that.  The function I posted is part of an application I wrote for managing VNC and Remote Desktop connections via Active Directory.  I've been using it for a couple of years and don't remember seeing anything like this cropping up.  I'm not clear on what you mean about the Next part of the loop failing.  There'd only be one host computer so the For ... Next loop should only process once.
I guess because I was testing a terminal server, I exptected more than one 'local' connection, but probably the only 'local' is the console session...
Makes sense.  That sounds like a reasonable explanation.  I don't use TS much, so I've little experience with it.
Hi guys,

I have a similar VBScript that I use to determine who is logged onto a remote machine.  I use a Ping(strComputer) function that uses wshShell.Run to execute a ping command.  The ping command pipes to a text file, which is then opened and tested for a "reply from" string.  I did originally use wshShell.Execute, but could not hide the command prompt, so opted for the text file version.

Anyway, from my testing, which was a while ago now, I ran into similar problems where the username returned was Null.  This only occurred on Windows 2000 clients.  I read somewhere that Windows XP allows all users to access some parts of Win32_OperatingSystem, whereas, on Windows 2000 machines, the logged on user must be a local Administrator.  You can test this, but from memory that's the way it worked.  So the only way to read the user name on a Windows 2000 machine would be to use alternate credentials, probably only via psexec and a remote copy of the script first.....untidy!

Acutally sometimes an "nbtstat -a computername" will work, but you have to massage the output to get the right line.

Hope this helps.

Regards,

Rob.
Also, the reason I came across this post was because of the 800A01CE error that I am getting, despite the fact the machine can be pinged.  I am about to implement the On Error Resume Next, and then output problematic PCs to a text file for later investigation.

The weird thing is that I can remotely manage the PC (view the Event Log, stop and restart Services) but cannot connect to a WMI Namespace.  I'm still looking.

Regards,

Rob.
I've done some testing with a bad computer name that I outputted when I received the GetObject failure.  In the case of two of my machines that I know about so far, there seems to be an old DNS lease in the DNS records.  For example, ComputerA (problematic) appears to respond to ping at say 192.0.1.3 BUT when I do nbtstat -a on 192.0.1.3 it returns ComputerB as the object at that IP Address.

The reason this DNS mix up seems to exist is because ComputerA has not been turned on for a couple of weeks, therefore the lease expires, and the same IP is renewed to a different machine by DHCP, but the ComputerA DNS entry still exists.

I must have DNS configured incorrectly.  If this problem sounds familiar to anyone, do they know of a way for me to clean up my DNS?

Regards,

Rob.
Avatar of dgore1

ASKER

1 last item:  you asked for the location of the original script, so here it is:  1 caveat...read the description which I failed to do...since our network is a mixture of NT servers, XP, and 2003.

The link is at:  http://www.microsoft.com/technet/scriptcenter/scripts/desktop/default.mspx?mfr=true
List the User Logged on to a Remote Computer

Description

Returns the user name of the user currently logged on to a remote computer. To use this script, replace atl-ws-01 with the name of the remote computer you want to check. Although this script will run on Windows NT 4.0, Windows 98, and Windows 2000, it will not always return information.

Supported Platforms

Windows Server 2003
 Yes
 
Windows XP
 Yes
 
Windows 2000
 Yes
 
Windows NT 4.0
 Yes
 
Windows 98
 Yes
 

Script Code

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

Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 
For Each objComputer in colComputer
    Wscript.Echo "Logged-on user: " & objComputer.UserName
Next