Link to home
Start Free TrialLog in
Avatar of vsmusic2000
vsmusic2000

asked on

Find out computer name given the username

Hi Experts,

I have got this script from the internet. I want to find out which computer a user is logged on to given the domain username of the person.
This script uses the net send command along with the nbtstat -c command to get the computer name and IP.
Can you please verify that it does so. I am not very good with scripting.
I am getting a compile error when I try and run this.
Thank you experts.
@echo off
if "%OS%" == "" goto syntax
if %1z == z? set goto syntax
 
 
if %1z == z set /p UN=Enter UserName: &goto main
set UN=%1
:main
net send "%UN%" "Finding your computername. Press OK - do NOT respond." >nul
if errorlevel == 1 goto error
 
nbtstat -c > %TEMP%\LOCATE.LOG
findstr /i /c:%UN% %TEMP%\LOCATE.LOG >%TEMP%\LOCATE2.LOG
FOR /F "tokens=3,4,5,6,7 delims=. " %%i in (%TEMP%\LOCATE2.LOG) do ping -a -n 1 -l 1 %%j.%%k.%%l.%%m >%TEMP%\LOCATE.LOG
FOR /F "eol=: tokens=2" %%a in (%TEMP%\LOCATE.LOG) do @echo %UN% is logged into %%a&goto end
Echo This does not report whether %UN% is logged into multiple workstations!
del %TEMP%\LOCATE*.LOG
goto end
 
:error
cls
echo Error: %UN% username not found
goto end
 
:syntax
echo Syntax %0 username
echo Must be run from Windows NT or Windows 2000
echo NT/2000 users located, Win9x only if winpopup loaded.
goto end
 
:end
pause

Open in new window

Avatar of Psy053
Psy053
Flag of Australia image

Whilst it's a clever way of obtaining the information, I wouldn't recommend using it in a corporate environment.
Having a message pop up on your screen, stealing focus, is a surefire way to annoy your user base.

I will see if I can find a better way.

Avatar of vsmusic2000
vsmusic2000

ASKER

Would then anyone please share a solution where I can find the computer name or IP address of the computer where a particular user is logged in.
I'm away from a real computer at the moment, but will rewrite the script when I can.
OK. Apologies for the delay. I  have re-written the script and it has been working well so far.

Rather then send out a Net Send message, the script queries Active Directory for all Computer Objects that do not have a Server Operating System Installed, the script then uses WMI to check what user is logged onto the machine, and then reports back if the specified user is found.

This script will also report if the user is logged onto multiple workstations.  

I'm just in the process of making a few changes so that it can handle a real-world environment, but I will have something for you soon.
WOw Psy 053, I am really excited to see what you have to offer. Thanks for taking the efforts.
Alright - I think we are ready to go.

I have included 2 different versions of the script, and put each in their own ZIP File.
(The ZIP Files will need to be extracted to separate folders as both ZIP Files contain a FindUser.cmd)

FindUser_AD.zip:
(Once you have extracted the files, you will need to rename FindUser.txt to FindUser.cmd, and FindUser_AD.txt to FindUser_AD.vbs)

This script is designed to search Active Directory for Computer Objects that do not have a Server Operating System installed. The script will then check each of the found computers for the specified username.

Before you can run this script, you will need to open FindUser_AD.vbs in notepad and change YourDomain.Local to your domain name

To run this Script, open a command prompt and run: FindUser.cmd DOMAINNAME\Username


FindUser_FromList:
(Once you have extracted the files, you will need to rename FindUser.txt to FindUser.cmd, and FindUser_FromList.txt to FindUser_FromList.vbs)

This script is designed to only check computers specified in the ComputerList.txt file for the specified user name. I have included this just in case you have concerns about running a script that queries Active Directory.

To run this Script, open a command prompt and run: FindUser.cmd DOMAINNAME\Username


Let me know how it goes for you.
FindUser-AD.zip
FindUser-FromList.zip
hi,
I have changed the domain name to my own (removing the .local also) which was there only once in the script and I ran the script but I got this message.
"FindUser_AD.vbs(14, 1) Provider: Table does not exist."
Please help.
I will have a look when I get back to my computer.

Quick question, is the domain name you used the fqdn or the NetBIOS name?
I used the FQDN (eg. abc.com)
Should I use the NetBIOS name?
Your internal domain name is deffinately .com and not .local?
I just did some quick testing and have only been able to replicate the issue by incorrectly entering the Internal Domain Name.
It sort of worked when I put the NetBIOS name but gave me this
"XXXXXX  can not be found or is unavailable" for every PC that was not found on the network or something.
Please advise. I am eager coz this is something new for me.
The error message is by design, and is an indication that the script is running as planned. It can be turned off if you would prefer by removing the following 3 lines from FindUser_AD.vbs

