+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
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 ...
  1. #1
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,481
    Thank Post
    351
    Thanked 261 Times in 213 Posts
    Rep Power
    99

    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.

  2. #2
    themightymrp's Avatar
    Join Date
    Dec 2009
    Location
    Leeds, West Yorkshire
    Posts
    1,204
    Thank Post
    212
    Thanked 223 Times in 192 Posts
    Rep Power
    72
    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:

    Code:
    for /f "tokens=2*" %%a in ('net user "%Username%" /domain ^| find /i "Full Name"') do set DisplayName=%%b

  3. Thanks to themightymrp from:

    mrbios (8th October 2012)

  4. #3
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,481
    Thank Post
    351
    Thanked 261 Times in 213 Posts
    Rep Power
    99
    What would i change on that line if i wanted %'SN'% and %'givenName'% from AD in the format of Surname, Lastname?

  5. #4
    themightymrp's Avatar
    Join Date
    Dec 2009
    Location
    Leeds, West Yorkshire
    Posts
    1,204
    Thank Post
    212
    Thanked 223 Times in 192 Posts
    Rep Power
    72
    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?

  6. #5
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,481
    Thank Post
    351
    Thanked 261 Times in 213 Posts
    Rep Power
    99
    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

  7. #6
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,481
    Thank Post
    351
    Thanked 261 Times in 213 Posts
    Rep Power
    99
    Found this:
    Code:
    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
    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.

    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.

  8. #7

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    21
    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:

    Code:
    adtoenv.exe FORENAME=givenName SURNAME=sn
    Will work with any AD attribute really, although some values are not human readable.

    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.
    Attached Files Attached Files
    Last edited by ChrisMiles; 8th October 2012 at 01:13 PM.

  9. Thanks to ChrisMiles from:

    mrbios (8th October 2012)

  10. #8
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,481
    Thank Post
    351
    Thanked 261 Times in 213 Posts
    Rep Power
    99
    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

  11. #9

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    21
    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?

  12. #10
    themightymrp's Avatar
    Join Date
    Dec 2009
    Location
    Leeds, West Yorkshire
    Posts
    1,204
    Thank Post
    212
    Thanked 223 Times in 192 Posts
    Rep Power
    72
    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

  13. #11
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,481
    Thank Post
    351
    Thanked 261 Times in 213 Posts
    Rep Power
    99
    Quote Originally Posted by ChrisMiles View Post
    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?
    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.

    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

  14. #12

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    21
    Quote Originally Posted by themightymrp View Post
    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
    Run it from a dedicated login script, but how you manage the execution order, I'm not too sure.

  15. #13

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    21
    Quote Originally Posted by mrbios View Post
    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.

    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
    Thats not going to work, im pretty sure login scripts run after all the stuff. Let me think about it...

  16. #14
    mrbios's Avatar
    Join Date
    Jun 2007
    Location
    Stroud, Gloucestershire
    Posts
    2,481
    Thank Post
    351
    Thanked 261 Times in 213 Posts
    Rep Power
    99
    Quote Originally Posted by ChrisMiles View Post
    Thats not going to work, im pretty sure login scripts run after all the stuff. Let me think about it...
    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?

  17. #15

    Join Date
    Mar 2011
    Location
    Bournemouth
    Posts
    280
    Thank Post
    16
    Thanked 74 Times in 64 Posts
    Rep Power
    21
    Ok Plan B....

    This application will read attributes from the current user context...

    Use it like this to create your folder...

    Code:
    for /f %a in ('GetADAttributes.exe givenName') do for /f %b in ('GetADAttributes.exe sn') do mkdir "C:\%b, %a"
    Attached Files Attached Files

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. adding Environment Variables?
    By GiantEnemyAnt in forum Scripts
    Replies: 7
    Last Post: 11th August 2009, 08:17 PM
  2. application data Environment variables redirect
    By ful56_uk in forum Wireless Networks
    Replies: 14
    Last Post: 27th July 2009, 10:23 PM
  3. Environment Variables Again
    By Reaper in forum Scripts
    Replies: 2
    Last Post: 19th February 2009, 04:45 PM
  4. Batch: Text file -> Environment variable?
    By PiqueABoo in forum Scripts
    Replies: 5
    Last Post: 13th December 2006, 11:09 PM
  5. Environment variables or other method
    By HodgeHi in forum Scripts
    Replies: 3
    Last Post: 21st June 2006, 09:05 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •