Monday, August 23, 2010

My bat file grows apace...

I keep making this easier and easier for the tech support guys to use. It is mildly gratifying.

Things the bat now does…

- Creates a temp file with all of the new MAC addresses added during that session (this file opens at the end when you are ready to document the new MACs in the Excel spread sheet… this helps save you from having to retype the MACs especially if you were adding them from a hand written note)

- Displays a cool blinking warning if you try to run the ‘app’ (I know, I am stretching there) while another admin is using it. This is because when you run the bat, a temp file is created and the bat checks for the existence of this file before allowing you to proceed. It’s to avoid more than one person editing the MAClist at a time to avoid duplicate entries. The last thing the bat does is delete this file before exiting.

- Error checking. Macs entered must now be no more or less than 12 characters long but before the counting happens, spaces and hyphens are removed. MACs can only use numbers and or letters A-F.


Old features

- Add new Macs one at a time to the existing MAClist text file

- Menu for controlling the DHCP service remotely via the ‘sc’ (service control) DOS command. Start, Stop and Query are available.

- Opens Excel spread sheet at the end so you can document newly added MAC addresses (user name, computer name, MAC and if applicable, expiration date).

If you are a nerd and actually care about this stuff, see below...





@echo off

:: This checks for the existence of the 'inuse' file which is created when someone is using
:: the maclist control app.  If the file exists it sends you to a message saying to try again later.

if exist \\lchqfp01\dhcp$\inuse.txt goto blink else goto prerun


:: This creates a temporary 'in-use' file to help avoid the problem with multiple admins using this
:: utility at the same time and potentially causing overwrites or redundant entries in the Maclist.

:prerun

echo This application is in use by another administrator (%username%).>>\\lchqfp01\dhcp$\inuse.txt


:: Creates a temp directory to hold temp txt file containing all macs entered in this session.  This
:: text file opens at the end so you can copy/paste into the XLS file.

md c:\temp007
del c:\temp007 /Q
echo Please copy these macs to the clipboard.  When you close this text file the Excel Spreadsheet will open and you can paste them in:>>c:\temp007\macs.txt
echo.>>C:\temp007\macs.txt


:: I got the 'convert case' parts from this website --
:: http://www.robvanderwoude.com/battech_convertcase.php which says to use the SETLOCAL
:: ENABLEDELAYEDEXPANSION command. Dunno what it does precisely besides making my code work.

:top

SETLOCAL ENABLEDELAYEDEXPANSION
cls
echo.
echo.
echo MACLIST MANAGEMENT CONSOLE
echo ==========================
echo.


:: Here is where the user enters a MAC address... the 'LoCase' routine will change
:: uppercase to lowercase and remove spaces and hyphens since neither are permitted
:: in the MAC list.  It sets the variable 'mac' as whatever the user inputs, then the
:: variable is put through the subroutine and finally the end result is added to an
:: existing text file called 'maclist.txt'.

:choice1
echo Capital letters will be converted to lowercase
echo     and spaces and hyphens will be removed
echo.
SET /p mac=[Enter a MAC address]?
CALL :LoCase mac


:: Ensures that the input does not exceed or come short of exactly 12 characters
  set Namecheck=%mac:~0,11%
  if "%NameCheck%"=="%mac%" (
    echo. ERROR: A MAC address cannot be less than 12 chars
    pause
   goto :top
  )
  set Namecheck2=%mac:~0,12%
  if not "%NameCheck2%"=="%mac%" (
    echo. ERROR: A MAC address cannot be more than 12 chars
    pause
   goto :top
  )


:: Ensures that the MAC address only uses hexadecimal notation (letters g-z not allowed)

echo %mac% | findstr /R /C:[g-z]>nul
if errorlevel 1 goto :check
if not errorlevel 1 echo.ERROR: Hexadecimal notation only uses numbers and A-F.
pause
goto:top


::This section does a find command on the maclist text file and if the entered MAC already is on the list
::an error message is given and they are taken back to the add mac section.

:check
find /I "%mac%" \\lchqcdc01\system32$\MAClist.txt
IF NOT errorlevel 1 goto redundant
IF errorlevel 1 goto :ADD

