We help IT Professionals succeed at work.

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

bsharath
bsharath asked
on
529 Views
Last Modified: 2012-05-05
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
Comment
Watch Question

:: 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

Author

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.


Author

Commented:
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]

Author

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?
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.