Solved .BAT file 1 calls hostnames from another file. How do I log

May 29, 2012 at 11:33:13
Specs: Windows 7, 4GB
Hello all.

I am in need of assistance. I have a batch file that restarts machines on a nightly basis. However, since the Windows 7 "Shutdown" command doesn't log remarks unless there is an issue, I need some assistance. Here's my batch file:

@ECHO OFF

FOR /F "tokens=*" %%A IN ('TIME/T') DO SET Now=%%A

set _=c:\scripts\hosts.txt
for /f %%i in (
%_%
) do (
>>c:\scripts\output.txt 2>&1 Echo Restarting of machines initiated on %date% @ %now%
>>c:\scripts\output.txt 2>&1 SHUTDOWN /r /m \\%%i /t %1 /c "This machine is forcibly restarting in %1 seconds!" /f
>>c:\scripts\output.txt 2>&1 Echo .
)

The hosts file is about 290 machine names listed each on a separate line. If possible, I would like to get each machine name listed in the output.txt file as well, even if it's a simple line showing the machine name and that's it. That way, I can compare the running script with the machine listing I have, to make sure each and every machine is in there.

Is this something easy?


See More: .BAT file 1 calls hostnames from another file. How do I log

Report •

#1
June 16, 2012 at 16:58:56
✔ Best Answer
shouldn't this work?

@ECHO OFF

FOR /F "tokens=*" %%A IN ('TIME/T') DO SET Now=%%A

set _=c:\scripts\hosts.txt
>>c:\scripts\output.txt 2>&1 Echo Restarting of machines initiated on %date% @ %now%
for /f %%i in (
%_%
) do (
>>c:\scripts\output.txt 2>&1 SHUTDOWN /r /m \\%%i /t %1 /c "This machine is forcibly restarting in %1 seconds!" /f
>> c:\scripts\output.txt echo %%i
>>c:\scripts\output.txt 2>&1 Echo .
)
:end
note that it doesn't say if the machine actually shuts down, only that it was submitted to the shutdown command with your parameters. To prove that the remote actually shut down, you could set up a timed ping looking for "host
unreachable" or "request timed out", but of course this would make your script run-time very long.


Report •

#2
June 18, 2012 at 04:27:47
Not much different than nbrane's code:

@ECHO OFF
FOR /F "tokens=*" %%A IN ('TIME/T') DO SET Now=%%A
set _=c:\scripts\hosts.txt
>>c:\scripts\output.txt 2>&1 Echo Restarting of machines initiated on %date% @ %now%
for /f %%i in (%_%) do (
    >>c:\scripts\output.txt 2>&1 Echo Restarting of %%i initiated on %date% @ %now%
    >>c:\scripts\output.txt 2>&1 SHUTDOWN /r /m \\%%i /t %1 /c "This machine is forcibly restarting in %1 seconds!" /f
    >>c:\scripts\output.txt 2>&1 Echo(
)

When your only tool is a hammer, every problem looks like a nail.


Report •
Related Solutions


Ask Question