如果没有局域网连接,如何关闭计算机

如果没有局域网连接,如何关闭计算机

如果我的路由器离线或与 LAN 断开连接,我希望关闭我的计算机。我需要一种方法来在启动 Windows 的同时开始监控状态,并不断检查路由器是否连接到 LAN。如果由于某种原因断开连接,我希望在 PC 即将关闭时收到 90 秒警告,以便我取消关机。有办法实现这一点吗?

答案1

复制粘贴此内容Notepad并保存为Shutdown.cmd

@ECHO OFF
REM Ping switch/router
    PING 192.168.1.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\

打开Task Scheduler(在开始菜单中搜索它)。

创建新任务并为其命名。

在 下TriggerAt log on选择Repeat task every:您想要的重复间隔(检查 LAN 连接的频率,)for a duration of: Indefinitely。在 下Action单击New...Start a programBrowse...并找到Shutdown.cmd您最近创建的文件。按两次确定以创建任务。找到您刚刚创建的任务,然后右键单击并选择Run。应出现如下对话框:

关机对话框

编辑Shutdown.cmd以满足您的需要(路由器地址等)。

VBS 脚本来自这里:关联

相关内容