A CMD Script Reads from a File

article #200, updated 4961 days ago

Let’s say we want to run a series of commands on a set of machines. A good example is a VIPRE Enterprise client refresh, where we want to remove the agents and reinstall them, perhaps after a server crash. We can do it using this .CMD script:

set list=pclist.txt
for /F %%x in (%list%) do (

echo Removing old...
psexec \\%%x msiexec /x {9D544611-F437-4153-913E-91CE036583CC} /qn
psexec \\%%x msiexec /x {DB7CF8FB-8638-484E-A6C1-37F5AC21DCB2} /qn

Echo Installing new...
copy SBVEA_EN-Workstation.msi \\%%x\C$
psexec \\%%x msiexec /i C:\SBVEA_EN-Workstation.msi /qn
)

Just put the list of machines in “pclist.txt”, one per line, and put pclist.txt in the same folder as the script.  The two IDs were found using regedit (search for “sunbelt” or maybe “vipre”); they will change as versions change.  Any application installed via MSI can be removed like this, unless the vendor has really munged the MSI.

There is one exception to the above, gratefully found reported here:

http://blog.crankybit.com/why-that-batch-for-loop-isnt-working/

It occurs when there is a separate variable to be set inside the FOR loop.  In these cases, one must add the following line as the first line in the script:

<a href="http://www.ss64.com/nt/setlocal.html">setlocal</a> EnableDelayedExpansion

Categories: