Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: batch file to restart all hosts included in text file with a random timeout  (Read 7629 times)

0 Members and 1 Guest are viewing this topic.

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Hi all
I want to restart a package of machines located in text file Through the below steps:
- Firstly ping for each one of them .
-IF the hosname is available
       - Create a random number between 0 and 15 seconds and store it in a variable.
       - Execute the remote shutdown command usinge a random timeout stored in the previous variable.
       - Store the result in an output file "Result.txt" in the same folder.
-IF the hostname is not available
        - Also store the result in an output file "Result.txt" in the same folder.

here is my batch file content
@echo off
for /f %%H in (hosts.txt) do (
   echo %%H
   ping -n 1 -l 32 -w 2000 -i 32 %%H >nul
   echo %errorlevel%
   pause
   IF %ERRORLEVEL%==0  (
      set /a "rand=(%RANDOM%*15/32768)+1"
      echo %rand%
      shutdown -r -t %rand% -c "Made by Eng Amin" -f -m \\%%H
      echo.[%%H is restarted]>>Result.txt
   )
   IF %ERRORLEVEL%==1  echo.[%%H is --- Offline]>>Result.txt
)


and this is the content of the hosts.txt and located in the same directory
C3I3C331
C3I3C332
C3I3C333
C3I3C334

by the way I'm still beginner
Can any one advice , correct my code and till me where is the bug

Abo-Zead

    Topic Starter


    Beginner
  • Thanked: 1
  • Experience: Familiar
  • OS: Windows 10
Hi All

Kindly don't ignore my question and Can anyone help me, please?

I want to restart any pc over my network using the shutdown command with a random timeout between 1 to 15 seconds.

Ex.
shutdown -r -f -t %random time from 1 to 15 sec% %hostname Or Ip%
as it didn't work with me ever
My point here is to give any pc a random timeout for shutdown command as all PCs included in a text file and I will use for loop to pass through them
and if you know a better solution kindly help