Skip to content

Instantly share code, notes, and snippets.

@davidruhmann
Created October 3, 2014 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidruhmann/b8dd00f8877489771eb5 to your computer and use it in GitHub Desktop.
Save davidruhmann/b8dd00f8877489771eb5 to your computer and use it in GitHub Desktop.
SO26181747.bat
@echo off
:: usage: script.bat [username=%username%]
call :AnyActiveRemoteSessions %~1 %username% && echo Yes || echo No
exit /b 0
:AnyActiveRemoteSessions [Username]
for /f "skip=1 tokens=2,4" %%A in ('query user %~1 ^| find ">"') do call :IsActiveRemoteSession "%%~A" "%%~B" && exit /b 0
exit /b 1
:IsActiveRemoteSession <Session> <State>
if /i "%~2"=="active" echo "%~1" | find /i "rdp-tcp" && exit /b 0
exit /b 1
:: OP's structure
for /f "skip=1 tokens=2,4" %%A in ('query user %username%') do (
if /i "%%~B"=="active" echo "%%~A" | find /i "rdp-tcp" >nul && echo RDP: Yes || echo RDP: No
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment