Auto shutdown WHS/Server 2003 if connection lost to router

Ian H 1 Reputation point
2021-09-26T13:02:14.157+00:00

Hi,

I'm still running media around my home with WHS(2003), it's fine for what we need and I don't see any reason to upgrade as its just for home (and I have spare Hardware). My UPS doesn't have have connectivity to the server, so I have no means of automatically shutting down the PC should there be a power failure. So I wrote a batch file to shutdown the server if connection was lost to the router (not on UPS) - by pinging router. all worked fine, until I had to reinstall after a Mobo failure. when I set the Batch File up again with the Scheduled task to run every 3 min it seems to run ok, except it doesn't shutdown the Server it just runs the command again? I have tried changing the command and it has the same result.

My Scheduled Task to run the command is set to repeat every 3 mins (i did have it to do it every 5 min), the batch file should shutdown the Server if it doesn't receive a reply from the router. the 2 scripts that I have tried are below:

Old Cmd - before re-install (used to work fine)
@Echo OFF
REM Ping switch/router
PING 192.168.0.1 | FINDSTR TTL
ECHO Errorlevel is %ERRORLEVEL%

REM Ping responds
IF %ERRORLEVEL% EQU 0 GOTO :ConnectedToLAN

REM Ping does not respond
IF %ERRORLEVEL% EQU 1 GOTO :NotConnectedToLAN

GOTO :eof

:ConnectedToLAN
ECHO Connected to LAN
GOTO :eof

:NotConnectedToLAN
ECHO Not connected to LAN
MKDIR %tmp%\TKH\Connected_to_Internet
REM VBScript to make popup dialog
(
ECHO.Option Explicit
ECHO.Dim oShell, retCode
ECHO.Set oShell = WScript.CreateObject^("WScript.Shell"^)
ECHO.retCode = oShell.Popup^("Shutdown computer now? If no answer is given within 90 sec computer will shutdown.", 90, "Shutdown?", 4 + 32^)
ECHO.Select Case retCode
ECHO. case 6, -1
ECHO. WScript.quit^(0^) 'Yes or time-out was chosen
ECHO. case 7
ECHO. WScript.quit^(1^) 'No was chosen
ECHO.End Select
)>"%tmp%\TKH\Connected_to_Internet\Shutdown.vbs"
CSCRIPT //nologo "%tmp%\TKH\Connected_to_Internet\Shutdown.vbs"
ECHO Errorlevel is %ERRORLEVEL%

REM Timeout or clicked Yes (Shutdown)  
IF %ERRORLEVEL% EQU 0 SHUTDOWN /P /F  

REM Clicked No (Do nothing)  
IF %ERRORLEVEL% EQU 1 REM  

REM Clean up
RD /Q /S "%tmp%\TKH\

New Cmd - tried because the cmd above didn't work
Ping 192.168.0.1
IF ERRORLEVEL 1 SHUTDOWN -s -f

My Server is a decrepit HP D530 CMT DG747A and any help would be most appreciated,

Best reagrds,

Ian

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,179 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 97,396 Reputation points MVP
    2021-09-26T15:15:54.733+00:00

    Hi anonymous user-9512 ,

    why not using PowerShell?

    if (!(Test-Connection <IP of the router> -Quiet)) {  
        Stop-Computer -Force  
    }  
    

    Works here without issues.

    With Start-Sleep you can add a delay before Stop-Computer if required.
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/start-sleep?view=powershell-7.1

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Limitless Technology 39,381 Reputation points
    2021-09-30T08:08:04.4+00:00

    Hello,

    Thank you for your question.

    In my opinion

    1. The server may be in Stopping services and already in shutdown state hence It will not take shutdown signal again.
    2. Please put some time gap before shutdown command of 5 minutes to 7 minutes as server may need some time to stop services and closing applications and logging out users.

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments