Solved

Determining who is logged onto a remote workstation using vbs script

Posted on 2007-04-03
22
5,354 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:dgore1
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Good luck - and thanx! :^)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:dgore1
Comment Utility
Thanks for all the help!!
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Much obliged! :^)
0
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now