Click here to Skip to main content
Licence CPOL
First Posted 3 Oct 2001
Views 381,529
Downloads 2,767
Bookmarked 33 times

Getting User Information Using WSH and VBScript

By | 3 Oct 2001 | Article
An article on getting User Information with VBScript using WSH

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sameers.ME

Web Developer

Pakistan Pakistan

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPutting the Signon information where I want it. PinmemberKentWisco6:38 9 Oct '07  
GeneralASP .NET problem PinmemberAlohaMora20:36 9 Mar '07  
GeneralRe: ASP .NET problem PinmemberSameers (theAngrycodeR )6:11 10 Mar '07  
Generalmicrosoft vbscript runtime error:activex component can't create:'getobject' Pinmemberanbumanikandan6:02 5 Nov '06  
GeneralRe: microsoft vbscript runtime error:activex component can't create:'getobject' Pinmembereashwar v0:02 13 Nov '09  
GeneralGet window username? [modified] Pinmember-C-?-M-22:04 22 Aug '06  
GeneralError After Hosting PinmemberSyri1:29 19 Jul '06  
Generaluse windows password in asp Pinmembernikoo_7012:54 29 Nov '05  
GeneralRe: use windows password in asp PinmemberGuy McMickle10:02 12 Dec '05  
GeneralRe: use windows password in asp Pinmembernikoo_7010:54 12 Dec '05  
GeneralRubbish PinmemberPhatSeeJay5:57 22 Jul '04  
GeneralRe: Rubbish PinsussAnonymous20:42 7 Sep '05  
GeneralRe: Rubbish Pinmemberwebfort20000:08 21 May '08  
Generaldisplaying user info e.g username on page Pinsussjamesred1:51 5 Jun '03  
GeneralRe: displaying user info e.g username on page Pinsussdaniel george20:04 18 Jan '05  
hi...hope this helps u out
 
bye dan
 
<%@ Language=VBScript %>
<% Option Explicit %>
<HTML>
<HEAD>
<title>ICFAI Business School</title>
 
<script language="JScript">
function userinfo()
{
var usern
var WshNetwork=new ActiveXObject("WScript.Network")
usern=WshNetwork.UserName;
}
 
function reinit(obj)
{
t=obj.name
document.login.selsub.value=t;
 
if(t=="overall"){
var pg;
pg=prompt("Enter Password: ","");
document.login.psword.value=pg;
}
}
</script>
 
</HEAD>
 
<!--<BODY önload="userinfo()" bgcolor=#ffffcc topmargin="10" leftmargin="0" marginheight="0" marginwidth="0">     -->
<BODY önload="userinfo()" bgcolor=Bisque topmargin="10" leftmargin="0" marginheight="0" marginwidth="0">    
<IMG src="ibslogo.jpg" width="85" height="45">

ICFAI BUSINESS SCHOOL

Hyderabad







<form name=login method=post action="fac_details.asp?flag=showmenu">
<input type=hidden name=selsub>
<input type=hidden name=psword>
 


 

 

<%
dim objconn,flag
dim totsub, strsql
 
flag = Request("flag")
 
set objconn=Server.CreateObject("ADODB.Connection")
objconn.ConnectionString ="provider=Microsoft.jet.oledb.4.0;data source="&server.mappath("Term52005.mdb")
objconn.Open
strsql = "select distinct sname,subject from faculty where abb<> null order by sname"
set totsub=Server.CreateObject("ADODB.Recordset")
totsub.Open strsql, objconn
%>
 


<% Response.Write "
"
Response.Write ""
Response.Write ""%>
<%
dim i
i=0
do while not totsub.eof
Response.Write "
"
Response.Write ""
Response.Write ""
if not totsub.eof then
totsub.movenext
Response.Write ""
Response.Write ""
Response.Write ""
totsub.movenext
i=i+1
loop
totsub.Close
set totsub = Nothing
objconn.Close
set objconn = Nothing
 
%>
 
<!--

<marquee align="right" loop="infinite" bgcolor="blue" >


ICFAI Business
School - Hyderabad


</marquee>-->
</FORM>
 
</BODY>
</HTML>
"
Response.Write " ADMIN
" %>
<input type=submit value="View" name=<%="overall"%> önclick="reinit(this)">
<% Response.Write "
"
Response.Write totsub("subject")&"
" &totsub("sname") &""
%>
<input type=submit value="View" name=<%=totsub("sname")%> önclick="reinit(this)">
<%
Response.Write "
"
Response.Write totsub("subject")&"
" &totsub("sname") &""
%>
<input type=submit value="View" name=<%=totsub("sname")%> önclick="reinit(this)">
<%
end if
Response.Write "
GeneralProblem with apache PinsussAnonymous2:30 16 Feb '03  
GeneralRe: Problem with apache PinmemberD2F3U2:48 20 May '03  
Generaltret PinsussAnonymous22:33 28 Jan '03  
GeneralThis works Pinmemberask2190021:06 4 Dec '02  
GeneralRe: This works PinsussStvsan7519:54 9 Jan '03  
GeneralRe: This works Pinmembeream_user17:30 13 May '04  
GeneralThis is cool, STV [modified] PinmemberTerry_Montreal5:09 1 Mar '07  
GeneralError Pinmemberkandukondein10:28 2 Jul '02  
GeneralRe: Error PinmemberAnonymous17:00 3 Jul '02  
GeneralRe: Error Pinmemberparkette17:20 11 Jul '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 4 Oct 2001
Article Copyright 2001 by Sameers.ME
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid