Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for sharemig.bat

(view source code of sharemig.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version -- Windows 2000 or later
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. VER | FIND.EXE "Windows NT" >NUL
  5. IF NOT ERRORLEVEL 1 GOTO Syntax
  6.  
  7. :: Check command line arguments -- none required
  8. IF NOT "%~1"=="" GOTO Syntax
  9.  
  10. :: Save a list of ALL shares found on the source server
  11. NET.EXE SHARE > "%~dp0%ComputerName%_originalshares.txt" 2>&1
  12.  
  13. :: List only the shares that should be recreated on the target server
  14. :: Note: Remove the FINDSTR filters for VPHOME and VPLOGON
  15. ::       if Norton AntiVirus is not installed on the servers
  16. NET.EXE SHARE | FIND.EXE /I /V " Spooled " | FINDSTR.EXE /I /V /R /B /C:"PRINT\$" | FINDSTR.EXE /I /V /R /B /C:"IPC\$" | FINDSTR.EXE /I /V /R /B /C:"ADMIN\$" | FINDSTR.EXE /I /V /R /B /C:"[A-Z]\$" | FINDSTR.EXE /I /V /R /B /C:"VPHOME" | FINDSTR.EXE /I /V /R /B /C:"VPLOGON" > "%~dp0%ComputerName%_netshares.txt"
  17.  
  18. :: Add a prompt for confirmation to the automatically generated batch files
  19. >  "%~dp0%ComputerName%_recreate_shares.bat" ECHO @ECHO OFF
  20. >> "%~dp0%ComputerName%_recreate_shares.bat" ECHO CLS
  21. >> "%~dp0%ComputerName%_recreate_shares.bat" ECHO ECHO.
  22. >> "%~dp0%ComputerName%_recreate_shares.bat" ECHO ECHO You are about to recreate %ComputerName%'s shares.
  23. >> "%~dp0%ComputerName%_recreate_shares.bat" ECHO IF /I NOT "%%ComputerName%%"=="%ComputerName%" ECHO This server is NOT the one that generated this batch file.
  24. >> "%~dp0%ComputerName%_recreate_shares.bat" ECHO PAUSE
  25. >> "%~dp0%ComputerName%_recreate_shares.bat" ECHO VER ^| FIND.EXE "Windows 2000" ^>NUL
  26. >> "%~dp0%ComputerName%_recreate_shares.bat" ECHO IF ERRORLEVEL 1 (SET Grant=/GRANT:Everyone,FULL) ELSE (SET Grant=)
  27. >> "%~dp0%ComputerName%_recreate_shares.bat" ECHO ECHO ON
  28.  
  29. >  "%~dp0%ComputerName%_delete_shares.bat" ECHO @ECHO OFF
  30. >> "%~dp0%ComputerName%_delete_shares.bat" ECHO CLS
  31. >> "%~dp0%ComputerName%_delete_shares.bat" ECHO ECHO.
  32. >> "%~dp0%ComputerName%_delete_shares.bat" ECHO ECHO You are about to delete all shares migrated from %ComputerName%.
  33. >> "%~dp0%ComputerName%_delete_shares.bat" ECHO IF /I NOT "%%ComputerName%%"=="%ComputerName%" ECHO This server is NOT the one that generated this batch file.
  34. >> "%~dp0%ComputerName%_delete_shares.bat" ECHO PAUSE
  35. >> "%~dp0%ComputerName%_delete_shares.bat" ECHO ECHO ON
  36.  
  37. :: Read the list of shares to be migrated and generate the required batch files
  38. FOR /F "skip=4 tokens=1" %%A IN ('TYPE "%~dp0%ComputerName%_netshares.txt" 2^>NUL ^| FINDSTR.EXE /R /B /I /V /C:"The command completed successfully\." ^| FINDSTR.EXE /R /B /V /C:" " ^| SORT') DO CALL :ReadShare %%A
  39.  
  40. :: Done
  41. TITLE Ready
  42. GOTO:EOF
  43.  
  44.  
  45. :ReadShare
  46. :: Display progress.
  47. TITLE %~1
  48. :: Retrieve the path associated with the share name and add the share to both batch files
  49. FOR /F "tokens=1*" %%a IN ('NET.EXE SHARE %1 2^>NUL ^| FINDSTR.EXE /R /B /I /C:"Path"') DO (
  50. 	>> "%~dp0%ComputerName%_recreate_shares.bat" ECHO NET.EXE SHARE %1="%%~b" %%Grant%%
  51. 	>> "%~dp0%ComputerName%_delete_shares.bat"   ECHO VER ^| NET.EXE SHARE %1 /DELETE
  52. )
  53. GOTO:EOF
  54.  
  55.  
  56. :Syntax
  57. ECHO.
  58. ECHO ShareMig.bat,  Version 1.00 for Windows 2000 / Windows Server 2003
  59. ECHO Prepare a migration of all shares from the current server to a new one.
  60. ECHO.
  61. ECHO Usage:  SHAREMIG.BAT
  62. ECHO.
  63. ECHO When run on a server, this batch file will generate 4 files:
  64. ECHO.
  65. ECHO   %%COMPUTERNAME%%_ORIGINALSHARES.TXT         text file listing ALL shares;
  66. ECHO   %%COMPUTERNAME%%_NETSHARES.TXT              list of shares to be migrated;
  67. ECHO   %%COMPUTERNAME%%_RECREATE_SHARES.BAT        use this batch file to recreate
  68. ECHO                                             the shares on the new server;
  69. ECHO   %%COMPUTERNAME%%_DELETE_SHARES.BAT          undo and test batch file to delete
  70. ECHO                                             the migrated shares again;
  71. ECHO.
  72. ECHO where %%COMPUTERNAME%% is the name of the server this batch file was executed on.
  73. ECHO Usually %%COMPUTERNAME%%_RECREATE_SHARES.BAT is the only file you'll need on the
  74. ECHO new server; %%COMPUTERNAME%%_DELETE_SHARES.BAT is for testing purposes only.
  75. ECHO If all else fails, use the generated text files as a reference for manual
  76. ECHO (re)creation of the shares.
  77. ECHO.
  78. ECHO Written by Rob van der Woude
  79. ECHO http://www.robvanderwoude.com
  80.  

page last modified: 2024-04-16; loaded in 0.0205 seconds