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:
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
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
@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!