Solved

Environment Variable: Lastname-First Initial

Posted on 2011-02-10
6
1,132 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 54

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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