Link to home
Start Free TrialLog in
Avatar of Olevo
OlevoFlag for Australia

asked on

how to make checking procedure in logon batch file

Hello there,

I have found this logon batch file script somewhere on the internet and have modified it to suits for our domain. I like author idea of drive mappings. Drive mappings are broken out from IFMEMBER statements, because a user might be in a read-only group, or they might be in another group that has more permissions. Either way, the NET USE command is used  only once which is more efficient in case the user is in both groups. Logon script work fine and very fast.

Here is my problem. If the user have USB hard drive or USB flash stick with the same drive letter, logon batch file fails with this error:
"System error 85 has occurred.
The local device name is already in use."

Because of this error user don't have network drive mapping. The only way to fix this problem is to manually change drive letter for USB drive to be something different and re-run batch file again.

I have some skills in batch file creation but luck of more advance knowledge and time put me on pressure to make this scrip work correctly as soon as possible.

I need someone with good batch file creation skills to modify/edit my script to include checking procedure. Let's say, if the Drive Letter already in use by USB hard drive,  logon batch script should assign next available drive letter for the network drive. Would be nice (and preferable) if batch logon script can change drive letter for the actual USB drive so we will have consistent network drive letters name but I guess it will much more difficult to achieve. Some kind of user warning notification should be also pop-up for the user saying that, because this drive letter already in use by USB hard drive the logon batch file temporary is using other letter for this network drive.

For some of the wireless laptop users logon batch script is not always run during logon. I'm thinking on start using persistent drive mapping. So, if network logon script is missed during logon, users will have network drives anyway. I heard somewhere that using persistent map drives not always a good idea. Reason for this can be a situations when network drives locations have been change. Users with persistent old drives will see "The local device name is already in use" error message. They need to disconnect the old mapping and re-logon.

As I said before I would like to start using persistent map drives (net use /persistent:yes) so my batch logon script has to be smart enough to detect old persistent drives, remove them and map new once.
   
Thanks in advance.
Avatar of Olevo
Olevo
Flag of Australia image

ASKER

========= here is a logon batch file =============
@ECHO OFF
REM This is the Logon Script as of 22-04-2010
Echo Welcome to the CONTOSO domain. Please wait while your logon is processed...
Echo.
REM Check to see if logon machine is NT. If not, move to END of script.
IF NOT "%OS%"=="Windows_NT" ECHO Windows 95/98 is not supported by CONTOSO! Please contact the CONTOSO helpdesk for assistance.& GOTO :EOF
REM Change title of window for NT machines
TITLE CONTOSO LOGON SCRIPT
REM Check to see if the user has administrative rights on the workstation. Display no output.
%logonserver%\netlogon\ifmember "BUILTIN\Administrators" > nul
REM If not, goto ALLCONTINUE
If NOT %errorlevel%==1 GOTO :ALLCONTINUE

:ALLCONTINUE

REM ================ CONTOSO Network Drives =========================================
REM   Drive            Path                        Access by
REM -----------------------------------------------------------------------------------------------------------------------------------------
REM       I:            \\SERVER1\COMMON            All
REM       P:            \\SERVER1\APPS                  All
REM       K:            \\SERVER8\SHARE                  HR, Office
REM       J:            \\SERVER8\STAFFSHR            Staff, Office

REM Drive mappings are broken out from IFMEMBER statements, because a user might be in a read-only group,
REM or they might be in another group that has more permissions. Either way, the NET USE command is used
REM only once which is more effecient in case the user is in both groups.

REM -- Drive Mappings for ALL Users
%logonserver%\netlogon\ifmember "CONTOSO\Domain Users" > nul
IF ERRORLEVEL 1 SET MAPDRV-I=TRUE
IF ERRORLEVEL 1 SET MAPDRV-P=TRUE

REM -- Drive Mappings for HR Users
%logonserver%\netlogon\ifmember "CONTOSO\HR" > nul
IF ERRORLEVEL 1 SET MAPDRV-K=TRUE

