locked
Mapping custom network drives and using AD groups RRS feed

  • Question

  • I'm trying to write a script that maps users to custom drive mappings (mapping not assigned in AD) and pull drives from AD. As of now I can achieve my goal but the problem is when a user doesn't have a custom drive mapping, I cannot call my other script that calls for the AD mappings. Do I need an ELSE statement that continues the USERNAME into the MYBYGROUP. Example below:

    REM: Remove local Drives
    net use * /d /y

    :: Reconnect Universal drives
    net use M: USERDRIVE

    net use Y: COMMONDRIVE

    SET USERDIR=%username%

    GOTO %USERDIR%

    :USERNAME
    net use F: CUSTOMDRIVE
    GOTO MAPBYGROUP

    :MAPBYGROUP

    wscript %0\..\logon.vbs

    Exit

    Tuesday, August 5, 2014 6:23 PM

Answers

  • Why not use group policy? Then you don't need a script.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Tuesday, August 5, 2014 6:31 PM
    • Marked as answer by Bill_Stewart Sunday, September 7, 2014 2:31 PM
    Tuesday, August 5, 2014 6:26 PM

All replies

  • Why not use group policy? Then you don't need a script.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Tuesday, August 5, 2014 6:31 PM
    • Marked as answer by Bill_Stewart Sunday, September 7, 2014 2:31 PM
    Tuesday, August 5, 2014 6:26 PM
  • we handle most of the drive mappings with AD groups; much easier to have our support center manage network drives this way.

    aj

    Tuesday, August 5, 2014 8:06 PM
  • Drive mappings can be managed by groups using group policy.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, August 5, 2014 8:10 PM
  • Permissions, VPN issues some other reasons. we are getting cisco AnyConnect soon so I may start looking into using GP but as of now, I have no way to map drives while user's are out of the office.

    aj

    Tuesday, August 5, 2014 8:22 PM
  • What's your specific question, then? You can map network drives by using the net use command or by using the MapNetworkDrive method of the WshNetwork object.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 6, 2014 2:59 PM
  • the above code works when :USERNAME is defined but if there is no :USERNAME the script stops and doesn't skip down to the :MYBYGROUP where are all the mappings take place.

    I tried IF EXIST %USERDIR (goto %username%) ELSE (goto MAPBYGROUP); but it seems to just map the first :USERNAME in the list and then goes to MAPBYGROUP instead of finding the corresponding :USERNAME.

    My apologize if I'm making this confusing; I don't have too much experience here (if it's not already apparent)


    aj

    Wednesday, August 6, 2014 3:05 PM
  • A line starting with a colon (:) in a shell script (batch file) is a label, not a variable.

    Try to explain what you want to do, not the code you think you need to write to do it.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 6, 2014 3:39 PM
  • I have two scripts that run at user logon; first being one close to the above example (i'm trying to make our current script a bit more efficient). This script maps users to custom mappings not defined in AD (some departments may what user's to have temp access or there maybe a user mapped to different network drive that has the same drive letter within an AD group and we need to have a separate mapping not included with AD). Not all users will have custom mappings so I need the script to skip past custom mapping down to the logon.vbs (MAPBYGROUP) which call the AD group mappings.

    Current script reads; (i'm trying to prevent every line being read but more to find the user, map a drive and/or skip to logon.vbs)

    :1
    if /i "%username%"=="USERNAME" net use F: NETWORKDRIVE
    if /i "%username%"=="USERNAME" net use L: NETWORKDRIVE
    if /i "%username%"=="USERNAME" net use J: NETWORKDRIVE
    if not errorlevel 1 goto 2


    aj

    Wednesday, August 6, 2014 3:59 PM
  • What does "custom mappings not defined in AD" mean?

    Some notes about the code:

    1) NETWORKDRIVE is not a valid remote path for mapping a drive.

    2) You are checking the exit code (errorlevel) after running 3 separate commands.

    I'm not sure we're going to be able to help you in this forum because you have a very vague problem description, and the script code you've posted is not valid.

    I think group policy settings are the best option. If you really need a script, you will probably need to bring someone in who can put eyes on your network and scenario and get you where you need to go.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 6, 2014 4:11 PM
  • yes I know NETWORKDRIVE isn't valid just a place holder for \\ server\location; I received an error trying to post it earlier so omitted the \\. the error level is really more to move down to the next line to map a different user for custom drives (what I'm trying to get away with)

    the below code is what I'd like to replace the current code with, this way i'm not reading each line but looking for a specific USERNAME then mapping custom NETWORKDRIVES then running MAPBYGROUP. As of right now, if there is no USERNAME then MAPBYGROUP doesn't run... we can continue to use our current code i'm just looking for something more efficient, thanks for the help. (btw, had to go into the HTML to break links )

    @ECHO off

    SET USERDIR=%username%

    SET U
    echo "Did I just set the USERDIR?"
    pause

     
    :USERNAME
    net use Q: \\SERVER\FOLDER
    net use L: \\SERVER\FOLDER
    net use J: \\SERVER\FOLDER
    GOTO MAPBYGROUP

    :USERNAME2
    net use Q: \\SERVER\FOLDER
    net use L: \\SERVER\FOLDER
    net use J: \\SERVER\FOLDER
    GOTO MAPBYGROUP

    :MAPBYGROUP
    echo "Running wscritp VBS Tool"
    REM wscript %0\..\logon.vbs

    goto END

    @ECHO OFF

    :END
    EXIT


    aj

    Wednesday, August 6, 2014 4:26 PM
  • Sorry, but I still don't understand how the script you're posting is related to your problem description.

    The script appears to be guesswork and/or copy-and-paste. For example:

    1. The :USERNAME and :USERNAME2 labels are not used in the script.

    2. The wscript command is commented out (it won't do anything).

    3. The @ECHO OFF command in the middle of the script, while not harmful, is superfluous (this is one reason I suspect a copy-and-paste job, because that command is usually at the beginning of a script).

    4. The EXIT command at the end of the script is unnecessary (and in fact bad for testing, because it will exit the running copy of cmd.exe if you're testing the script).

    It's not really within the scope of this forum for us to guess what you're trying to do (remember, we can't see your screen) and rewrite your script based on our guesses of what we think you're trying to say.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 6, 2014 4:35 PM
  • correct :USERNAME in the running script would be the actual username of the user; I should have removed the REM from wsript command (for testing purposes this won't work since i'm testing from my desktop and not the current file location so I added the actual file location to my test script) and yes the beginning and end were copied from my working script that is currently used when user's login, sorry for the confusion and thanks for the help.

    aj

    Wednesday, August 6, 2014 4:40 PM
  • I tried reading your problem descriptions again. I still don't understand what you want to do.

    It's not really within the scope of this forum for us to guess what you're trying to do (remember, we can't see your screen) and rewrite your script based on our guesses of what we think you're trying to say.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 6, 2014 5:57 PM
  • Just the one issue; if there is no username in script one goto script two. Right now I have no way to have the set username goto script two (MAPBYGROUP). i'm not sure of the syntax that would be needed to continue on if there is no username in script one unless I put all my usernames in the script with a goto mapbygroup to continue onto the second script.

    aj

    Wednesday, August 6, 2014 6:21 PM
  • What does "if there is no username in script one" mean?

    The USERNAME environment variable is always defined when a user logs on.

    You need to start over and try to explain in very simple language what you want to do. Don't try to interpret what you want to do in a script.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 6, 2014 6:30 PM
  • John, Jack and Jill have defined custom network drives mapped with my first script. when these 3 users login, the 1st script maps the drives defined by myself and then maps network drives defined in AD groups.

    Abe logs in and has no user defined mapping so the script doesn't continue down to the network drives defined in AD groups (this is my issue).

    If the username is in the first script everything runs as i'd like, if the username isn't defined then the process doesn't work.


    aj

    Wednesday, August 6, 2014 6:45 PM
  • Start with basic logic. Do you mean something like this?


    @echo off
    if /i "%USERNAME%"=="john" goto :FOUND
    if /i "%USERNAME%"=="jack" goto :FOUND
    if /i "%USERNAME%"=="jill" goto :FOUND
    echo User not defined
    goto :EOF
    :FOUND
    echo User defined - %USERNAME%
    

    Make sure that you can read and understand this script before you proceed.

    If you don't understand what this script is doing, then I would say you need to understand the basics of shell scripts (batch files) before you continue. It's not really possible to teach a scripting language incrementally in a forum.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 6, 2014 8:06 PM
  • I'm not sure about all of these GOTO commands.

    How about an ELSE IF block to map the drives for the "Special" users, and the script ends by running the network script for everyone. [Or am I missing something here?]

    @echo off
    
    echo %username%
    
    If "%username%"=="john" (
    net use Q: \\SERVER\FOLDER
    net use L: \\SERVER\FOLDER
    net use J: \\SERVER\FOLDER
    ) ELSE IF "%username%"=="jack" (
    net use Q: \\SERVER\FOLDER
    net use L: \\SERVER\FOLDER
    net use J: \\SERVER\FOLDER
    ) ELSE IF "%username%"=="jill" (
    net use Q: \\SERVER\FOLDER
    net use L: \\SERVER\FOLDER
    net use J: \\SERVER\FOLDER
    ) ELSE (
    echo "%username%" has no special drive mappings
    )
    
    wscript %0\..\logon.vbs


    Thursday, August 7, 2014 2:50 PM
  • that's I bit more what I was imagining; I'm super busy today but I'll let you know once I can find time to test. thanks!

    aj

    Thursday, August 7, 2014 2:59 PM
  • ok - after a bit more research - "findstr" ended up being the key and error checking.

    REM First, set the USERDIR variable equal to the users's Network ID
    REM Search the "bat file" looking for a label associated with the user's Network_ID
    REM *** The ERRORLEVEL check must IMMEDIATELY follow "FINDSTR" (since it checks the result of the last command) ***
    REM If FINDSTR does not find a heading for the user (ERRORLEVEL <> 0), immediately jump to the heading :MAPBYGROUP
    REM If you FINDSTR does find a heading for the user (ERRORLEVEL == 0), GOTO that user's heading

    SET USERDIR=%username%
    FINDSTR /I /C:":%USERDIR%" "current bat file"
    IF ERRORLEVEL 1 GOTO MAPBYGROUP
    GOTO %USERDIR%
    REM
    REM:Individual Drive Mappings-----------------------------------------

    :"UserName"

    net use F: \\ (to file location)GOTO MAPBYGROUP

    :"UserName2"

    net use F: \\ (to file location)GOTO MAPBYGROUP

    :MAPBYGROUP

    start (vbs script)


    aj

    Tuesday, August 12, 2014 12:44 PM
  • Strange. Why assign USERDIR variable with content of the USERNAME variable? Seems superfluous.

    Here's another way that will run faster because it doesn't need findstr.exe:


    @echo off
    if /i "%USERNAME%"=="john" goto :%USERNAME%
    if /i "%USERNAME%"=="jack" goto :%USERNAME%
    if /i "%USERNAME%"=="jill" goto :%USERNAME%
    goto :MAPBYGROUP
    
    :JOHN
    echo Map drive for john
    goto :MAPBYGROUP
    
    :JACK
    echo Map drive for jack
    goto :MAPBYGROUP
    
    :JILL
    echo Map drive for jill
    goto :MAPBYGROUP
    
    :MAPBYGROUP
    echo Map drives by group
    


    -- Bill Stewart [Bill_Stewart]


    • Edited by Bill_Stewart Tuesday, August 12, 2014 2:42 PM Correction
    Tuesday, August 12, 2014 2:29 PM