Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

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
Avatar of MSE-dwells
MSE-dwells
Flag of Yemen image

:: 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
NOTE - don't name the script 'tsdiscon' since that binary already exists, I used 'tsDestroy.cmd' ...
... 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
Avatar of bsharath

ASKER

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.


I have a disconnected seesion in the machine
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]
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?
ASKER CERTIFIED SOLUTION
Avatar of MSE-dwells
MSE-dwells
Flag of Yemen image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial