批处理日志循环回显

Batch log looping echo

我的批处理文件通过 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,没有其他数据。

您需要在 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