question

IanH-9512 avatar image
0 Votes"
IanH-9512 asked LimitlessTechnology-2700 answered

Auto shutdown WHS/Server 2003 if connection lost to router

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten edited

Hi @IanH-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://docs.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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

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--

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.