Home > Batch Scripting, Windows Server > Restarting a server at a certain time from accross the LAN.

Restarting a server at a certain time from accross the LAN.

Just a quick entry as i got an email requesting this today, “How can i schedule a restart on a remote server to run out of hours?”

This script uses at.exe which is soon replaced by schtasks.exe. This can only be run by an administrator of the server you wish to restart, obviously.

Rem setting vars:
set server=servertorestart
set time=05:00

REM performing check for previous file and deleting it:
if exist
\\%server%\c$\rebootme.bat del \\%server%\c$\rebootme.bat

Rem Creating reboot batch file:
echo shutdown -r -t 03 >>
\\%server%\c$\rebootme.bat

Rem Scheduling the job:
at
\\%server% %time% c:\rebootme.bat

To Create a reusable, error checking script with idiot proof options! save  the following as a .bat file:

@echo off
cls
Rem setting vars:
set /p server=[Please enter the name of the server you wish to restart:]
cls
echo Selected server = %server%
set /p time=[Please enter the time  in HH:MM format at which you wish to restart the server: e.g. 05:00]
cls

REM performing final idiot check
echo Are you absolutely sure you wish to restart %server% at %time%?
Pause

REM performing check for previous file and deleting it:
if exist
\\%server%\c$\rebootme.bat del \\%server%\c$\rebootme.bat & echo deleted previous file

Rem Creating reboot batch file:
echo shutdown -r -t 03 >>
\\%server%\c$\rebootme.bat

Rem Ensuring reboot file exists:
if exist
\\%server%\c$\rebootme.bat echo reboot script created successfully.
if not exist
\\%server%\c$\rebootme.bat goto Failed

Rem Scheduling the job:
at
\\%server% %time% c:\rebootme.bat
Echo Job scheduled, Script complete.
pause

:failed
echo something failed, I’d look into that if i were you.

pause
exit

  1. May 29, 2009 at 4:00 am

    I usually use psshutdown from the wonderful pstools for this.

    psshutdown \\servername -u domain\username -r -f -t 01:15 -e p:02:17

    This will restart the server at 01:15am, force any apps to close and enter a shutdown code 02:17 (planned patch reboot). It will prompt for password or you can add a -p in there too.

  2. Andrew Morgan
    June 2, 2009 at 7:41 am

    Nice touch! I wasnt aware of the time option for psshutdown, learn something new every day 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: