Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grab Users First Name for Login Script

Status
Not open for further replies.

jwithi

Technical User
Jan 13, 2008
32
GB
Good Morning All,

I currently have a VBScript that shows a welcome message that grabs the user username. I would like to know if it is possible to grab the users first name instead?
 
If you are talking about Active Directory users, check out the FAQ section

faq329-5688 will get you started...


Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
How are you grabbing the user's username?

WSHNetwork? If so, you should use ADSystemInfo, bind to the users AD object, and get the "givenName" property.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Hi, Below is the top of my vbscript. i have googled using the 'givenName' variable but could not find anything.

I would like to use the same kind of method already used in the script that grabs the username for group drive mappings etc.

ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path, HourNow, Greeting

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")

'==============================================================================================
'AUTOMATICALLY GRAB THE USERS DOMAIN NAME
'----------------------------------------------------------------------------------------------
DomainString = Wshnetwork.UserDomain

'==============================================================================================
'FIND WINDOWS DIRECTORY
'----------------------------------------------------------------------------------------------
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")

'==============================================================================================
'GRABS THE USERNAME
'----------------------------------------------------------------------------------------------
UserString = WSHNetwork.UserName

'==============================================================================================
'BIND TO USER OBJECT TO GET USERNAME AND CHECK FOR GROUP MEMBERSHIP LATER
'----------------------------------------------------------------------------------------------
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

'==============================================================================================
'GRABS COMPUTER NAME FOR USE IN ADD-ON CODE LATER
'----------------------------------------------------------------------------------------------
strComputer = WSHNetwork.ComputerName

'==============================================================================================
'CHECKS O\S, IF 2000/2003 SERVER IT EXITS THE SCRIPT WITHOUT MAPPING DRIVES
'----------------------------------------------------------------------------------------------
ServerCheck = False
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem In colItems
If InStr(1,objItem.Caption,"Server") Then wscript.Quit

Next
Set objWMIService = Nothing
Set colItems = Nothing
Set objItem = Nothing

'==============================================================================================
'WELCOME MESSAGE BOX
'----------------------------------------------------------------------------------------------
HourNow = Hour(Now)
If HourNow >5 And HourNow <12 Then
Greeting = "Morning"
Elseif HourNow =>12 And HourNow <16 Then
Greeting = "Afternoon"
Else
Greeting = "Evening"
End If

WshShell.Popup "Good " & Greeting & " " & UserString & "." ,3, "Welcome"

WshShell.Popup "You Are Now Being Logged Into The System, Please Be Patient...",4, "RDASH Login Script"
 
Hi jwithi,

If you already have that, it will be very easy for you.

You should be able to do it like this:

Code:
'BIND TO USER OBJECT TO GET USERNAME AND CHECK FOR GROUP MEMBERSHIP LATER
'----------------------------------------------------------------------------------------------
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
UserFullName = UserObj.name

And later in the script replace UserString with UserFullName


[Offtopic - naging mode]
But please let me also make a little remark.

I don't like the 'On error resume next' line at the top of a script. It is hard to troubleshoot if something doesn't work.
And to be sure little typo's don't get in your way, use the 'Option explicit' command at the top of your script, and declare all the varables you use in the script...
[/offtopic - /naging mode]



Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
Hi SaMaLaKo,

I have just tried what you suggested but it still returns the username in the welcome message, and the first name.

Just to make myself clearer on what i am looking for, i need to be able to grab the username, so that it can be used for mapping drives based on group membership. I would also like to grab the users first name from AD so that it can be used in the welcome message.
 
Ok,

Can you try this then?

Code:
UserFullName = UserObj.Get("givenName")

FYI:
Code:
strInitials = UserObj.Get("initials")
strSn = UserObj.Get("sn")
strDisplayName = UserObj.Get("displayName")

Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
This is what i have now:

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserFullName, UserObj, Path, HourNow, Greeting

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")

'==============================================================================================
'AUTOMATICALLY GRAB THE USERS DOMAIN NAME
'----------------------------------------------------------------------------------------------
DomainString = Wshnetwork.UserDomain

'==============================================================================================
'FIND WINDOWS DIRECTORY
'----------------------------------------------------------------------------------------------
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")

'==============================================================================================
'GRABS THE USERNAME
'----------------------------------------------------------------------------------------------
UserString = WSHNetwork.UserName

'==============================================================================================
'BIND TO USER OBJECT TO GET USERNAME AND CHECK FOR GROUP MEMBERSHIP LATER
'----------------------------------------------------------------------------------------------
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
UserFullName = UserObj.Get("givenName")

'==============================================================================================
'GRABS COMPUTER NAME FOR USE IN ADD-ON CODE LATER
'----------------------------------------------------------------------------------------------
strComputer = WSHNetwork.ComputerName

'==============================================================================================
'CHECKS O\S, IF 2000/2003 SERVER IT EXITS THE SCRIPT WITHOUT MAPPING DRIVES
'----------------------------------------------------------------------------------------------
ServerCheck = False
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem In colItems
If InStr(1,objItem.Caption,"Server") Then wscript.Quit

Next
Set objWMIService = Nothing
Set colItems = Nothing
Set objItem = Nothing

'==============================================================================================
'WELCOME MESSAGE BOX
'----------------------------------------------------------------------------------------------
HourNow = Hour(Now)
If HourNow >5 And HourNow <12 Then
Greeting = "Morning"
Elseif HourNow =>12 And HourNow <16 Then
Greeting = "Afternoon"
Else
Greeting = "Evening"
End If

WshShell.Popup "Good " & Greeting & " " & UserFullName & "." ,3, "Welcome"

WshShell.Popup "You Are Now Being Logged Into The System, Please Be Patient...",4, "RDASH Login Script"

The welcome message now does not show a username or first name, it is blank?
 
hope this helps

Code:
Dim WshShell, WshEnv, objFSO, WshNetwork
Const OverwriteExisting = True

'Configures Variables
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("Process")
Set WshNetwork = CreateObject("WScript.Network")

Set objSysInfo = CreateObject("ADSystemInfo")
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")

'Grab the user name
UserString = WshNetwork.UserName
Set objCurrentUser = GetObject("LDAP://" & objSysInfo.UserName)
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)

strFullName = objUser.FullName
strFirstName = objUser.FirstName


wscript.echo "Full Name " & strFullName 
wscript.echo "First Name " & strFirstName 
wscript.echo "strUser " & strUser
wscript.echo "Username " & UserString
 
Thanks GrimR,

I am not much of an expert when it comes to VBScript, this is the first one i have ever done.

The above code looks a bit confusing to me, is there just on line of code i can added that grabs the users first name from AD and declares it as a variable which i can then add into the welcome message?

 
The only part that you would really need for the first name would be something like this...

Set objSysInfo = CreateObject("ADSystemInfo")
Set objCurrentUser = GetObject("LDAP://" & objSysInfo.UserName)
WScript.Echo objCurrentUser.givenName

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
That worked a treat, dm4ever! thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top