@Echo off
rem Script for Control Connetion via RemoteDesktop
rem #New:31.10.2012 J.Fes
rem #LUp:22.12.2014 J.Fes Update after reorg 01-BinServer.Custom
rem #LUp:18.02.2024 J.Fes add :PingOpt
setlocal

set Client_ComputerName=#JobComputers_tbl.ComputerName#
set Client_ComputerFQDN=#JobComputers_tbl.ComputerFQDN#
set OsUser=#JobComputers_tbl.OsUser#
if "%Client_ComputerFQDN%" equ "" set Client_ComputerFQDN=%Client_ComputerName%

REM set MyAppData_Root_DP=%AppData%\JobManager
set MyAppData_Root_DP=%~dp0..\..\..\..\01-BinServer.Custom\Computers\%ComputerName%\Remote\%UserName%
set MyAppData_Setting_DPNE=%MyAppData_Root_DP%\%Client_ComputerName%_LastUsedRDP.cmd
set Rdp_DPNE=%MyAppData_Root_DP%\%Client_ComputerName%.rdp

if not exist "%MyAppData_Root_DP%" md  "%MyAppData_Root_DP%"
set doNext=

set 20-JobComputerScripts_DP=#20-JobComputerScripts_DP#
set 01-BinServer_DP=#01-BinServer_DP#
set 02-BinClient_DP=#02-BinClient_DP#
set 03-BinPublic_DP=#03-BinPublic_DP#
set 04-Templates_DP=#04-Templates_DP#
set 10-Doku_DP=#10-Doku_DP#
set 90-DATA_DP=#90-DATA_DP#
set S_Root_DP=#00-ApplRoot_DP#

rem Titelzeile schreiben
title RDP connect via:[\\%Rdp_DPNE%]

:Menue
 color E0

  if /I "%Client_ComputerName%" neq "%ComputerName%" goto :jumbOver
    Echo -----------------------------------------------------------------------
    color C0
    Echo Info: You try to create a self referencing remote connection
    Echo on the computer you are currently working with [\\%Client_ComputerName%]
    Echo.
    Echo after pressing return key process ends
    pause
    Exit /b
    Echo -----------------------------------------------------------------------
 :jumbOver


 set S1_W=1024& set S1_H=768
 set M1_W=1200& set M1_H=840
 set L1_W=1767& set L1_H=1100
 set L2_W=1680& set L2_H=1050
 set L3_W=1920& set L3_H=1200
 set R_SPAN=/span
 set R_Full=/f

 rem set Defaults
 if exist "%MyAppData_Setting_DPNE%" call "%MyAppData_Setting_DPNE%"
 if not defined doNext set doNext=M
 if not defined res    set R_W=%M1_W%  & set R_H=%M1_H%

 Echo.
 Echo --------------------------------------------------------------------------
 Echo Connect Remote via RDP to:
 Echo.
 Echo ComputerName.....: %Client_ComputerName%
 Echo ComputerName.FQDN: %Client_ComputerFQDN%
 Echo.
 Echo  P  = Ping: %Client_ComputerName%
 Echo  P2 = Ping: %Client_ComputerName% (Continues pinging -t)
 Echo  FL = Flush DNS via Iponfig
 Echo.
 Echo  S  = W:[%S1_W%]   H:[%S1_H%] small
 Echo  M  = W:[%M1_W%]   H:[%M1_H%] middel
 Echo  L1 = W:[%L1_W%]   H:[%L1_H%] large 1
 Echo  L2 = W:[%L2_W%]   H:[%L2_H%] large 2
 Echo  L3 = W:[%L3_W%]   H:[%L3_H%] large 3
 Echo.
 Echo  SP = W:[%R_SPAN%] use win span mode
 Echo  F  = W:[%R_Full%] use win full mode
 Echo  E  = use Rdp File /Edit
 Echo       %Rdp_DPNE%
 Echo.
 Echo  Q  = Quit (end this menue)
 Echo.
 Echo please select the resolution you would like to use:
 set /P doNext=or function Default:[%doNext%] :

 if /I "%doNext%" neq "" if /I "%doNext%" neq "?" if /I "%doNext%" neq "Q" if /I "%doNext%" neq "FL" if /I "%doNext%" neq "P"    Echo set doNext=%doNext%>"%MyAppData_Setting_DPNE%"
 Color A0
 if /I "%doNext%"=="Q"    Exit
 if /I "%doNext%"=="S"    call :RemoteDesktop "%S1_W%" "%S1_H%"
 if /I "%doNext%"=="M"    call :RemoteDesktop "%M1_W%" "%M1_H%"
 if /I "%doNext%"=="L1"   call :RemoteDesktop "%L1_W%" "%L1_H%"
 if /I "%doNext%"=="L2"   call :RemoteDesktop "%L2_W%" "%L2_H%"
 if /I "%doNext%"=="L3"   call :RemoteDesktop "%L3_W%" "%L3_H%"
 if /I "%doNext%"==""     call :RemoteDesktop "%L1_W%" "%L1_H%"
 if /I "%doNext%"=="SP"   call :RemoteDesktop /span
 if /I "%doNext%"=="F"    call :RemoteDesktop /f
 if /I "%doNext%"=="E"    call :RemoteDesktop /edit
 if /I "%doNext%"=="EE"   start Explorer /e, "%MyAppData_Root_DP%"
 if /I "%doNext%"=="?"    start "RDP" mstsc.exe /?
 if /I "%doNext%"=="P"    Call :Ping
 if /I "%doNext%"=="P1"   Call :Ping
 if /I "%doNext%"=="P2"   Call :Ping "-t"
 if /I "%doNext%"=="FL"   Call :IpConfig "/displaydns"& Call :IpConfig "/FlushDns"& Call :IpConfig "/displaydns"
 goto :Menue


