• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 518
  • Last Modified:

Script to disconnect all idle Mstsc connections on all machines in the file

Hi,

I have a txt file that has 100 machines .I want a script that can check all machines in the file and disconnect any idle or not properl logged off machines .Need the script to log off only such sessions.

Regards
Sharath
0
bsharath
Asked:
bsharath
  • 5
  • 3
1 Solution
 
MSE-dwellsCommented:
:: tsDISCON.CMD - destroys disconnected sessions on Terminal Servers listed in supplied file
:: Dean Wells - MSEtechnology / October 2007

@echo off

setlocal ENABLEDELAYEDEXPANSION

echo/

:: Locate critical executables
for %%e in (query.exe logoff.exe ping.exe) do (
      set where="%%~$PATH:e"
      if "!where!"=="""" (
            echo #ERROR - Required executable, "%%e", not located within the path
            goto :END
      )
)

:: Verify arguments
if "%1"=="" goto :ERROR1
if not exist "%1" goto :ERROR1

:: Script body
echo + Processing servers in file '%*' -

:: Enumerate file
for /f "usebackq" %%s in ("%*") do (
      echo/
      echo   = SERVER: %%s
      ping -n 1 %%s >nul 2>&1
      if not errorlevel 1 (
            call :QUERY %%s
      ) else (
            echo     #unable to contact server
      )
)

echo/
echo - Done.

goto :END

:: Define functions/subs

:QUERY
set SERVER=%*
set allACTIVE=1
query user /server:%SERVER% >%TEMP%\%~n0.$$$
for /f "tokens=1,2,3,4 usebackq skip=1" %%s in ("%TEMP%\%~n0.$$$") do (
      if /i not "%%v"=="Active" (
            if /i "%%t"=="0" (
                  echo     - '%%s @ session %%t' is a console logon; skipping
            ) else (
                  echo     + destroying session: '%%s @ session %%t' -
                  logoff %%t /server:%SERVER% 1>nul 2>&1 && (
                        echo       - SUCCESS
                        set allACTIVE=0
                  ) || (
                        echo       - #FAILED
                  )
            )
      )
)
if "%allACTIVE%"=="1" (
      echo     - no sessions were affected
)
goto :EOF

:ERROR1
echo #ERROR - supply a filename containing a list of servers that
echo          you wish to destroy idle or disconnected sessions on.
goto :END

:END
0
 
MSE-dwellsCommented:
NOTE - don't name the script 'tsdiscon' since that binary already exists, I used 'tsDestroy.cmd' ...
0
 
MSE-dwellsCommented:
... a couple of tweaks for my own purposes that may also be useful -

[BEGIN FILE]

:: tsDestroy.CMD - destroys disconnected sessions on Terminal Servers in supplied file or a single
:: server supplied as argument 1

:: Dean Wells - MSEtechnology / October 2007

@echo off

setlocal ENABLEDELAYEDEXPANSION

echo/

:: Locate critical executables
for %%e in (query.exe logoff.exe ping.exe) do (
      set where="%%~$PATH:e"
      if "!where!"=="""" (
            echo #ERROR - Required executable, "%%e", not located within the path
            goto :END
      )
)

:: Verify arguments
if "%1"=="" goto :ERROR1
if not exist "%*" (
      echo %*>%TEMP%\%~n0.lst
      set FILE=%TEMP%\%~n0.lst
) else (
      set FILE=%*
)

:: Script body
echo + Processing server[s] -

:: Enumerate file
for /f "usebackq" %%s in ("%FILE%") do (
      echo/
      echo   = SERVER: %%s
      ping -n 1 %%s >nul 2>&1
      if not errorlevel 1 (
            call :QUERY %%s
      ) else (
            echo     #unable to contact server
      )
)

echo/
echo - Done.

goto :END

:: Define functions/subs

:QUERY
set SERVER=%*
set allACTIVE=1
query user /server:%SERVER% >%TEMP%\%~n0.$$$
for /f "tokens=1,2,3,4 usebackq skip=1" %%s in ("%TEMP%\%~n0.$$$") do (
      if /i not "%%v"=="Active" (
            if /i "%%t"=="0" (
                  echo     - '%%s @ session %%t' is a console logon; skipping
            ) else (
                  echo     + destroying session: '%%s @ session %%t' -
                  logoff %%t /server:%SERVER% 1>nul 2>&1 && (
                        echo       - SUCCESS
                        set allACTIVE=0
                  ) || (
                        echo       - #FAILED
                  )
            )
      )
)
if "%allACTIVE%"=="1" (
      echo     - no sessions were affected
)
goto :EOF

:ERROR1
echo #ERROR - supply a server or a filename containing a list of servers
echo          that you wish to destroy idle or disconnected sessions on
goto :END

:END
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
bsharathAuthor Commented:
Hi,

Is this the right way i need to run the script.


C:\>"Disconnect all Ts sessions.cmd" computers.txt

+ Processing server[s] -

  = SERVER: dev-chen-mrd100
Invalid parameter(s)
Display information about users logged on to the system.

QUERY USER [username | sessionname | sessionid] [/SERVER:servername]

  username            Identifies the username.
  sessionname         Identifies the session named sessionname.
  sessionid           Identifies the session with ID sessionid.
  /SERVER:servername  The server to be queried (default is current).

The system cannot find the file C:\DOCUME~1\ADMINI~1.DEV\LOCALS~1\Temp\Disconnec
t all Ts sessions.$$$.
    - no sessions were affected

- Done.


0
 
