批处理日志循环回显

时间:2018-06-20 19:22:45

标签: loops batch-file logging echo psexec

我的批处理文件停止并通过psexec启动服务,并将结果记录到txt文件中。批处理按预期方式运行,但是日志文件仅从psexec所引用的文件中写入第一个令牌。如何确保日志在循环中迭代?

@echo off
setlocal enabledelayedexpansion
for /f %%a in (IP_LIST.txt) do call :action %%a

:action
psexec \\@IP_LIST.txt -u admin -p password cmd /c "net stop spooler && net start spooler && echo [%date%, %time%] - Restarted service on %1 && echo ===========================================" >> LOG.txt

这是日志文件的样子:

The spooler service is stopping. The spooler service was stopped successfully.

The spooler service is starting. The spooler service was started successfully.

[Wed 06/20/2018, 12:24:30.61] - Restarted spooler service on
192.168.0.1

===========================================  The spooler service is stopping. The spooler service was stopped successfully.

The spooler service is starting. The spooler service was started successfully.

[Wed 06/20/2018, 12:24:30.61] - Restarted spooler service on
192.168.0.1 
===========================================

文件IP_LIST.txt每行包含1个IP,没有其他数据。

1 个答案:

答案 0 :(得分:0)

您需要在PSEXEC中使用命令行参数。

@echo off
for /f %%a in (IP_LIST.txt) do call :action %%a

GOTO :EOF
:action
psexec \\%1 -u admin -p password cmd /c "net stop spooler && net start spooler && echo [%date%, %time%] - Restarted service on %1 && echo ===========================================" >> LOG.txt