I'm trying to create a batch file that calls other batch files which unlock a specific drive on a server at start. I know the unlock scripts work. The drive unlocks as it should if I run them. But when I try to automate it by creating another batch script to call those scripts based on the server name, it fails to unlock. When running the script manually it tells me the called upon script has the incorrect unlock code.
I tried adding in the full name of the server I'm aiming for.
Instead of
If %computername% == "PartialServername*" Goto Servername
I changed it to
If %computername% == "Servername" Goto Servername
I also tried running it as:
If %computername% == "Servername" Call Servername.bat
And this worked, but not if I had multiple lines of if
.
Server Unlock.bat
pushd "\\servername\scripts\unlock scripts\"
If %computerName% == "PartialServerName1*" Goto Servername
:Servername Call Servername.Bat
Servername.bat
manage-bde -unlock d: -recoverypassword *Recovery key here*
I expected it to call upon the batch file and unlock the drive, but instead the batch file calls upon the file and then says the recovery password is wrong.
Full script:
REM Created temporary network drive to the unlock scripts folder on vipre
pushd "\\scriptserver\unlock scripts\"
REM Checks the value of the Server name then goes to that section of the
script REM to run the appropriate script
If %computerName% == "Bow*" Goto Bow
If %computerName% == "Del*" Goto Del
If %computerName% == "Fin*" Goto Fin
If %computerName% == "Gah*" Goto Gah
If %computerName% == "Gib*" Goto Gib
If %computerName% == "Kal*" Goto Kal
If %computerName% == "Ken*" Goto Ken
If %computerName% == "Lei*" Goto Lei
If %computerName% == "Lew*" Goto Lew
If %computerName% == "Lim*" Goto Lim
If %computerName% == "Loa*" Goto Loa
If %computerName% == "Mar*" Goto Mar
If %computerName% == "Ott*" Goto Ott
If %Computername% == "Pem*" Goto Pem
If %computerName% == "Ric*" Goto Ric
If %computerName% == "Sha*" Goto Sha
If %computerName% == "Wes*" Goto Wes
:Bow
Call Bow.bat
Call DistributionRestart.bat
popd
:Del
Call Del.bat
Call DistributionRestart.bat
popd
:Fin
Call Fin.bat
Call DistributionRestart.bat
popd
:Gah
Call Gah.bat
Call DistributionRestart.bat
popd
:Gib
Call Gib.bat
Call DistributionRestart.bat
popd
:Kal
Call Kal.bat
Call DistributionRestart.bat
popd
:Ken
Call Ken.bat
Call DistributionRestart.bat
popd
:Lei
Call Lei.bat
Call DistributionRestart.bat
popd
:Lew
Call Lew.bat
Call DistributionRestart.bat
popd
:Lim
Call Lim.bat
Call DistributionRestart.bat
popd
:Loa
Call Loa.bat
Call DistributionRestart.bat
popd
:Mar
Call Mar.bat
Call DistributionRestart.bat
popd
:Ott
Call Ott.bat
Call DistributionRestart.bat
popd
:Pem
Call Pem.bat
Call DistributionRestart.bat
popd
:Ric
Call Ric.bat
Call DistributionRestart.bat
popd
:Sha
Call Sha.bat
Call DistributionRestart.bat
popd
:Wes
Call Wes.bat
popd
Computername is an environment variable, and so the comparison needs to be case insensitive. Use the /I
option to do a case insensitive search
You need to have equal quoting on both side of the equation
IF/I "%computername%" == "<value>" ....
. ^ ^ ^ ^
. ^ ^ ^ ^
Note the quotes around %computername% and around <value>
.
The wild card logic won't work, but a simple technique is to use a substring compare. You are always comparing 3 characters so you can change the logic to something like:
if /I "%COMPUTERNAME:~0,3%" == "Bow" .....
This starts at the beginning (index 0) and creats a string of length 3, and then compares that to Bow to do a case insensitive string comparison, where the quoting matches on both sides
Consider the following alternative script
withDistribRestart
and noDistribRestart
to trigger the respective scripts where the passed in parameter is the name of the batch script to execute@ECHO OFF
GOTO :Main
REM =========================================================================
:Main
SETLOCAL
SET "retVal=0"
pushd "\\scriptserver\unlock scripts"
if /I "%COMPUTERNAME:~0,3%" == "Bow" (
CALL :withDistribRestart Bow
) else if /I "%COMPUTERNAME:~0,3%" == "Del" (
CALL :withDistribRestart Del
) else if /I "%COMPUTERNAME:~0,3%" == "Fin" (
CALL :withDistribRestart Fin
) else if /I "%COMPUTERNAME:~0,3%" == "Gah" (
CALL :withDistribRestart Gah
) else if /I "%COMPUTERNAME:~0,3%" == "Gib" (
CALL :withDistribRestart Gib
) else if /I "%COMPUTERNAME:~0,3%" == "Kal" (
CALL :withDistribRestart Kal
) else if /I "%COMPUTERNAME:~0,3%" == "Ken" (
CALL :withDistribRestart Ken
) else if /I "%COMPUTERNAME:~0,3%" == "Lei" (
CALL :withDistribRestart Lei
) else if /I "%COMPUTERNAME:~0,3%" == "Lew" (
CALL :withDistribRestart Lew
) else if /I "%COMPUTERNAME:~0,3%" == "Lim" (
CALL :withDistribRestart Lim
) else if /I "%COMPUTERNAME:~0,3%" == "Loa" (
CALL :withDistribRestart Loa
) else if /I "%COMPUTERNAME:~0,3%" == "Mar" (
CALL :withDistribRestart Mar
) else if /I "%COMPUTERNAME:~0,3%" == "Ott" (
CALL :withDistribRestart Ott
) else if /I "%COMPUTERNAME:~0,3%" == "Pem" (
CALL :withDistribRestart Pem
) else if /I "%COMPUTERNAME:~0,3%" == "Ric" (
CALL :withDistribRestart Ric
) else if /I "%COMPUTERNAME:~0,3%" == "Sha" (
CALL :withDistribRestart Sha
) else if /I "%COMPUTERNAME:~0,3%" == "Wes" (
CALL :noDistribRestart Wes
) else (
ECHO Unexpected COMPUTERNAME '"%COMPUTERNAME%"'
SET "retVal=1"
)
popd
(ENDLOCAL
EXIT /B %retVal%)
REM ========================================================================
:withDistribRestart
SETLOCAL
SET "PREFIX=%~1"
CALL "%PREFIX%.bat"
CALL "DistributionRestart.bat"
(ENDLOCAL
EXIT /B 0)
REM ========================================================================
:noDistribRestart
SETLOCAL
SET "PREFIX=%~1"
CALL "%PREFIX%.bat"
(ENDLOCAL
EXIT /B 0)