REM -- Drive Mappings for Office Users
%logonserver%\netlogon\ifmember "CONTOSO\Office" > nul
IF ERRORLEVEL 1 SET MAPDRV-K=TRUE
IF ERRORLEVEL 1 SET MAPDRV-J=TRUE

REM -- Drive Mappings for Staff Users
%logonserver%\netlogon\ifmember "CONTOSO\Staff" > nul
IF ERRORLEVEL 1 SET MAPDRV-J=TRUE

echo Mapping Network Drives. Please wait...

IF [%MAPDRV-I%]==[TRUE] (IF EXIST I: NET USE /d I:
NET USE I: "\\SERVER1\COMMON")

IF [%MAPDRV-P%]==[TRUE] (IF EXIST P: NET USE /d P:
NET USE P: "\\SERVER1\APPS")

IF [%MAPDRV-K%]==[TRUE] (IF EXIST K: NET USE /d K:
NET USE K: "\\SERVER8\SHARE")

IF [%MAPDRV-J%]==[TRUE] (IF EXIST J: NET USE /d J:
NET USE J: "\\SERVER8\STAFFSHR")

echo Network Drive Mapping finished...

:EOF
exit  
======== END of logon batch file =============
Avatar of Olevo

ASKER

Ok, maybe I'm asking too much. I need simple checking procedure for using different drive letters if one already in use.  
Avatar of AmazingTech
AmazingTech

Here's a little piece of code which should work.
Set DriveLetters contains the set of letters you want available to be assigned.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

Set DriveLetters=E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z

REM This is the Logon Script as of 22-04-2010
Echo Welcome to the CONTOSO domain. Please wait while your logon is processed...
Echo.
REM Check to see if logon machine is NT. If not, move to END of script.
IF NOT "%OS%"=="Windows_NT" ECHO Windows 95/98 is not supported by CONTOSO! Please contact the CONTOSO helpdesk for assistance.& GOTO :EOF
REM Change title of window for NT machines
TITLE CONTOSO LOGON SCRIPT
REM Check to see if the user has administrative rights on the workstation. Display no output.
%logonserver%\netlogon\ifmember "BUILTIN\Administrators" > nul
REM If not, goto ALLCONTINUE
If NOT %errorlevel%==1 GOTO :ALLCONTINUE

:ALLCONTINUE

REM ================ CONTOSO Network Drives =========================================
REM   Drive            Path                        Access by
REM -----------------------------------------------------------------------------------------------------------------------------------------
REM       I:            \\SERVER1\COMMON            All
REM       P:            \\SERVER1\APPS                  All
REM       K:            \\SERVER8\SHARE                  HR, Office
REM       J:            \\SERVER8\STAFFSHR            Staff, Office

REM Drive mappings are broken out from IFMEMBER statements, because a user might be in a read-only group,
REM or they might be in another group that has more permissions. Either way, the NET USE command is used
REM only once which is more effecient in case the user is in both groups.

REM -- Drive Mappings for ALL Users
%logonserver%\netlogon\ifmember "CONTOSO\Domain Users" > nul
IF ERRORLEVEL 1 SET MAPDRV-I=TRUE
IF ERRORLEVEL 1 SET MAPDRV-P=TRUE

REM -- Drive Mappings for HR Users
%logonserver%\netlogon\ifmember "CONTOSO\HR" > nul
IF ERRORLEVEL 1 SET MAPDRV-K=TRUE

REM -- Drive Mappings for Office Users
%logonserver%\netlogon\ifmember "CONTOSO\Office" > nul
IF ERRORLEVEL 1 SET MAPDRV-K=TRUE
IF ERRORLEVEL 1 SET MAPDRV-J=TRUE

REM -- Drive Mappings for Staff Users
%logonserver%\netlogon\ifmember "CONTOSO\Staff" > nul
IF ERRORLEVEL 1 SET MAPDRV-J=TRUE

echo Mapping Network Drives. Please wait...

IF [%MAPDRV-I%]==[TRUE] CALL :NETUSE I "\\SERVER1\COMMON"

IF [%MAPDRV-P%]==[TRUE] CALL :NETUSE P "\\SERVER1\APPS"

