locked
Mapping a drive using GPO's with modified subsets of %Username% RRS feed

  • Question

  • Hi Guys,

    Just upgrading to a 2008R2 domain and looking at doing away with our login scripts. We use a vital part of the username to map to a drive. Our username configuration is Location&Usernumber. For example Melbourne1 will be Melbourne - User 1. We support many locations (300+) with very few users in each location (less than 5). Each location uses the first part of the username (Melbourne) to identify a mapped shared folder and the lat part (1) to set a user environmnet variable.

    All sites access is through a terminal server farm

    In our old scripts we use the expression in a batch file to set these options:

    Drive Mapping NET USE M: \\SERVERNAME\%USERNAME:~0,-1%
    Site User Variable SETX USER %USERNAME:~-1,1%

    Can this be replicated using a GPO? Each time i try it actually sets the variable to literally be %USERNAME:~-1,1% rather than 1

    Thanks in advance for your interest and any replies!

    Justin

    • Changed type Cicely Feng Tuesday, July 24, 2012 4:15 AM
    Monday, July 23, 2012 3:59 AM

Answers

  • Hello Justin,

    You should have a look at Group Policy Preferences.

    You can use LDAP queries, but therefore you may use a different field of the user account.

    http://technet.microsoft.com/library/cc731892.aspx


    MVP Group Policy - Mythen, Insiderinfos und Troubleshooting zum Thema GPOs: Let's go, use GPO!

    • Marked as answer by Cicely Feng Tuesday, July 24, 2012 4:15 AM
    Monday, July 23, 2012 6:22 AM
  • It seems however to not be able to handle the expression \\SERVERNAME\%USERNAME:~0,-1% as a drive mapping.

    That could be...
    However, you have either two possibilities:

    1. Use GPP and use separate properties (fields) of the user account

    2. Use a VBScript that splits your username:

    Set oShell = CreateObject( "WScript.Shell" )
    user=oShell.ExpandEnvironmentStrings("%UserName%")

    arrList = Split(user , " ")
    username="String name: " & arrList(0)
    Wscript.Echo "String: " & username
    For i = 1 to Ubound(arrList)
    if trim(arrList(i)) <> "" then
    Wscript.Echo "String: " & arrList(i)
    end if
    Next

    Dim objNetwork
    Set objNetwork = WScript.CreateObject("WScript.Network")
    strLocalDrive = "M:"
    strRemoteShare = "\\server\" & username
    objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False

    ...


    MVP Group Policy - Mythen, Insiderinfos und Troubleshooting zum Thema GPOs: Let's go, use GPO!

    • Marked as answer by Cicely Feng Tuesday, July 24, 2012 4:15 AM
    Monday, July 23, 2012 7:42 AM
  •  
    > %USERNAME:~0,-1%
     
    This is a function builtin to cmd.exe, not in the windows shell. As
    Matthias said: Either you have to use a script or you may populate some
    unneeded attributes in AD with the required data.
     
    regards, Martin
     

    NO THEY ARE NOT EVIL, if you know what you are doing: Good or bad GPOs?
    Wenn meine Antwort hilfreich war, freue ich mich über eine Bewertung! If my answer was helpful, I'm glad about a rating!
    • Marked as answer by Cicely Feng Tuesday, July 24, 2012 4:15 AM
    Monday, July 23, 2012 11:12 AM

All replies

  • Hello Justin,

    You should have a look at Group Policy Preferences.

    You can use LDAP queries, but therefore you may use a different field of the user account.

    http://technet.microsoft.com/library/cc731892.aspx


    MVP Group Policy - Mythen, Insiderinfos und Troubleshooting zum Thema GPOs: Let's go, use GPO!

    • Marked as answer by Cicely Feng Tuesday, July 24, 2012 4:15 AM
    Monday, July 23, 2012 6:22 AM
  • Thanks Matthias,

    This is exactly where I am looking to implement the solution. It seems however to not be able to handle the expression \\SERVERNAME\%USERNAME:~0,-1% as a drive mapping. I get an error in the event viewer that the location cannot be found. The same exists if i type the expression \\SERVERNAME\%USERNAME:~0,-1% into a windows explorer menu. If I use a CMD window the expression works.


    Justin

    Monday, July 23, 2012 6:29 AM
  • It seems however to not be able to handle the expression \\SERVERNAME\%USERNAME:~0,-1% as a drive mapping.

    That could be...
    However, you have either two possibilities:

    1. Use GPP and use separate properties (fields) of the user account

    2. Use a VBScript that splits your username:

    Set oShell = CreateObject( "WScript.Shell" )
    user=oShell.ExpandEnvironmentStrings("%UserName%")

    arrList = Split(user , " ")
    username="String name: " & arrList(0)
    Wscript.Echo "String: " & username
    For i = 1 to Ubound(arrList)
    if trim(arrList(i)) <> "" then
    Wscript.Echo "String: " & arrList(i)
    end if
    Next

    Dim objNetwork
    Set objNetwork = WScript.CreateObject("WScript.Network")
    strLocalDrive = "M:"
    strRemoteShare = "\\server\" & username
    objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False

    ...


    MVP Group Policy - Mythen, Insiderinfos und Troubleshooting zum Thema GPOs: Let's go, use GPO!

    • Marked as answer by Cicely Feng Tuesday, July 24, 2012 4:15 AM
    Monday, July 23, 2012 7:42 AM
  •  
    > %USERNAME:~0,-1%
     
    This is a function builtin to cmd.exe, not in the windows shell. As
    Matthias said: Either you have to use a script or you may populate some
    unneeded attributes in AD with the required data.
     
    regards, Martin
     

    NO THEY ARE NOT EVIL, if you know what you are doing: Good or bad GPOs?
    Wenn meine Antwort hilfreich war, freue ich mich über eine Bewertung! If my answer was helpful, I'm glad about a rating!
    • Marked as answer by Cicely Feng Tuesday, July 24, 2012 4:15 AM
    Monday, July 23, 2012 11:12 AM
  • Thanks for your responses guys. Issue resolved. Many thanks!

    Justin

    Monday, July 23, 2012 10:39 PM