:redundant
cls
echo.
echo This mac is already in the list.  Would you like to:
echo.
echo 1. Add a different MAC
echo 2. Copy maclist and continue
echo 3. Exit this utility now
echo.
echo **Only use option 3 if you have NOT entered any MACs successfully
echo   and you are finished with the utility.
echo.
:choice3
set /P C1=[1,2,3]?
if "%C1%"=="1" goto top
if "%C1%"=="2" goto copy
if "%C1%"=="3" goto exitnow
goto choice3


::Appends the entered MAC address to a file on the network.

:ADD
echo %mac%>>\\lchqcdc01\system32$\MAClist.txt
echo %mac%>>c:\temp007\macs.txt
ENDLOCAL
GOTO:NEXT

:: Subroutine to convert a variable VALUE to all lower case and remove spaces and hyphens.
:: The argument for this subroutine is the variable NAME.
:: Only A-F are included as hexadecimal doesn't use the rest of the alphabet.

:LoCase

SET %~1=!%1:A=a!
SET %~1=!%1:B=b!
SET %~1=!%1:C=c!
SET %~1=!%1:D=d!
SET %~1=!%1:E=e!
SET %~1=!%1:F=f!
SET %~1=!%1: =!
SET %~1=!%1:-=!
GOTO:EOF

:NEXT
cls
echo MAC has been added...
echo.
echo 1. Add another MAC
echo 2. Copy maclist and continue
echo.

:choice2
set /P C1=[1,2]?
if "%C1%"=="1" goto top
if "%C1%"=="2" goto copy
goto choice2


::Copies the newly edited MAClist from the primary to the secondary DHCP server

:copy
echo Copying newly edited MAClist from LCHQCDC01 to LCHQCDC02...
copy \\lchqcdc01\system32$\MAClist.txt \\lchqcdc02\calloutdll$\MAClist.txt /y
pause
color 0E
goto menu


:: This menu allows users to issues service control commands to two DHCP servers.
:: The MACfilter list is not reapplied until the DHCP services have been restarted.

:menu
cls
echo.
echo Please choose from the following menu
echo =========================================
echo.
echo 1. Stop DHCP service on both corporate DCs
echo.
echo 2. Start DHCP service on both corporate DCs
echo.
echo 3. Query DHCP server status on both corporate DCs
echo.
echo 4. Open Excel spreadsheet and Quit
echo.

:choice3
set /P C=[1,2,3,4]?
if "%C%"=="4" goto quit
if "%C%"=="3" goto query
if "%C%"=="2" goto start
if "%C%"=="1" goto stop
goto choice3

:stop
cls
sc \\lchqcdc02 stop DHCPserver
sc \\lchqcdc01 stop DHCPserver
pause
goto menu

:start
cls
sc \\lchqcdc02 start DHCPserver
sc \\lchqcdc01 start DHCPserver
pause
goto menu

:query
cls
sc \\lchqcdc02 query DHCPserver
sc \\lchqcdc01 query DHCPserver
pause
goto menu


:: This last step opens an Excel spreadsheet where we manually document new MAC addresses
:: which have been added to the MAC filter list.

:quit
"c:\temp007\macs.txt"
"\\lchqfp01\DHCP$\MAC-HOST-USER list.xlsx"

:exitnow
del \\lchqfp01\dhcp$\inuse.txt /Q
exit

:blink
cls
echo.
echo.
color 47
echo                    ------------------------------
echo                    i ++++++++++++++++++++++++++ i
echo                    i +                        + i  
echo                    i +        WARNING!!!      + i
echo                    i +                        + i
echo                    i +  The DHCP control app  + i
echo                    i +  is in use by another  + i
echo                    i +     administrator.     + i
echo                    i +                        + i
echo                    i +    Please try again    + i
echo                    i +         later.         + i
echo                    i +                        + i
echo                    i ++++++++++++++++++++++++++ i
echo                    ------------------------------
echo.
\\lchqfp01\dhcp$\sleep -m 800
cls
color 07
echo.
echo.
echo                    ------------------------------
echo                    i ++++++++++++++++++++++++++ i
echo                    i +                        + i  
echo                    i +                        + i
echo                    i +                        + i
echo                    i +  The DHCP control app  + i
echo                    i +  is in use by another  + i
echo                    i +     administrator.     + i
echo                    i +                        + i
echo                    i +    Please try again    + i
echo                    i +         later.         + i
echo                    i +                        + i
echo                    i ++++++++++++++++++++++++++ i
echo                    ------------------------------
echo.
\\lchqfp01\dhcp$\sleep -m 800
cls
goto blink


No comments: