Click here to Skip to main content
Click here to Skip to main content

Getting User Information Using WSH and VBScript

By , 3 Oct 2001
 

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
No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGetting windows logged in username in servermembersari_veena2 Jul '12 - 21:02 
Hi,
 
I used the code mentioned this article to get the windows logged in user name. Which is working fine for localhost. But not in Server.
 
Could you please help me to get the logged in user name in server. Possible suggest me the solution in vbScript
 
Thanks & Regards,
Sari
GeneralRe: Getting windows logged in username in servermemberSameers.ME2 Jul '12 - 21:21 
You can't run such script from the server. That is a security risk.
You might be able to do that if you can get a certificate to sign your code, without that, it will never work.
FREE MSN Auto Responder[^]
 
History Remember Vendors, NOT Developers

GeneralRe: Getting windows logged in username in servermembersari_veena3 Jul '12 - 0:07 
Hi,

Thank you Sameers for the reply . Is it possible to get windows username from CLIENT side using vb script?

Thanks & Regards,
Sari
GeneralPutting the Signon information where I want it.memberKentWisco9 Oct '07 - 6:38 
I am trying to get the User Signon to propogate into textbox but am not having any luck. I am very new to VBScript and have had no formal training. Please be very detailed.
 
Thanks for your time and assistance.
 
Kent Wisco

GeneralASP .NET problemmemberAlohaMora9 Mar '07 - 20:36 
I am looking for something in asp .net.
 
I want to get the client computer username, who visits my webiste.
 
Plz Help if anybody hv the solution for same. I am running my application of IIS ofcourse.
 

 
Aloha
GeneralRe: ASP .NET problemmemberSameers (theAngrycodeR )10 Mar '07 - 6:11 
Well, it is possible actually using the script in this article. BUT, it will actually NOT run in the user's browser until you digitally sign that using some third party security certificate like www.verisign.com. Fore more information, See Verisign website.[^]
 
The idea behind is that scripting could be really harmful for the user's system. For example, if I could create FSO object through browser, I can delete whole disk of the user's system. Similarly, using other objects, I can get private information from user's system (stored passwords etc). That is why, it is not allowed to create such objects from the browser. If you really need any, you will have ensure the user that script is coming from your website. If you sign your code using above mentioned product, browser will ask user that XXX script is trying to execute and it is from XXXX vendor. Do you want to Execute? and if user allow, it will execute, otherwise NOT.
 
Hope it makes sense.
 
Sameers
 
Need custom software? Contact DevelopersINN[^]
Need to add reminders for your Outlook emails? Try Outlook Personal Assistant[^]

Generalmicrosoft vbscript runtime error:activex component can't create:'getobject'memberanbumanikandan5 Nov '06 - 6:02 
hai everybody
when i am running this code.i receive this error message "microsoft vbscript runtime error:activex component can't create:'getobject'".please help to rectify this.
 
<html>
<head>
<title>vbs testing</title>
<script language=vbs>
sub display()
Dim strComputer,fso,objTxtFile,objWMIService

strComputer = document.myform.mytext.value
msgbox strComputer
 
'Set WshShell = WScript.CreateObject("WScript.Shell")
 

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

set fso=createobject("Scripting.FileSystemObject")
set objTextFile=fs.CreateFileText("C:\sam301006.txt",true)
 
Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service")
 
For Each objService in colRunningServices
Wscript.Echo objService.DisplayName & VbTab & objService.State
objTextFile.writeline(objService.DisplayName & VbTab & objService.State)
Next
end sub
</script>
</head>
<body>
<form name=myform >
<input type=text name=mytext>
<input type=submit name=mysubmit önclick=display>
</form>
</body>
</html>
GeneralRe: microsoft vbscript runtime error:activex component can't create:'getobject'membereashwar v13 Nov '09 - 0:02 
Hi,
Under trusted sites / security click Custom Level and enable Initalizing and Scripting ActiveX controls that are not marked safe. That should enable createobject on client.
 
By
eashwar v
GeneralGet window username? [modified]member-C-?-M-22 Aug '06 - 22:04 
I'm using the code below:
 
----------------------------------------------------------------------------
Dim objNet
On Error Resume Next
Set objNet = CreateObject("WScript.NetWork")
If Err.Number <> 0 Then MsgBox "Don't be Shy." & vbCRLF & "Do not press ""No"" If your browser warns you."
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
----------------------------------------------------------------------------
Its worked fine with local machine but when i upload to server: Apache and browse in remote computer it doesn't work fine and return error 13.
 
Any clue for it?

GeneralError After HostingmemberSyri19 Jul '06 - 1:29 
When i browsed the page containig your code through IIS i got the error message:
 
MsgBox "Don't be Shy." & vbCRLF &_
"Do not press ""No"" If your browser warns you."
 
but, when i browsed it locally (without IIS) it worked.
 
Why??

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

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