我正在制作一个简单的批处理脚本来ping通主机并检查其连接。这是我的代码:
@ECHO OFF
@powershell -ExecutionPolicy UnRestricted -Command "(Add-Type -memberDefinition \"[DllImport(\"\"user32.dll\"\")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x,int y,int cx, int xy, uint flagsw);\" -name \"Win32SetWindowPos\" -passThru )::SetWindowPos((Add-Type -memberDefinition \"[DllImport(\"\"Kernel32.dll\"\")] public static extern IntPtr GetConsoleWindow();\" -name \"Win32GetConsoleWindow\" -passThru )::GetConsoleWindow(),-1,0,0,0,0,67)"
title Ping tool
mode con: cols=50 lines=25
:PING
cls & set /p address= [*] Host to ping:
@echo [*] Started pinging at: %time%
title Pinging %address%
if %ERRORLEVEL% == 1 echo Host didn't respond.
ping %address% -t & echo. & pause. & goto :PING
我正在努力做到这一点,以便当我收到错误代码(请求超时)时,它会显示“主机未响应”,而不是通常的打印。但是它不起作用。
我将如何继续这样做?
P.S脚本的第二行是我找到的命令here,这使CMD窗口始终位于顶部。非常有用!
您可以将输出重定向到文件,测试错误级别,如果遇到错误级别检查,则输入输出。
@Echo off
:ping
cls & set /p address= [*] Host to ping:
ping %address%>output.txt
If Errorlevel 1 (Echo(Host unavailable) Else (TYPE output.txt)
pause>nul
Goto :ping