2

On multiple (about 20) servers, there exists the same directory C:\Deployments. This directory has multiple subfolders which contain more subfolders and files. The C:\Deployments directory itself does not contain files.

I have a batch file that, when run locally, cleans out the C:\Deployments directory. That is, it deletes all subfolders and their files, but doesn't delete the C:\Deployements directory itself.

The batch file code is:

echo off

set CAT=c:\deployments

dir "%%CAT%%"/s/b/a | sort /r >> %TEMP%\files2del.txt 
for /f "delims=;" %%D in (%TEMP%\files2del.txt) do (del /q "%%D" & rd "%%D") 
del /q %TEMP%\files2del.txt

My question is this:

How do I run this batch file on each server from a localized window? That is, how do I run one batch file and have these commands execute on every server simultaneously?

Thanks for any help/ideas!

3
  • 2
    Seems like a question for superuser.com
    – Uwe Keim
    Nov 10, 2011 at 18:41
  • 1
    I'm not familiar with these sites. Is superuser.com a better place to ask about batch files?
    – user1040362
    Nov 10, 2011 at 18:51
  • 2
    Question about writing batch files: stackoverflow. Question about deploying batch files to 20 computers: serverfault (if domain) or superuser (if non-domain).
    – Raymond Chen
    Nov 10, 2011 at 19:36

3 Answers 3

3

I don't have a Windows machine anymore, but used to do this stuff at work. There is psexec which allows you to remote to another server. You can also look into powershell remoting.

Powershell Remoting see http://msdn.microsoft.com/en-us/library/windows/desktop/ee706585(v=vs.85).aspx

Using Dos / Batch, see http://ss64.com/nt/psexec.html

If you want them to run simultaneously, you can do this, by running jobs in powershell, see the same ss64 site.

If you are interested...

If your servers are listed in active directory, you can do a little trick where you say loop through computers using Active Directory

dsquery computer -limit 0 | ?{$_ -imatch "CN=([^,]+),"} | % {
  #remotely run your script here through ps remote
}

or replace dsquery... with gc $your_file_name containing list of files. Dos/Batch - my syntax may be off, but this is a nice way to loop over computers

for /f "eol=, tokens=1, delims=" %cc in ('dsquery computer -name -limit 0 ^| find /i /r /c:'"CN=([^,]+),"') do (

  @REM cannot recall how i used to get back references in dos like this, so may be off.
  @REM call your script here, passing your batch program to psexec
  set computer=%%c
  echo computer=!computer!
)

or replace dsquery... with type "%your file name%"

Also, if you ever run into a problem setting variables within a loop in batch where they do not persist as you would expect, place setlocal enabledelayedexpansion at the top of your program, and evaluate your variables like this !var_name! instead of %var_name%

0

I would like to suggest the WinRM/WinRS (Windows Remote Management/Windows Remote Shell) combination which requires a little bit of setup beforehand, but uses tools already built into Windows.

Once you have the setup complete, your script will need to change to include the winrs command to connect. You could create a list of servers in another text file and wrap another FOR loop around your current script to avoid having 20 lines that all do the same thing.

0

Frankly kikuchiyo's answer is awesome and I have voted as much. However for completeness sake I will suggest that you could map to each server and delete the files via a remote share.

I am assuming a domain administrator here, or at least an account that has admin on each of the machines.

But basically I would do womething like this (keeping your code to do the work, not how I woud have perosnally done it but that doesn't mean I'm right)

servers.txt is just a list of the server hostname/IP address

for /f %%a in (servers.txt) do :process %%a

:process

set SERVER=%1

IF [%SERVER%]==[] GOTO EOF

NET USE T: \%SERVER%\C$\Deployments

set CAT=T:

dir "%%CAT%%"/s/b/a | sort /r >> %TEMP%\files2del.txt

for /f "delims=;" %%D in (%TEMP%\files2del.txt) do (del /q "%%D" & rd "%%D")

del /q %TEMP%\files2del.txt

:EOF

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .