5,692,513 members and growing! (16,929 online)
Email Password   helpLost your password?
Languages » VBScript » General     Intermediate

Getting User Information Using WSH and VBScript

By Sameers (theAngrycodeR)

An article on getting User Information with VBScript using WSH
VBScript, Windows, Visual Studio, ASP, Dev

Posted: 3 Oct 2001
Updated: 3 Oct 2001
Views: 264,135
Bookmarked: 24 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 5.23 Rating: 4.09 out of 5
3 votes, 20.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
2 votes, 13.3%
4
10 votes, 66.7%
5

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

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

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 Name A string representation of the computer name.
UserDomain A string representation of the user's domain.
UserName A string representation of the user name.

These are the methods of Network Object of WScript.

AddPrinterConnection Maps a remote printer to a local resource name.
EnumNetworkDrives Returns the current network drive mappings.
EnumPrinterConnections Returns the current network drive mappings.
MapNetworkDrive Maps a share point to a local resource name.
RemoveNetworkDrive Removes the current resource connection.
RemovePrinterConnection Removes a current resource connection.
SetDefaultPrinter Sets 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sameers (theAngrycodeR)



Occupation: Web Developer
Location: United States United States

Other popular VBScript articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 26 (Total in Forum: 26) (Refresh)FirstPrevNext
GeneralPutting the Signon information where I want it.memberKentWisco7:38 9 Oct '07  
GeneralASP .NET problemmemberAlohaMora21:36 9 Mar '07  
GeneralRe: ASP .NET problemmemberSameers (theAngrycodeR )7:11 10 Mar '07  
Generalmicrosoft vbscript runtime error:activex component can't create:'getobject'memberanbumanikandan7:02 5 Nov '06  
GeneralGet window username? [modified]member-C-?-M-23:04 22 Aug '06  
GeneralError After HostingmemberSyri2:29 19 Jul '06  
Generaluse windows password in aspmembernikoo_7013:54 29 Nov '05  
GeneralRe: use windows password in aspmemberGuy McMickle11:02 12 Dec '05  
GeneralRe: use windows password in aspmembernikoo_7011:54 12 Dec '05  
GeneralRubbishmemberPhatSeeJay6:57 22 Jul '04  
GeneralRe: RubbishsussAnonymous21:42 7 Sep '05  
GeneralRe: Rubbishmemberwebfort20001:08 21 May '08  
Generaldisplaying user info e.g username on pagesussjamesred2:51 5 Jun '03  
GeneralRe: displaying user info e.g username on pagesussdaniel george21:04 18 Jan '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Oct 2001
Editor: Chris Maunder
Copyright 2001 by Sameers (theAngrycodeR)
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project