Every server administrator has a set of utility programs they like to use. Typically, these tools are kept on each machine to ensure availability and can come from a variety of sources such as Sysinternals and Nirsoft. To keep these tools updated to the latest versions, manually updating can be quite tedious and time consuming, especially when there are multiple servers in the mix. So we have a solution for you: a simple to set up solution where you maintain a single set of tools and all your machines sync the versions of their tools to this location. For example, suppose you maintain systems on 3 separate networks and like to have the same set of 10 tools available on all these machines for scripts and maintenance. You would have a master folder of tools (available via a web site) where you maintain the release versions you want distributed to all your systems. All your other systems then “phone home” to this master folder and download the respective version locally. This process provides an automated solution to always making sure your utility versions are consistent across all your systems.
Setting up the Environment
The environment for this to work is pretty easy to set up and, once done, is truly a “set it and forget it” solution.
Alternate Uses
While we have covered a practical use a system admin may have for this type of script, there are many other situations where this script can come in handy:
The script supports both hardcoding a URL as well as supplying one from the command line (i.e. UpdateFromWeb http://source.site.com/Stuff.zip), so use your imagination.
The Script
@ECHO OFFTITLE Update From Web
ECHO Update From Web
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.
SETLOCAL EnableExtensions
REM Place this script in the folder which contains the utilities to update.
REM Requires WGet to be in the same folder as this script or in a location set in the PATH variable.
REM URL to look for updates on in the event none is specified as a parameter.
REM URL's of interest ---
REM Sysinternals : http://live.sysinternals.com/tools
REM NirSoft : http://www.nirsoft.net/panel
SET URL=http://my.site.com/ToolsFolder
REM If a parameter is specified, use that as the URL.
IF NOT {%1}=={} SET URL=%~1
REM Set this value to 1 to restart tasks that are stopped during the update process.
SET RestartKilledTasks=0
ECHO Detected directory: %~dp0
%~d0
CD %~p0
ECHO.
ECHO.
SET NewFile=.NEW
FOR /F %%A IN ('DIR /B') DO WGet --output-document="%%A%NewFile%" "%URL%/%%A"
REM Delete empty (not found) files.
FORFILES /P . /C "CMD /C IF @fsize==0 DEL /F /Q @path"
SET StartWhenFinished="%TEMP%StartWhenFinished.tmp.txt"
ECHO ;Terminated tools > %StartWhenFinished%
FOR /F %%A IN ('DIR /B') DO (
IF EXIST "%%A%NewFile%" (
ECHO Found new version of: %%A
REM If the file is running, kill it so it can be replaced.
FOR /F %%B IN ('TASKLIST') DO (
IF /I [%%A]==[%%B] (
ECHO %%B is currently running, killing process.
ECHO %%A >> %StartWhenFinished%
TASKKILL /IM "%%B" /T /F
)
)
REM Replace with the new version.
DEL /F /Q "%%A"
RENAME "%%A%NewFile%" "%%A"
)
)
IF {%RestartKilledTasks%}=={1} (
ECHO.
ECHO Resuming killed tasks
FOR /F "usebackq skip=1" %%A IN (%StartWhenFinished%) DO (
ECHO Starting %%A
START "Restarting %%A" "%%A"
)
)
IF EXIST "%StartWhenFinished%" DEL "%StartWhenFinished%"
ENDLOCAL