Click here to Skip to main content
15,867,851 members
Articles / Programming Languages / VBScript

Getting User Information Using WSH and VBScript

Rate me:
Please Sign up or sign in to vote.
4.80/5 (16 votes)
3 Oct 2001CPOL3 min read 492.5K   3.3K   35   30
An article on getting User Information with VBScript using WSH

Image 1

Introduction

I spent almost three to four months looking for a way to get the User Name and Computer Name of the User who is visiting my site. I tried to get it from ASP using Server Variables but that was empty almost every time. Some days before, I visited Microsoft site http://www.microsoft.com/scripting/ and found some material on the Windows Scripting Host. This solved my problem on getting User Name and Computer Name from my Script.

Here is a simple example to get this information. Try it out and then see the detail description below

VBScript
Dim objNet
On Error Resume Next 

'In case we fail to create object then display our custom error

Set objNet = CreateObject("WScript.NetWork") 
If  Err.Number <> 0 Then                'If error occured then display notice
	MsgBox "Don't be Shy." & vbCRLF &_
               "Do not press ""No"" If your browser warns you."
	Document.Location = "UserInfo.html"	  
                                        'Place the Name of the document. 
	                                'It will display again
End if
	
Dim strInfo
strInfo = "User Name is     " & objNet.UserName & vbCRLF & _
          "Computer Name is " & objNet.ComputerName & vbCRLF & _
          "Domain Name is   " & objNet.UserDomain
MsgBox strInfo
	
Set objNet = Nothing                    'Destroy the Object to free the Memory

That's All the code. Now see the description.

First of all, we created an object of type WScript.NetWork under the VBScript tag. Network type object contains all the information about the network you may have. You can get a lot of information by using this object. All the properties and the Methods are listed below. For now, I was in need to get the information about the User Name, User Domain and the Computer Name of the User who is visiting my site.

Before creating the object, I used the On Error Resume Next statement. The goal in using this statement was to prevent any run time error. When you run this example, you will encounter a message from Internet Explorer like this

Image 2

You have to choose Yes else the statement If Err.Number <> 0 will be true and a message will be displayed and the browser will be redirected to the same page. If I did not used this statement I encountered errors with the script runtime.

If you press the Yes button, the Object will be created and now we are ready to receive the values. We get the values from the properties of the objNet. The Property UserName gives the Name of the Logged on User, same as the LOGON_USER Server variable in ASP. The ComputerName property provides the Name of the Computer and UserDomain provides the Name of the Domain to which the user belongs. Finally, we set the objNet to nothing in order to free the memory.

So, that was all to get the User Info. Now let's see the complete description of the Network object of WScript.

Here are the properties of the Network Object of WScrip.

Computer NameA string representation of the computer name.
UserDomainA string representation of the user's domain.
UserNameA string representation of the user name.

These are the methods of Network Object of WScript.

AddPrinterConnectionMaps a remote printer to a local resource name.
EnumNetworkDrivesReturns the current network drive mappings.
EnumPrinterConnectionsReturns the current network drive mappings.
MapNetworkDriveMaps a share point to a local resource name.
RemoveNetworkDriveRemoves the current resource connection.
RemovePrinterConnectionRemoves a current resource connection.
SetDefaultPrinterSets the default printer.

You can find more documentations on WSC from the Microsoft site, http://www.microsoft.com/scripting/. I have included a .vbs file too here which do not need any browser. Just double click on it and get the same information.

Be the AngrycodeR on http://www.theangrycoder.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGetting windows logged in username in server Pin
sari_veena2-Jul-12 21:02
sari_veena2-Jul-12 21:02 
GeneralRe: Getting windows logged in username in server Pin
Sameers Javed2-Jul-12 21:21
Sameers Javed2-Jul-12 21:21 
GeneralRe: Getting windows logged in username in server Pin
sari_veena3-Jul-12 0:07
sari_veena3-Jul-12 0:07 
GeneralPutting the Signon information where I want it. Pin
KentWisco9-Oct-07 6:38
KentWisco9-Oct-07 6:38 
GeneralASP .NET problem Pin
Rajiv Sharma9-Mar-07 20:36
Rajiv Sharma9-Mar-07 20:36 
GeneralRe: ASP .NET problem Pin
Sameers Javed10-Mar-07 6:11
Sameers Javed10-Mar-07 6:11 
Generalmicrosoft vbscript runtime error:activex component can't create:'getobject' Pin
anbumanikandan5-Nov-06 6:02
anbumanikandan5-Nov-06 6:02 
GeneralRe: microsoft vbscript runtime error:activex component can't create:'getobject' Pin
eashwar v13-Nov-09 0:02
eashwar v13-Nov-09 0:02 
GeneralGet window username? [modified] Pin
-C-?-M-22-Aug-06 22:04
-C-?-M-22-Aug-06 22:04 
GeneralError After Hosting Pin
Syri19-Jul-06 1:29
Syri19-Jul-06 1:29 
Generaluse windows password in asp Pin
nikoo_7029-Nov-05 12:54
nikoo_7029-Nov-05 12:54 
GeneralRe: use windows password in asp Pin
Guy McMickle12-Dec-05 10:02
Guy McMickle12-Dec-05 10:02 
GeneralRe: use windows password in asp Pin
nikoo_7012-Dec-05 10:54
nikoo_7012-Dec-05 10:54 
thanks for your help..Smile | :)

nikoo
GeneralRubbish Pin
PhatSeeJay22-Jul-04 5:57
PhatSeeJay22-Jul-04 5:57 
GeneralRe: Rubbish Pin
Anonymous7-Sep-05 20:42
Anonymous7-Sep-05 20:42 
GeneralRe: Rubbish Pin
webfort200021-May-08 0:08
webfort200021-May-08 0:08 
Generaldisplaying user info e.g username on page Pin
JamesRed5-Jun-03 1:51
JamesRed5-Jun-03 1:51 
GeneralRe: displaying user info e.g username on page Pin
Daniel George18-Jan-05 20:04
Daniel George18-Jan-05 20:04 
GeneralProblem with apache Pin
Anonymous16-Feb-03 2:30
Anonymous16-Feb-03 2:30 
GeneralRe: Problem with apache Pin
DFU2320-May-03 2:48
DFU2320-May-03 2:48 
Generaltret Pin
Anonymous28-Jan-03 22:33
Anonymous28-Jan-03 22:33 
GeneralThis works Pin
ask219004-Dec-02 21:06
ask219004-Dec-02 21:06 
GeneralRe: This works Pin
Stvsan759-Jan-03 19:54
sussStvsan759-Jan-03 19:54 
GeneralRe: This works Pin
AirNog13-May-04 17:30
AirNog13-May-04 17:30 
GeneralThis is cool, STV [modified] Pin
Terry_Montreal1-Mar-07 5:09
Terry_Montreal1-Mar-07 5:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.