Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1198
  • Last Modified:

Environment Variable: Lastname-First Initial

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
Joseph Moody
Asked:
Joseph Moody
1 Solution
 
SimonJarvisCommented:
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
 
RobSampsonCommented:
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
 
Bill PrewCommented:
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
 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

 
Joseph MoodyBlogger and wearer of all hats.Author Commented:
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
 
RobSampsonCommented:
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
 
Joseph MoodyBlogger and wearer of all hats.Author Commented:
Thanks so much! That worked out!
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now