Re: Get logged on Username from Active Directory

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Randy wrote:

I am writing a login script for a domain where I need to pick up the
current logged in user on an XP box and pass it off to function in the
form:

"CN='Test User',OU=xxx,DC=temp,DC=com"

The code I am using to get the currently logged in user is:
******************
Option Explicit

Dim objNetworkCon, strUserName

Set objNetworkCon = WScript.CreateObject("WScript.Network")

strUserName = objNetworkCon.UserName
WScript.Echo "Username is - " & strUserName
********************
If username was 'test' and filed in Active Directory as "Test User", I
want to get back something that tell me it is "Test User".

Hi,

You are retrieving the NT name (NetBIOS name) of the user from the
wshNetwork object and you want the Distinguished Name (DN) so you can bind
to the user object with the LDAP provider. If all clients are w2k or above,
you can use the ADSystemInfo object to retrieve the DN directly:
======
Set objSysInfo = CreateObject("ADSystemInfo")
strUserDN = objSysInfo.UserName
Set objUser = GetObject("LDAP://"; & strUserDN)
======
If you have older clients, you can use the NameTranslate object to convert
the NT name of the user to the DN. NameTranslate is available if DSClient is
installed (Win9x or NT), or the client is w2k or above. For more, see this
link:

http://www.rlmueller.net/NameTranslateFAQ.htm

An example VBScript logon script that uses ADSystemInfo:

http://www.rlmueller.net/Logon3.htm

An example VBScript logon script that uses the NameTranslate object:

http://www.rlmueller.net/Logon2.htm

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net


.