IF [%MAPDRV-K%]==[TRUE] CALL :NETUSE K "\\SERVER8\SHARE"

IF [%MAPDRV-J%]==[TRUE] CALL :NETUSE J "\\SERVER8\STAFFSHR"

echo Network Drive Mapping finished...

exit  


:NETUSE
IF EXIST %1 NET USE %1: /d
NET USE %1: %2
if errorlevel 1 (
    echo Error mapping %1: to %2
    Set FoundDrive=
    for %%a in (%Driveletters%) do (
        if not defined FoundDrive if %%a gtr %1 (
            NET USE %%a: %2 1>NUL 2>NUL
            if not errorlevel 1 Set FoundDrive=%%a
        )
    )
    if defined FoundDrive (
        echo Mapped !FoundDrive!: to %2.
        echo Note standard drive letter %1: is now !FoundDrive!:
    ) else (
        echo Could not find a drive letter for standard drive %1: for network path %2
    )
    PAUSE
)
Avatar of Olevo

ASKER

Thanks for the help, now we're going somewhere... Script is mapping drives but with a few glitches. First of all if the network drive already exist, script just maps a copy of it with new drive letter?! I run script one more time and I have third the same mapping just with a different drive letter. To get rid of existing network drives I put (not sure if that right) net use * /delete /yes just below :ALLCONTINUE tag. Now, all drives are deleted but script maps network drives with different letters?! Let's say, script should map path \\SERVER1\COMMON to I: drive but instead it's map it to next available letter. Why, I don't even have I: drive mapped yet?!
Thanks AmazingTech for sorting out this for me. I have noticed that you put echo line about "standard drive letter of X: is now Y:" The problem with this (for us) is that logon batch file is running minimised on the users computers. They will not see those warning messages. I was wondering if some kind of pop-up windows will tell users about that drive letters were remapped. It probably can be done much easily with vbscript but our requirements is to use batch file commands only. From my memory (correct me if I wrong) Microsoft even have some kind of pop-up utility on their Resource Kits.
Opps. OK try this.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

Set DriveLetters=E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z

REM This is the Logon Script as of 22-04-2010
Echo Welcome to the CONTOSO domain. Please wait while your logon is processed...
Echo.
REM Check to see if logon machine is NT. If not, move to END of script.
IF NOT "%OS%"=="Windows_NT" ECHO Windows 95/98 is not supported by CONTOSO! Please contact the CONTOSO helpdesk for assistance.& GOTO :EOF
REM Change title of window for NT machines
TITLE CONTOSO LOGON SCRIPT
REM Check to see if the user has administrative rights on the workstation. Display no output.
%logonserver%\netlogon\ifmember "BUILTIN\Administrators" > nul
REM If not, goto ALLCONTINUE
If NOT %errorlevel%==1 GOTO :ALLCONTINUE

:ALLCONTINUE

REM ================ CONTOSO Network Drives =========================================
REM   Drive            Path                        Access by
REM -----------------------------------------------------------------------------------------------------------------------------------------
REM       I:            \\SERVER1\COMMON            All
REM       P:            \\SERVER1\APPS                  All
REM       K:            \\SERVER8\SHARE                  HR, Office
REM       J:            \\SERVER8\STAFFSHR            Staff, Office

REM Drive mappings are broken out from IFMEMBER statements, because a user might be in a read-only group,
REM or they might be in another group that has more permissions. Either way, the NET USE command is used
REM only once which is more effecient in case the user is in both groups.

REM -- Drive Mappings for ALL Users
%logonserver%\netlogon\ifmember "CONTOSO\Domain Users" > nul
IF ERRORLEVEL 1 SET MAPDRV-I=TRUE
IF ERRORLEVEL 1 SET MAPDRV-P=TRUE

REM -- Drive Mappings for HR Users
%logonserver%\netlogon\ifmember "CONTOSO\HR" > nul
IF ERRORLEVEL 1 SET MAPDRV-K=TRUE

