Solved

Environment Variable: Lastname-First Initial

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

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
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…
The viewer will learn how to count occurrences of each item in an array.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now