If Err.Number<> 0 Then
Wscript.echo objRecordSet.Fields("Name").Value, "can not be found or is unavailable"
End If
I removed that but now its just mentions "Searching" is there a way to make it faster. Now the above mentioned errors have stopped but I cant find the users quickly.
I have redone the script so that it:
Creates a list of all Computers in Active Directory that do not have a Server Operating System installed;
Uses Alive.exe on the above list to create a list of available computers;
Checks the available computers for the specified user.

In testing it has been significantly faster than the previous method.

Once you have extracted the files from FindUser.zip, you will need to rename the following files:

From: (To:)
Alive.txt (Alive.exe)
ListADComputers.txt (ListADComputers.vbs)
SearchResults.txt (SearchResults.vbs)
FindUser.txt (FindUser.cmd)

As I have not been able to find an online location for Alive.exe so I have included it. Alive.exe is a freeware utility written by Steven Wettberg that can be used to ping a server and return an Errorlevel.
Alive.exe was previously found at http://wettberg.home.texas.net/freeware however the site no longer exists.

To run, open a command prompt and run: FindUser.cmd DOMAIN\Username
FindUser.zip
Psy053, thank you so much for this. I am not in office but I will give this a try the first thing in the morning.
in another 20 hours.
Thanks
Hi Psy053,
THe search now mentions 'The system cannot find the file Results.txt'. do I rename the searchresults file or change some code.
Please help.
The error "The system cannot find the file Results.txt" is indicative of the ListADComputers.vbs not returning any results.

Did you remeber to update ListADComputers.vbs with your internal domain name?


Oops, sorry, I just re-read the instructions I gave you, and see that I missed the part about updating ListADComputers.vbs. Sorry about that.
It completes searching active directory for computers
but it keeps mentioning "Searching for available Computers"
could this be due to the domain name.
Stupid question, how would I find if my internal domain name is different from my external one and how do I know what it would be??
When it is "Searching for available Computers" it is checking the list of AD Computers to see which are pingable.

It still takes time to process these computers, however, it is about one third of the time the previous method took.

I have also noticed that I have made a small typo in one of the scripts that may mean it returns some false positives during the searching phase.

You will need to open FindUser.cmd in Notepad and change the following line;
From:
alive.exe /repeat=1 /timeout=0 %1
To:
alive.exe /repeat=1 /timeout=1 %1
Done all but the screen still shows "Searching for available Computers" I am sure I am doing something wrong on the domain name side.
how would I find if my internal domain name is different from my external one and how do I know what it would be??
You should be able to do a NSLookup on your Domain Controller and it should return somethings like SVRDC.YourDomain.Local (where YourDomain.Local is the internal Domain Name)

I'll get you to modify the FindUser.cmd again so that we can troubleshoot what is happening.

Can you open it in notepad, and change the following lines
From:
IF EXIST Results.txt Del Results.txt
IF EXIST ADComputers.txt Del ADComputers.txt
To:
REM IF EXIST Results.txt Del Results.txt
REM IF EXIST ADComputers.txt Del ADComputers.txt

The first line occurs twice in the script, however, please only change the lines that appear next to each other .

Once you done that, run the script again, and please let me know if an ADComputers.txt and Results.txt are created and let me know if there is anything inside them.
THere is only a ADcomputers file that is created and not the results.txt
I seen this line too 'alive.exe /repeat=1 /timeout=1 %1 > nul'
Is it supposed to be 'nul' and not 'null'??
Is there a list of computers within ADComputers.txt?
Yes, there are a lot of computer names, in thousands....
But the next step keeps on searching and nothing happens after that ....
My apologies, I have been testing a number of different computers and different environments, however I have not been able to replicate the problem.

Can you please tell me if the script is returning you to a Prompt, or is it sitting there with a _?

If it is sitting there with a _ it is possible that it is still scanning, to check this you can modify the FindUser.cmd and remove the > nul from the following line:
alive.exe /repeat=1 /timeout=0 %1 > nul

What this will do is show the results of the alive check on screen.
Yes, its is scanning, after removing the line its giving an
eg. TOBY_PC can not be found or is unavailable

But its slow as in I feel it doing a live ping with 4 pings and each PC takes a long time. What timing do you get? Is it in quick in seconds. It finishes the initial computer scanning of AD for computers but scan for each PC takes quite some time. And please dont apologize, you are helping me so much,.
Hi,

Why can't you try for a particular OU first to check and run for the whole domain?

regards
Chandru
In the testing I have done it's been pretty fast with the exception of machines that can not be found in DNS i.e. Machines that no longer exist on the network or have not been turned on for a while.