bsharathAuthor Commented:
I have a disconnected seesion in the machine
0
 
MSE-dwellsCommented:
My first suggestion would be that you follow my earlier advice and name the script something more inline with command line convention, i.e. something like tsDestroy.cmd.

That said though, I confess the script should have dealt better with extremely long filenames ... this version does -

[BEGIN FILE]
:: tsDestroy.CMD - destroys disconnected sessions on Terminal Servers in supplied file or a single
:: server supplied as argument 1

:: Dean Wells - MSEtechnology / October 2007

@echo off

setlocal ENABLEDELAYEDEXPANSION
set ARGS=%~1 & set tARGS=!ARGS:?=!

echo/

:: Locate critical executables
for %%e in (query.exe logoff.exe ping.exe) do (
      set where="%%~$PATH:e"
      if "!where!"=="""" (
            echo #ERROR - Required executable, "%%e", not located within the path
            goto :END
      )
)

:: Verify arguments
if "%ARGS%"=="" goto :ERROR1
if not "%tARGS%"=="%ARGS%" goto :ERROR1

if not exist "%ARGS%" (
      echo %ARGS%>%TEMP%\%~ns0.lst
      set FILE=%TEMP%\%~ns0.lst
) else (
      set FILE=%ARGS%
)

:: Script body
echo + Processing server[s] -

:: Enumerate file
for /f "usebackq" %%s in ("%FILE%") do (
      echo/
      echo   = SERVER: %%s
      ping -n 1 %%s >nul 2>&1
      if not errorlevel 1 (
            call :QUERY %%s
      ) else (
            echo     #unable to contact server
      )
)

echo/
echo - Done.

goto :END

:: Define functions/subs

:QUERY
set SERVER=%*
set allACTIVE=1
query user /server:%SERVER% >%TEMP%\%~ns0.$$$
for /f "tokens=1,2,3,4 usebackq skip=1" %%s in ("%TEMP%\%~ns0.$$$") do (
      if /i not "%%v"=="Active" (
            if /i "%%t"=="0" (
                  echo     - '%%s @ session %%t' is a console logon; skipping
            ) else (
                  echo     + destroying session: '%%s @ session %%t' -
                  logoff %%t /server:%SERVER% 1>nul 2>&1 && (
                        echo       - SUCCESS
                        set allACTIVE=0
                  ) || (
                        echo       - #FAILED
                  )
            )
      )
)
if "%allACTIVE%"=="1" (
      echo     - no sessions were affected
)
goto :EOF

:ERROR1
echo #HELP - supply a server name, address or a filename containing a list of
echo         servers that you wish to destroy idle or disconnected sessions on
goto :END

:END

[/END FILE]
0
 
bsharathAuthor Commented:
Hi Sorry for the delay...

I get this...

C:\>tsDestroy.CMD

+ Processing server[s] -

  = SERVER: ECHO
    #unable to contact server

- Done.
The system cannot find the path specified.



From which file will the computer names be taken?
0
 
MSE-dwellsCommented:
Why are you not supplying an argument anymore?  The script accepts a computer name, an IP address or a file containing a list of servers.  Since I'll use this myself, I've added a little more error handling of my own and you're welcome to try this version with the extras -

[BEGIN FILE]
:: tsDestroy.CMD - destroys disconnected sessions on Terminal Servers in supplied file or a single
:: server supplied as argument 1

:: Dean Wells - MSEtechnology / October 2007 (dwells@msetechnology.com)

@echo off

setlocal ENABLEDELAYEDEXPANSION
set ARGS=%~1
set tARGS=%ARGS:?=%

echo/

:: Locate critical executables
for %%e in (query.exe logoff.exe ping.exe) do (
      set where="%%~$PATH:e"
      if "!where!"=="""" (
            echo #ERROR - Required executable, "%%e", not located within the path
            goto :END
      )
)

:: Verify arguments
if "%ARGS%"=="" goto :ERROR1
if not "%tARGS%"=="%ARGS%" goto :ERROR1

if not exist "%ARGS%" (
      echo %ARGS%>%TEMP%\%~ns0.lst
      set FILE=%TEMP%\%~ns0.lst
) else (
      set FILE=%ARGS%
)

:: Script body
echo + Processing server[s] -

:: Enumerate file
for /f "usebackq" %%s in ("%FILE%") do (
      echo/
      echo   = SERVER: %%s
      ping -n 1 %%s >nul 2>&1
      if not errorlevel 1 (
            call :QUERY %%s
      ) else (
            echo     #unable to contact server
      )
)

echo/
echo - Done.

goto :END

:: Define functions/subs

:QUERY
set SERVER=%*
set allACTIVE=1
query user /server:%SERVER% >%TEMP%\%~ns0.$$$
for /f "tokens=1,2,3,4 usebackq skip=1" %%s in ("%TEMP%\%~ns0.$$$") do (
      if /i not "%%v"=="Active" (
            if /i "%%t"=="0" (
                  echo     - '%%s @ session %%t' is a console logon; skipping
            ) else (
                  echo     + destroying session: '%%s @ session %%t' -
                  logoff %%t /server:%SERVER% 1>nul 2>&1 && (
                        echo       - SUCCESS
                        set allACTIVE=0
                  ) || (
                        echo       - #FAILED
                  )
            )
      )
)
if "%allACTIVE%"=="1" (
      echo     - no sessions were affected
)
goto :EOF

:ERROR1
echo #HELP - supply a server name, address or a filename containing a list of
echo         servers that you wish to destroy idle or disconnected sessions on
goto :END

:END
[END FILE]
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now