EPM is often installed on several servers: web-, application- or database servers. and when a full DTAP cycle is used, the number of servers increases even more. To automate the distribution of files to multiple servers, I wrote the following script.
It has to run on the command line and will accept two parameters:
Path\Filename of file to copy
Path\Filename of file containing servernames to copy the file to
Path\Filename of file containing servernames to copy the file to
:: Name : Distribute.cmd
:: Purpose : Distributes files to other servers
:: Parameter: File to distribute
:: File with list of servernames to distribute to
:: Author : P. da Graça
:: Remark : Edited with Notepad++, layout maybe off in other editors
:: Run as (domain) user with sufficient access to the servers.
::
:: When Who What
:: 18-05-2011 PdaGraça First Setup
:: 19-05-2011 PdaGraça Skip copying to localhost
:: 23-05-2011 PdaGraça If 'file to distribute' begins with @, the filenames in that file are copied
:: 24-05-2011 PdaGraça If @distribute.txt is specified, all scriptfiles are copied to all servers
::
:: Purpose : Distributes files to other servers
:: Parameter: File to distribute
:: File with list of servernames to distribute to
:: Author : P. da Graça
:: Remark : Edited with Notepad++, layout maybe off in other editors
:: Run as (domain) user with sufficient access to the servers.
::
:: When Who What
:: 18-05-2011 PdaGraça First Setup
:: 19-05-2011 PdaGraça Skip copying to localhost
:: 23-05-2011 PdaGraça If 'file to distribute' begins with @, the filenames in that file are copied
:: 24-05-2011 PdaGraça If @distribute.txt is specified, all scriptfiles are copied to all servers
::
@Echo off
Setlocal
:: Set dynamic variables
For /f %%H in ('hostname') Do Set Hostname=%%H
For /f %%H in ('cd') Do Set ScrDir=%%H
:: Set general variables
Set Success=0
Set Error=0
Set ExitCode=0
Set Script=%~0
Set DistributionFile=Distribution.txt
Set AllServerFile=%ScrDir%\Servers-All.txt
Set ServerFile=%ScrDir%\Servers-DEV.txt
:: Main Routine
:: If no parameter specified, abort
If "%1" EQU "" Goto NoPar
:: If parameter starts with @, open the file and process its contents. Each line should list a filename to copy
:: Otherwise, copy the file itself. The ForPar variable is used to process the contents.
Set File=%1
If "%File:~0,1%" NEQ "@" Goto Verder
Set ForPar=/F
Set File=%File:~1,99%
:Verder
:: Check if file to copy exists
If Not Exist %File% Goto NoFile
:: Check if file with servernames has been specified, otherwise the default filename is used (see above).
If "%2" NEQ "" Set Serverfile=%~f2
:: If one specific @filename is given, use a specific corresponding file with the names of all servers as well. The @filename should
:: contain the names of this script and all supportfiles. This is used to distribute this tool across all servers.
If /I "%File%" EQU "%DistributionFile%" Set Serverfile=%AllServersFile%
:: Check if file with servernames exists
If Not Exist %ServerFile% Goto NoServerFile
:: Process the file itself, or the lines it contains.
For %ForPar% %%X In (%File%) Do Call :DoFile %%X
Set /A Total=%Success%+%Error%
Echo.
Echo Total filecopies : %Total%
Echo Succeeded filecopies: %Success%
Echo Failed filecopies : %Error%
:End
Goto :Eof
:: End of main routine
:DoFile
:: Add full drive and path to filename to copy
Set DoFile=%~f1
If Not Exist %DoFile% Goto NoFile
:: Process the file with servernames
For /f %%Y In (%ServerFile%) Do Call :CopyFile %%Y
Goto :Eof
:CopyFile
:: Check if the servername in the file is that of the current server. Skip the copy if it is.
for /f "tokens=1 delims=." %%I in ("%1") do Set ServerName=%%I
If "%ServerName%" EQU "%HostName%" Goto :Eof
:: The target filename is the source filename where Drive: is replaced by Drive$
Set ToFile=%DoFile::=$%
For /f %%I in ("%DoFile%") Do Set DoDir=%~dpI%
:: Check if target directory exists
For /f %%I in ("%ToFile%") Do Set ToDir=%~dpI%
:: Create target directory if it doesn't
If Not Exist %ToDir%\ Md %ToDir%
:: If target file exists, check if it's readonly
If Not Exist \\%1\\%ToFile% Goto Verder
Attrib \\%1\\%ToFile% | find " R " > Nul
If %Errorlevel% EQU 0 Set RO=ReadOnly
:Verder
:: Actual copying and errorhandling
If "%RO%" EQU "ReadOnly" Attrib -r \\%1\\%ToFile%
Copy %DoFile% \\%1\%ToFile% /V /Y >NUL 2>&1
Set El=%Errorlevel%
If %El% EQU 0 (Set ExitCode=0) & (Echo %DoFile% ^=^> \\%1\%ToFile% %RO%) & (Set /A Success=%Success%+1)
If %El% NEQ 0 (Set ExitCode=1) & (Echo %DoFile% ^!^> \\%1\%ToFile% %RO%) & (Set /A Error=%Error%+1)
If "%RO%" EQU "ReadOnly" Attrib +r \\%1\\%ToFile%
Set ReadOnly=
Goto :Eof
:NoPar
Echo.
Echo No file specified!
Call :Syntax
Set ExitCode=1
Goto End
:NoFile
Echo.
Echo File %File% does not exist!
Call :Syntax
Set ExitCode=1
Goto End
:NoDoFile
Echo.
Echo File %DoFile% does not exist!
Call :Syntax
Set ExitCode=1
Goto End
:NoServerFile
Echo.
Echo No serverfile specified!
Call :Syntax
Set ExitCode=1
Goto End
:Syntax
Echo.
Echo Syntax: %Script% filename(s) file_with_servernames
Echo or
Echo Syntax: %Script% @file_with_filenames file_with_servernames
Echo.
Echo Default file_with_servernames is %ServerFile%
Goto :Eof
No comments:
Post a Comment