Archive

Posts Tagged ‘batch script remote desktop’

Script to Check or Set Remote Desktop Configuration

February 5, 2012 Leave a comment

This script can be used to audit and/or set the Remote Desktop configurationĀ on all Windows servers from Windows 2003 thru Windows 2008 R2 and all Windows clients from Windows XP thru WIndows 7. The steps are as follows:

  1. Check current Remote Desktop setting and set the %RDCFG% variable.
  2. Review the current Remote Desktop setting.
  3. Query to change Remote Desktop setting based on the current configuration.
  4. Change Remote Desktop setting or exit script based on user choice
  5. Review Remote Desktop setting again to validate configuration change.

You can copy and paste the code below or you can download the script here.


@echo off
TITLE Check/Set Remote Desktop Configuration
COLOR 17

:RDCHK
FOR /F “skip=1 tokens=1″ %%g IN (‘wmic rdtoggle get allowtsconnections ^|find ” “‘) DO (set RDCFG=%%g)

:RDRVW
cls
echo.
echo Current Remote Desktop configuration
echo ————————————

:RDRVW
echo.
IF /I “%RDCFG%”==”1” (echo Remote Desktop connections are enabled.) ELSE (IF /I “%RDCFG%”==”0” (echo Remote Desktop connections are disabled.))
goto RDQRY

:RDQRY
echo.
IF /I “%RDCFG%”==”0” (goto RDCFG0) ELSE (IF /I “%RDCFG%”==”1” (goto RDCFG1))

:RDCFG0
SET RDENB=
SET /P RDENB=Enable Remote Desktop connections? [y/n/q to quit] %=%
IF /I “%RDENB%”==”y” (goto RDENB) ELSE (IF /I “%RDENB%”==”n” (goto END) ELSE (IF /I “%RDENB%”==”q” (goto END)))
IF /I NOT “%RDENB%”==”y” (IF /I NOT “%RDENB%”==”n” (IF /I NOT “%RDENB%”==”q” goto RDQRY0))

:RDCFG1
SET RDDIS=
SET /P RDDIS=Disable Remote Desktop connections? [y/n/q to quit] %=%
IF /I “%RDDIS%”==”y” (goto RDDIS) ELSE (IF /I “%RDDIS%”==”n” (goto END) ELSE (IF /I “%RDDIS%”==”q” (goto END)))
IF /I NOT “%RDDIS%”==”y” (IF /I NOT “%RDDIS%”==”n” (IF /I NOT “%RDDIS%”==”q” goto RDQRY0))

:RDQRY0
echo.
echo You made an invalid entry – please try again.
echo.
pause
goto RDRVW

:RDENB
echo.
echo Enabling Remote Desktop…
ping localhost -n 3 >NUL
wmic rdtoggle WHERE “ServerName=’%computername%'” CALL SetAllowTSConnections 1 >NUL
goto RDCHK

:RDDIS
echo.
echo Disabling Remote Desktop…
ping localhost -n 3 >NUL
wmic rdtoggle WHERE “ServerName=’%computername%'” CALL SetAllowTSConnections 0 >NUL
goto RDCHK

:END
echo.
echo Exiting script…
echo.
ping localhost -n 5 >NUL
EXIT

 

Notes:

The WMIC call to set the TS connections requires the WHERE clause to work. Since this script uses the ServerName property, you may replace %computername% with the name of a remote server. As long as your user account has permission to access and modify the server via WMI remotely you will be able to check or set the Remote Desktop configuration of the remote server.

 

These scripts have been tested and are fully functional on Windows Server 2008, Windows Server 2008 R2 and Windows 2003 Server. They are provided with no guarantee and I assume no responsibility for the use or misuse of these scripts or any issues resulting from their use.