所以,我一直在搜索互联网,试图找到一种方法来检测远程桌面是否正在运行。我真的没找到我想要的东西,所以我开始自己做。我发现当我query user
时,我得到以下结果:
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
>michaelh rdp-tcp#0 1 Active none 9/29/2014 11:14 AM
所以我使用for loop
找到并拆分了我需要的行。这就是我想出的:
@ECHO off
setlocal EnableDelayedExpansion
for /f "tokens=2,4" %%a in ('QUERY USER ^| FINDSTR ">"') DO (
set "consoleTemp=%%a"
set "connectionState=%%b"
set "consoleType=!consoleTemp:~0,7!"
)
SET consoleTypeCorrect=
IF "!consoleType!"=="rdp-tcp" SET consoleTypeCorrect=1
SET connectionStateCorrect=
IF "connectionState"=="Active" SET connectionStateCorrect=1
set TRUE=
set TRUE=%connectionStateCorrect%%consoleTypeCorrect%
IF "%TRUE%"=="11" (
echo You are connected via !consoleType!
) else (
ECHO Sorry, the console type is !consoleType!
)
endlocal
PAUSE
这很有效,但我对你们的问题是,有更好的办法吗?如果你正在看这个并且不明白它是如何工作的,那么下午/给我发电子邮件,我会解释它。
答案 0 :(得分:0)
感谢@David Ruhmann的投入!如果没有更好的方法来检测RDP是否已连接,我会将此作为我的答案发布。
@ECHO off
setlocal EnableDelayedExpansion
for /f "tokens=2,4" %%a in ('QUERY USER ^| FINDSTR ">"') DO (
set "consoleTemp=%%a"
set "connectionState=%%b"
set "consoleType=!consoleTemp:~0,7!"
)
SET consoleTypeCorrect=
IF "!consoleType!"=="rdp-tcp" SET consoleTypeCorrect=1
SET connectionStateCorrect=
IF "connectionState"=="Active" SET connectionStateCorrect=1
set TRUE=
set TRUE=%connectionStateCorrect%%consoleTypeCorrect%
IF "%TRUE%"=="11" (
echo You are connected via !consoleType!
) else (
ECHO Sorry, the console type is !consoleType!
)
endlocal
PAUSE