Windows 7 Thread, Environment variables GPP in Technical; Is it possible to create an environment variable using group policy preferences for either display name from active directly, or ...
8th October 2012, 11:10 AM #1
Environment variables GPP
Is it possible to create an environment variable using group policy preferences for either display name from active directly, or more ideally, first and last name from within active directory?
I'm after a way of pulling the first name and last name from active directory for use as a variable in a folder path, and I'd ideally like to be able to do all this within group policy rather than making custom VB scripts.
8th October 2012, 11:29 AM #2
Don't know if it will let you do this in the environment variables of GPP but this line will get you the display name:
for /f "tokens=2*" %%a in ('net user "%Username%" /domain ^| find /i "Full Name"') do set DisplayName=%%b
Thanks to themightymrp from:
mrbios (8th October 2012)
8th October 2012, 11:45 AM #3
What would i change on that line if i wanted %'SN'% and %'givenName'% from AD in the format of Surname, Lastname?
8th October 2012, 11:53 AM #4
Tbh I'm not sure, this line just searches the output of net user %username% /domain for the Full Name line. You can probably get creative and add an expression which splits the output using the space as the divider? Still not sure this would even work in a GPP - have you tried it?
8th October 2012, 12:04 PM #5
Yea it wouldn't work in the environment variables section, unless I'm doing something wrong of course.
Frustrating really as i wanted to try and keep everything out of a script but it looks like that might be my only option
8th October 2012, 12:32 PM #6
Ran that on my machine fine, and found i could do "echo %last%, %first%" and it'd give me exactly what i want, now i've tried adding that as a user logon script along with a folder creation GPP to create a folder where the path variable contains "%last%, %first%" but it just names the folder "%last%, %first%" and running the echo on the machine shows "echo , " which is strange.
Set objShell = CreateObject("WScript.Shell")
Set objUserEnv = objShell.Environment("USER")
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
' This will create the variable %first.last% for the first.lastname
objUserEnv("first.last") = objUser.sn & ", " & objUser.givenName
Frustrating as i feel like i'm so nearly there
EDITED for what i've changed the code to.
I get the feeling i need that script to run as an admin on logon
Last edited by mrbios; 8th October 2012 at 12:55 PM.
8th October 2012, 01:01 PM #7
I found this quite interesting so I looked into it and ended up writing this program. I've attached it here for you to use.
You can run it on logon script or from group policy to map Active Directory attributes to environment variables.
You must run it under the user's context as it uses the current context to get the attributes and uses user environment variables.
You can run it like this:
Will work with any AD attribute really, although some values are not human readable.
adtoenv.exe FORENAME=givenName SURNAME=sn
Edit: Requires .NET Framework 4 Client Profile, source available on request.
Edit 2: Bear in mind while testing, that command prompts don't pick up external changes to env variables until they are closed and reopened, got caught by this when debugging.
Last edited by ChrisMiles; 8th October 2012 at 01:13 PM.
Thanks to ChrisMiles from:
mrbios (8th October 2012)
8th October 2012, 01:21 PM #8
That's awesome, thanks Chris!
Sadly though i can't get it working Everything i do from vbs script to your program draws a blank. When doing an echo for the variables as the logged on user it gives me nothing, and when logging on it's just using the variable names to name the folder rather than the actual variable
8th October 2012, 01:29 PM #9
Calling the application from VBscript isn't going to work, as I edited above, command prompts and applications don't pick up changes to environment variables until they are restarted. Since you said you didn't want to use a script, i didn't envisage using the application in that way.
You need to run the program independently to set the variables.
Can you clarify what you are trying to do? When and how are you trying to use a folder path with environment variables?
8th October 2012, 01:32 PM #10
So how do you run this program independantly without sticking it into a logon script? Just tried running it from the Run command and managed to pull givenName into a %FORENAME% variable
8th October 2012, 01:35 PM #11
I've just made a .bat file to run your program whilst logged on as a typical locked down user and that works fine and the variables work, problem is it won't work for me on logon.
Originally Posted by ChrisMiles
The aim here is to have the variables created before the group policy folder creation GPP runs so that it creates a folder on the network for each user in a specific place called "SURNAME, FORENAME"
EDIT: oh the vbscript part i was on about was the script i was using prior to testing your program by the way
8th October 2012, 01:43 PM #12
Run it from a dedicated login script, but how you manage the execution order, I'm not too sure.
Originally Posted by themightymrp
8th October 2012, 01:46 PM #13
Thats not going to work, im pretty sure login scripts run after all the stuff. Let me think about it...
Originally Posted by mrbios
8th October 2012, 01:47 PM #14
I guess my only other option would be to have the bat file i run your ADtoENV program in also create the folders / call up another bat file to create the folders?
Originally Posted by ChrisMiles
8th October 2012, 02:08 PM #15
Ok Plan B....
This application will read attributes from the current user context...
Use it like this to create your folder...
for /f %a in ('GetADAttributes.exe givenName') do for /f %b in ('GetADAttributes.exe sn') do mkdir "C:\%b, %a"
By GiantEnemyAnt in forum Scripts
Last Post: 11th August 2009, 08:17 PM
By ful56_uk in forum Wireless Networks
Last Post: 27th July 2009, 10:23 PM
By Reaper in forum Scripts
Last Post: 19th February 2009, 04:45 PM
By PiqueABoo in forum Scripts
Last Post: 13th December 2006, 11:09 PM
By HodgeHi in forum Scripts
Last Post: 21st June 2006, 09:05 AM
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)