• Hey All,

    Here's are some scripts that I've used a couple of times in our environment. They work well if a script or many scripts have to be run against a bunch of servers. I've added some of the sqlcmd parameters to allow for proper formatting in the log files.

    First batch file:

    Example:

    Assume that the file is called "runall.bat" and it's placed in c:\utils and the command is executed as follows: runall.bat my_server_list.txt

    The script will assume that all of the files placed in c:\utils\sql_scripts are scripts and will run them against all of the servers listed in my_server_list.txt.

    Code for runall.bat

    ___________________________________________________

    dir /b sql_scripts > blahtemp12345.txt

    for /F "eol=; tokens=1" %%h in (blahtemp12345.txt) do for /F "eol=; tokens=1" %%i in (%1) do sqlcmd -s "|" -S %%i -E -W -h -1 -i .\sql_scripts\%%h > .\logs\%%i-%%h.log

    del blahtemp12345.txt

    ____________________________________________________

    Sample my_server_list.txt

    ______________________________

    ServerA

    ServerB

    ServerC

    ...

    ServerN

    ______________________________

    Second batch file code (used for one-off script execution)

    Assume the file is called runonce.bat and is placed in c:\utils and the command is invoked as follows:

    runonce.bat my_server_list.txt my_sql_script.sql

    Runonce will iterate through all of the servers in my_server_list.txt and execute the sql script my_sql_script.sql against them. All logs will be outputted to .\logs from the perspect

    runonce.bat code

    __________________________________________________

    @@echo off

    @rem Run a specified sql script file against a list of PCs

    @rem %1 - list of servers

    @rem %2 - sql script

    for /F "eol=; tokens=1" %%i in (%1) do sqlcmd -s "|" -S %%i -E -i %2 > logs\%%i-%2.log

    ___________________________________________________

    Feedback is always appreciated - thanks!

    ---Fidel