Tomorrow morning I will see if I can find a way to get around it, possibly by doing an nslookup on the returned results, and then only doing the alive check on machines that return an address.
Sounds good. Worth the wait... excited if this works fast enough.
Thanks
Alright. The biggest issue we were having was that the Alive Check was taking a long time on computers that don't exist or haven't been turned on recently.

What I have done is made a small change to the ListADComputers.vbs so that it outputs a full computer name (i.e. Computer01.Domain.Local) to the ADComputers.txt which has significantly increased the speed of the Alive check.

Now given that you have a large amount of computers to check, I still suspect that it will take a few moments, however, it should be much, much quicker - and atleast you are not annoying possibly 1000s of users, and Server Administrators with a popup message.

To bring our scripts back in line with each other, I recommend that you delete your current ListADComputers.vbs, SearchResults.vbs, and FindUser.cmd file then re-download them from the previously posted FindUser.zip (See comment 25531677) and rename extracted files as previously noted.

Once you have done that:
Please open ListADComputers.vbs in notepad and change StrDomain to match your Internal Domain Name, and then change the following line;
From:
      Wscript.Echo objRecordSet.Fields("Name").Value
To:
      Wscript.Echo objRecordSet.Fields("Name").Value & "." & strDomain


Please open FindUsers.cmd in notepad and change the following section;

From:
      :AliveCheck
      alive.exe /repeat=1 /timeout=0 %1 > nul
      IF %ERRORLEVEL% == 0 echo %1 >> Results.txt
      GoTo END
      
To:
      :AliveCheck
      alive.exe /repeat=1 /timeout=1 %1 > nul
      IF %ERRORLEVEL% == 0 echo %1 >> Results.txt
      IF %ERRORLEVEL%      GEQ 1 echo %1 >> Unavailable.txt
      GoTo END

You will notice that the script now also creates Unavailable.txt, which is a list of computers that Alive did not get a response from. This list may be valuable in determining machines that need to be cleaned from your environment - which in turn will make the FindUser script run faster in the future.
How have you gone with the script?
Hi Psy053,
I am so sorry for being out of touch. I have tried the scripts and the scripts runs and creates the 2 files, unavailable and adcomputers which are both a huge list. But the result does not appear though it keeps on filling the two files with more data.
Please help.
How many computers in your environment?
I am not even sure, coz my site is just a small branch spanning various countries.
there may be around 10-15K PC's in the whole AD structure. Can something be done to access only a certain 'site' in the AD?
No wonder it was taking a long time.
I'll set about re-writing it tomorrow for you.
Apologies for the delay, I will write this up today.
ASKER CERTIFIED SOLUTION
Avatar of Psy053
Psy053
Flag of Australia 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
How have you gotten on with this?
I would recommend awarding all points to comment 25716515, as the attached solution meets all requirements of the poster, and is a much better solution than the one they originally intended using.

As a side note, I am linking to this question in another question (q24983554) so please take that into consideration before deleting this question.
Hi sorry my account was locked out and I could not login to post anything.
Ok you gave a very good suggestion but unfortunately I do not think all our computer objects are in my branch OU. I think they may be placed in the COMPUTERS folder by default.
Can I still use this scripts and get the results?
You can still use the script to get the results, you will just need to change the strOUPath in ListADComputers.vbs to the root level. For example: strOUPath = "'LDAP://DC=YourDomain,DC=Local'"

As with before though, this is going to take a long time for 10K - 15K Computer objects. Honestly, your best bet for now and in the long run would be to clean up your AD environment, and move all of your Computer objects into their own OU's instead of just using the default Computers container.

How have you gone with this?
Might I suggest a different approach to this?  Take a look in my login script FAQ; there you will find some code to record login times and locations to a database.  You could simply look the information up in the database whenever needed.

http://www.tek-tips.com/faqs.cfm?fid=5798

Search for the text "Recording Login Times" and look at the second example.
If you're still looking into it, make this a logon script and it will record the username and computer name whenever the user logs on.
Const ForReading = 1
Const ForAppending = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("path\document.txt", ForReading)
objFile.Close

Set WshNtwk = WScript.CreateObject("WScript.Network")
PropertyINfo = "User Domain" & vbTab & "=  " & WshNtwk.UserDomain & vbCrLf & _
"Computer Name" & vbTab & "=  " & WshNtwk.ComputerName  & vbCrLf & "User Name" _
  & vbTab & "=  " & WshNtwk.UserName   & vbCrLf

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set OFile = oFSO.OpenTextFile("path\document.txt", ForAppending)
oFile.WriteLine PropertyInfo
OFile.Close

Open in new window

Do you require any further assistance with this question?
Avatar of Guy Hengel [angelIII / a3]
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.