rem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
:RDP_FileGen
 Echo gen RDP: "%Rdp_DPNE%"

 set /a winpos_x2=%R_W% + 50
 set /a winpos_y2=%R_H% + 50
 Echo screen mode id:i:1>>"%Rdp_DPNE%"
 Echo desktopwidth:i:%R_W%>>"%Rdp_DPNE%"
 Echo desktopheight:i:%R_H%>>"%Rdp_DPNE%"
 Echo session bpp:i:15>>"%Rdp_DPNE%"
 Echo winposstr:s:0,1,0,0,%winpos_x2%,%winpos_y2% >>"%Rdp_DPNE%"
 Echo full address:s:%Client_ComputerFQDN%>>"%Rdp_DPNE%"
 Echo compression:i:1>>"%Rdp_DPNE%"
 Echo keyboardhook:i:2>>"%Rdp_DPNE%"
 Echo audiomode:i:0>>"%Rdp_DPNE%"
 Echo redirectdrives:i:1 >>"%Rdp_DPNE%"
 Echo redirectprinters:i:0>>"%Rdp_DPNE%"
 Echo redirectcomports:i:0>>"%Rdp_DPNE%"
 Echo redirectsmartcards:i:0>>"%Rdp_DPNE%"
 Echo displayconnectionbar:i:1>>"%Rdp_DPNE%"
 Echo autoreconnection enabled:i:1>>"%Rdp_DPNE%"
 Echo username:s:%OsUser%>>"%Rdp_DPNE%"
