Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Determining who is logged onto a remote workstation using vbs script

Posted on 2007-04-03
22
5,429 Views
Last Modified: 2010-10-05
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

0
Comment
Question by:dgore1
  • 9
  • 7
  • 3
  • +1
22 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18842978
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
0
 

Author Comment

by:dgore1
ID: 18844004
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
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18844081
I didn't change any of the normal processing...not sure why it all of a sudden doesn't work...?

Try this version...

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!"
On Error Resume Next 'continue when an error occurs
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
On Error GoTo 0
Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 For Each objComputer In colComputer
    Wscript.Echo "Logged-on user: " & objComputer.UserName
 Next
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:dgore1
ID: 18847454
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?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18848064
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?
0
 

Author Comment

by:dgore1
ID: 18854638
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
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18854688
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.... :^)
0
 

Author Comment

by:dgore1
ID: 18854724
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?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18854730
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?
0
 

Author Comment

by:dgore1
ID: 18854800
Boy...that was awhile back...but I'll try and look it up...

hmmm...cool idea!!  I'll give that a try as well...
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18854803
Good luck - and thanx! :^)
0
 

Author Comment

by:dgore1
ID: 18854822
Thanks for all the help!!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18854824
Much obliged! :^)
0
 
LVL 76

Expert Comment

by:David Lee
ID: 18854865
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
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18854889
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...
0
 
LVL 76

Expert Comment

by:David Lee
ID: 18854984
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.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18856488
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...
0
 
LVL 76

Expert Comment

by:David Lee
ID: 18856699
Makes sense.  That sounds like a reasonable explanation.  I don't use TS much, so I've little experience with it.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 18880700
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.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 18880714
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.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 18922116
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.
0
 

Author Comment

by:dgore1
ID: 18935240
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

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question