REM -- Drive Mappings for Office Users
%logonserver%\netlogon\ifmember "CONTOSO\Office" > nul
IF ERRORLEVEL 1 SET MAPDRV-K=TRUE
IF ERRORLEVEL 1 SET MAPDRV-J=TRUE

REM -- Drive Mappings for Staff Users
%logonserver%\netlogon\ifmember "CONTOSO\Staff" > nul
IF ERRORLEVEL 1 SET MAPDRV-J=TRUE

echo Mapping Network Drives. Please wait...

IF [%MAPDRV-I%]==[TRUE] CALL :NETUSE I "\\SERVER1\COMMON"

IF [%MAPDRV-P%]==[TRUE] CALL :NETUSE P "\\SERVER1\APPS"

IF [%MAPDRV-K%]==[TRUE] CALL :NETUSE K "\\SERVER8\SHARE"

IF [%MAPDRV-J%]==[TRUE] CALL :NETUSE J "\\SERVER8\STAFFSHR"

echo Network Drive Mapping finished...

exit  


:NETUSE
IF EXIST %1: NET USE %1: /delete /yes
NET USE %1: %2
if errorlevel 1 (
    echo Error mapping %1: to %2
    Set FoundDrive=
    for %%a in (%Driveletters%) do (
        if not defined FoundDrive if %%a gtr %1 (
            NET USE %%a: %2 1>NUL 2>NUL
            if not errorlevel 1 Set FoundDrive=%%a
        )
    )
    if defined FoundDrive (
        echo Mapped !FoundDrive!: to %2.
        msg %username% /w Note standard drive letter %1: is now !FoundDrive!:
    ) else (
        msg %username% /w Could not find a drive letter for standard drive %1: for network path %2
    )
)
Avatar of Olevo

ASKER

Thanks AmazingTech, haven’t tryed your updated script yet, I’ll do it tomorrow at work. If we’re already mapping network drives using letters I, P, K and J do we still need to specify them in “Set DriveLetters=E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z” or can I just simply take them off from Ser DriveLetters command? Sorry, don’t have Windows XP in front of me, will be “msg” command supported on XP?
Avatar of Olevo

ASKER

When I run this script I've got this message: "'msg' is not recognized as an internal or external command, operable program or batch file."  ;-(

I'm testing this script on Windows 7, however majority of our users still running Windows XP. Script still mapping using wrong drive letters?!  
Yes. Remove the drive letters from the Set DriveLetters.

msg is available on XP.
Avatar of Olevo

ASKER

AmazingTech thank you very much for your help. Every time when drive re-maps I have notification popup, that is what I want. However, instead of multiple pop-ups when the end user has to click "OK" button can we have one pop-up with the summary of the all remapped drives?
When user already logged on to the system and I'm trying to run logon script from \\server1\netlogon\logon.bat I always have "'msg' is not recognized as an internal or external command, operable program or batch file." But during normal logon time I don't have "msg" error message. Drives still get wrong letters assign to them even though those letter are not taken yet?!
I can't see why the first NET USE %1: %2 would fail?
Might need to introduce a delay for diagnosing.

Add "ping 127.0.0.1" before the NET USE command.

You could try specifying the exact path to the msg.exe.
%systemroot%\system32\msg.exe

Going to have to look into the summary part.
Avatar of Olevo

ASKER

No pushing... I was wondering if you had a chance to update this script with one summary pop-up window for me please.
Thanks for the reminder. I'm having problems getting msg.exe to do multiple lines.
Perhaps, we can use vbs instead of msg.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

Set DriveLetters=E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
if exist "%temp%\Summary.vbs" del "%temp%\Summary.vbs"

REM This is the Logon Script as of 22-04-2010
Echo Welcome to the CONTOSO domain. Please wait while your logon is processed...
Echo.
REM Check to see if logon machine is NT. If not, move to END of script.
IF NOT "%OS%"=="Windows_NT" ECHO Windows 95/98 is not supported by CONTOSO! Please contact the CONTOSO helpdesk for assistance.& GOTO :EOF
REM Change title of window for NT machines
TITLE CONTOSO LOGON SCRIPT
REM Check to see if the user has administrative rights on the workstation. Display no output.
%logonserver%\netlogon\ifmember "BUILTIN\Administrators" > nul
REM If not, goto ALLCONTINUE
If NOT %errorlevel%==1 GOTO :ALLCONTINUE

