简体   繁体   English

使用读取文本文件的for循环替换多行批处理文件

[英]Replacing multiline batch file with a for loop which reads a text file

I currently have a batch file that schedules chkdsk to run on multiple servers pending the next reboot- 我目前有一个批处理文件,用于安排chkdsk在多个服务器上运行,等待下次重启 -

c:\pstools\psexec \\server1 -d -u Domain\administrator cmd /c "echo Y | chkdsk c: /F"  
c:\pstools\psexec \\server2 -d -u Domain\administrator cmd /c "echo Y | chkdsk c: /F"  
c:\pstools\psexec \\server3 -d -u Domain\administrator cmd /c "echo Y | chkdsk c: /F"  
c:\pstools\psexec \\server4 -d -u Domain\administrator cmd /c "echo Y | chkdsk c: /F"  
c:\pstools\psexec \\server5 -d -u Domain\administrator cmd /c "echo Y | chkdsk c: /F"  

I am trying to understand how to construct a FOR loop which will read from a txt file composed of - 我试图了解如何构建一个FOR循环,它将从一个由txt组成的文件中读取 -

server1  
server2  
server3  
server4  

The txt file will be located in the data folder on my local e: drive. txt文件将位于我本地e:驱动器上的数据文件夹中。 Also, is there any way to not have to enter the password for the user ID more than once? 另外,有没有办法不必多次输入用户ID的密码? Appreciate the guidance! 感谢指导! Thx and a Happy New Year to all! 感谢所有人新年快乐!

You could use a FOR, but you could also create a file named servers.txt that contains: 您可以使用FOR,但您也可以创建一个名为servers.txt的文件,其中包含:

server1
server2
server3
server4

and run this command: 并运行此命令:

c:\pstools\psexec @servers.txt ...your command...

PsExec will execute the command on each of the computers listed in the file. PsExec将在文件中列出的每台计算机上执行该命令。 Please see the psexec documentation here , and see the @file parameter. 请参阅此处psexec文档 ,并查看@file参数。

Here's a for loop method: 这是一个for循环方法:

@echo off
for /f "delims=" %%a in (servers.txt) do (
   c:\pstools\psexec \\%%a -d -u Domain\administrator cmd /c "echo Y | chkdsk c: /F"  
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM