Solved

Environment Variable: Lastname-First Initial

Posted on 2011-02-10
6
1,115 Views
Last Modified: 2012-05-11
How can I create an environment variable (per user) for lastname-first?

I want to be able to type %lastname-firsti% and see Moody-J
0
Comment
Question by:Joseph Moody
6 Comments
 

Expert Comment

by:SimonJarvis
ID: 34867137
If you have a firstname and lastname environment variable then you can simply echo

%lastname%-%firstname:~0,1% = Moody-J

%lastname% = Moody
%firstname% = Jane

If the user firstname and lastname are not in your environments then you can create a simple vbscript to echo out the Jastname-First Initial like below.

' ------------------ Start Script -----------

' connect to the User object in AD of this (current) user
Set objSysInfo = CreateObject("ADSystemInfo")
userDN = objSysInfo.username
Set objUser = GetObject("LDAP://" & userDN)

' cache all the used attributes of the user (optional, for better performance on multiple attr calls)
objUser.GetInfoEx Array("GivenName","SN"), 0

fn = objUser.Get("GivenName")
sn = objUser.Get("SN")

WScript.Echo sn & "-" & Left(fn, 1)

' ------------------ End Script -----------

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34867223
Hi, see if this works.

Regards,

Rob.
Set wshShell = CreateObject( "WScript.Shell" )
Set wshSystemEnv = wshShell.Environment("USER")
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
strLastName = objUser.sn
strInitial = Left(objUser.GivenName, 1)
' Set the environment variable
wshSystemEnv("lastname-firsti") = strLastName & "-" & strInitial

Open in new window

0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 34867790
Since you might want these for use in a batch file (it seems), you might want to look at the UserName free utility at:

http://www.joeware.net/freetools/index.htm

It even has the option to write a CMD file of SET commands for various flavors of the user info.

~bp
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 22

Author Comment

by:Joseph Moody
ID: 34888635
Thank you for your suggestions.

RobSampson: your script works great! I have it currently running as a logon script. Is there a way to make it work on the first logon? Right now, I have to log off and log back on.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34892701
Hi, this has worked for me:
 
Set objShell = CreateObject("WScript.Shell")
Set objUserEnv = objShell.Environment("USER")
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
strLastName = objUser.sn
strInitial = Left(objUser.GivenName, 1)
' Set the environment variable
objUserEnv("lastname-firsti") = strLastName & "-" & strInitial
WScript.Echo objUserEnv("lastname-firsti")

Open in new window


So I'm not sure where the value is not accessible for you.

I have read though, that sometimes it won't exist until the script has complete, so here's a trick that will get value to register by creating and running another script to do it.

Regards,

Rob.
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strTempScript = objShell.ExpandEnvironmentStrings("%TEMP%") & "\" & objFSO.GetTempName & ".vbs"

strEnvVar = "lastname-firsti"

' Create the second temporary script to set the environment variable
Set objTempScript = objFSO.CreateTextFile(strTempScript, True)
objTempScript.WriteLine "Set objShell = CreateObject(""WScript.Shell"")"
objTempScript.WriteLine "Set objUserEnv = objShell.Environment(""USER"")"
objTempScript.WriteLine "Set objADSysInfo = CreateObject(""ADSystemInfo"")"
objTempScript.WriteLine "Set objUser = GetObject(""LDAP://"" & objADSysInfo.UserName)"
objTempScript.WriteLine "strLastName = objUser.sn"
objTempScript.WriteLine "strInitial = Left(objUser.GivenName, 1)"
objTempScript.WriteLine "' Set the environment variable"
objTempScript.WriteLine "objUserEnv(""" & strEnvVar & """) = strLastName & ""-"" & strInitial"
objTempScript.Close

' Run the temporary script, then delete it
objShell.Run "wscript.exe """ & strTempScript & """", 1, True

' Now continue with the rest of the script that uses the environment variable
Set objUserEnv = objShell.Environment("USER")
WScript.Echo objUserEnv(strEnvVar)

Open in new window

0
 
LVL 22

Author Closing Comment

by:Joseph Moody
ID: 34892845
Thanks so much! That worked out!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question