REM  Echo domain:s: >>"%Rdp_DPNE%"
 Echo alternate shell:s:>>"%Rdp_DPNE%"
 Echo shell working directory:s:>>"%Rdp_DPNE%"
 Echo disable wallpaper:i:1>>"%Rdp_DPNE%"
 Echo disable full window drag:i:1>>"%Rdp_DPNE%"
 Echo disable menu anims:i:1>>"%Rdp_DPNE%"
 Echo disable themes:i:1>>"%Rdp_DPNE%"
 Echo disable cursor setting:i:0>>"%Rdp_DPNE%"
 Echo bitmapcachepersistenable:i:1>>"%Rdp_DPNE%"
 Echo use multimon:i:0>>"%Rdp_DPNE%"
 Echo audiocapturemode:i:0>>"%Rdp_DPNE%"
 Echo videoplaybackmode:i:1>>"%Rdp_DPNE%"
 Echo connection type:i:1>>"%Rdp_DPNE%"
 Echo allow font smoothing:i:0>>"%Rdp_DPNE%"
 Echo allow desktop composition:i:0>>"%Rdp_DPNE%"
 Echo redirectclipboard:i:1>>"%Rdp_DPNE%"
 Echo redirectposdevices:i:0>>"%Rdp_DPNE%"
 Echo redirectdirectx:i:1>>"%Rdp_DPNE%"
 Echo drivestoredirect:s:*>>"%Rdp_DPNE%"
 Echo authentication level:i:2>>"%Rdp_DPNE%"
 Echo prompt for credentials:i:0>>"%Rdp_DPNE%"
 Echo negotiate security layer:i:1>>"%Rdp_DPNE%"
 Echo remoteapplicationmode:i:0>>"%Rdp_DPNE%"
 Echo gatewayhostname:s:>>"%Rdp_DPNE%"
 Echo gatewayusagemethod:i:4>>"%Rdp_DPNE%"
 Echo gatewaycredentialssource:i:4>>"%Rdp_DPNE%"
 Echo gatewayprofileusagemethod:i:0>>"%Rdp_DPNE%"
 Echo promptcredentialonce:i:1>>"%Rdp_DPNE%"
 Echo use redirection server name:i:0>>"%Rdp_DPNE%"
 goto :eof

rem -- Start RDP via mstsc.exe -------------------------------------------------
:RemoteDesktop

 set toRun=
 if /i "%1" neq "/f" goto :JumpOver
  if not exist "%Rdp_DPNE%" call :RDP_FileGen
  set toRun=start "RDP" mstsc.exe "%Rdp_DPNE%" /f
  Echo %toRun%
  %toRun%
  exit
 :JumpOver

 if /i "%1" neq "/span" goto :JumpOver
  if not exist "%Rdp_DPNE%" call :RDP_FileGen
  set toRun=start "RDP" mstsc.exe "%Rdp_DPNE%" /span
  Echo %toRun%
  %toRun%
  exit
 :JumpOver

 if /i "%1" neq  "/edit" goto :JumpOver
    if not exist "%Rdp_DPNE%" call :RDP_FileGen
    set toRun=start "RDP" mstsc.exe /edit "%Rdp_DPNE%"
    %toRun%
    goto :eof
 :JumpOver

 set R_W=%~1
 set R_H=%~2
 if "%R_W%" equ "" if "%R_H%" equ "" goto :JumpOver
    if not exist "%Rdp_DPNE%" call :RDP_FileGen
    set toRun=start "RDP" mstsc.exe /V:%Client_ComputerFQDN% /w:%R_W% /h:%R_H%
    Echo %toRun%
    %toRun%
    exit
 :JumpOver
 goto :eof



rem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
:Ping
 rem #LUp 18.02.2024/J.Fes
 set PingOpt=%~1
 if defined PingOpt goto :PingOpt
 Echo.
 Echo --------------------------------------------------------------------------
 Echo Ping \\%Client_ComputerName% FQDN=[%Client_ComputerFQDN%]
 Ping %Client_ComputerFQDN%
 Echo --------------------------------------------------------------------------
 goto :MyEnd

 :PingOpt
 Echo.
 Echo --------------------------------------------------------------------------
 Echo Ping %PingOpt% \\%Client_ComputerName% FQDN=[%Client_ComputerFQDN%]
 Ping %PingOpt% %Client_ComputerFQDN%
 Echo --------------------------------------------------------------------------

 :MyEnd
 goto :eof

rem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
:IpConfig
 set IpConfig_opt=%~1
 Echo.
 Echo --------------------------------------------------------------------------
 Echo ipconfig %IpConfig_opt% on \\%ComputerName%
 ipconfig %IpConfig_opt%
 Echo --------------------------------------------------------------------------
 pause
 goto :eof
:Ende
endlocal