:ALLCONTINUE

REM ================ CONTOSO Network Drives =========================================
REM   Drive            Path                        Access by
REM -----------------------------------------------------------------------------------------------------------------------------------------
REM       I:            \\SERVER1\COMMON            All
REM       P:            \\SERVER1\APPS                  All
REM       K:            \\SERVER8\SHARE                  HR, Office
REM       J:            \\SERVER8\STAFFSHR            Staff, Office

REM Drive mappings are broken out from IFMEMBER statements, because a user might be in a read-only group,
REM or they might be in another group that has more permissions. Either way, the NET USE command is used
REM only once which is more effecient in case the user is in both groups.

REM -- Drive Mappings for ALL Users
%logonserver%\netlogon\ifmember "CONTOSO\Domain Users" > nul
IF ERRORLEVEL 1 SET MAPDRV-I=TRUE
IF ERRORLEVEL 1 SET MAPDRV-P=TRUE

REM -- Drive Mappings for HR Users
%logonserver%\netlogon\ifmember "CONTOSO\HR" > nul
IF ERRORLEVEL 1 SET MAPDRV-K=TRUE

REM -- Drive Mappings for Office Users
%logonserver%\netlogon\ifmember "CONTOSO\Office" > nul
IF ERRORLEVEL 1 SET MAPDRV-K=TRUE
IF ERRORLEVEL 1 SET MAPDRV-J=TRUE

REM -- Drive Mappings for Staff Users
%logonserver%\netlogon\ifmember "CONTOSO\Staff" > nul
IF ERRORLEVEL 1 SET MAPDRV-J=TRUE

echo Mapping Network Drives. Please wait...

IF [%MAPDRV-I%]==[TRUE] CALL :NETUSE I "\\SERVER1\COMMON"

IF [%MAPDRV-P%]==[TRUE] CALL :NETUSE P "\\SERVER1\APPS"

IF [%MAPDRV-K%]==[TRUE] CALL :NETUSE K "\\SERVER8\SHARE"

IF [%MAPDRV-J%]==[TRUE] CALL :NETUSE J "\\SERVER8\STAFFSHR"

echo Network Drive Mapping finished...
if exist "%temp%\Summary.vbs" wscript "%temp%\Summary.vbs"
exit  


:NETUSE
IF EXIST %1: NET USE %1: /delete /yes
NET USE %1: %2
if errorlevel 1 (
    echo Error mapping %1: to %2
    Set FoundDrive=
    for %%a in (%Driveletters%) do (
        if not defined FoundDrive if %%a gtr %1 (
            NET USE %%a: %2 1>NUL 2>NUL
            if not errorlevel 1 Set FoundDrive=%%a
        )
    )
    if defined FoundDrive (
        echo Mapped !FoundDrive!: to %2.
        echo wscript.echo "Note standard drive letter %1: is now !FoundDrive!:" + chr^(13^)>>"%temp%\Summary.vbs"
    ) else (
        echo wscript.echo "Could not find a drive letter for standard drive %1: for network path %2" + chr^(13^)>>"%temp%\Summary.vbs"
    )
)
ASKER CERTIFIED SOLUTION
Avatar of AmazingTech
AmazingTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Olevo

ASKER

Sorry for the delay. I’ll try to have a look at your script shortly and let you know with the results.
Avatar of Olevo

ASKER

Sorry for not responding for this post. I still very busy at work right now I will try respond as soon as I can.
Avatar of Olevo

ASKER

I’m sorry AmazingTech for not responding. Was and still very busy at work and didn’t have a chance to check your batch file. I have tried your new batch file but the drives still get wrong letters assign to them even though those letter are not taken yet?! Any troubleshooting ideas?
Remove the >nul and 2>nul, put a pause before exit, and watch the output for messages and errors.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Olevo

ASKER

Thanks Qlemo, I’ll try that tomorrow at work.