Solved

Environment Variable: Lastname-First Initial

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A procedure for exporting installed hotfix details of remote computers using powershell
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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