stascorp / rdpwrap

RDP Wrapper Library
Apache License 2.0
14.63k stars 3.82k forks source link

update_rdpwrapini.bat - replaces the rdpwrap.ini on a running rdpwrap #1612

Open BrianDent opened 2 years ago

BrianDent commented 2 years ago

MOVED.

sashaqwert commented 2 years ago

Please, arrange the script code as a code and / or remove it under the spoiler.

 <details>
 Very long text.
 </details>
affinityv commented 2 years ago

Interesting, but I think all you need to do is start an elevated CMD session and do the following: RDPWInst.exe -r

That's it.

affinityv commented 2 years ago

All you need to do is copy in new INI file the right directory, then do RDPWInst -r to restart RDP. the -r is to do the restart.

affinityv commented 2 years ago

btw you are putting a lot of payload in the thread here; better to do the "view on Github" and reply from there (logged in using the browser for Github of course). Can you please edit the posts above and remove all the mess that email adds? It will be cleaner, that's all. Thanks.

affinityv commented 2 years ago

Okay, this is a script that I run as an admin user; if there is an rdpwrap.ini on the desktop, it will update it. If there is no rdpwrap.ini on the desktop, then it will just show what is i place in the program directory.

@echo on

if exist \users\%USERNAME%\desktop\rdpwrap.ini (
    echo "Before copy"
    dir     \users\%USERNAME%\desktop\rdpwrap.ini "\Program Files\RDP Wrapper\rdpwrap.ini"
    echo.
    xcopy \users\%USERNAME%\desktop\rdpwrap.ini "\Program Files\RDP Wrapper"    /FY
    echo.
    echo "After copy"
    dir     "\Program Files\RDP Wrapper\rdpwrap.ini"
    pause
    \bin\rdp-wrk\RDPWInst.exe -r
)

pause

If an update occurs, then I lose my RDP session due to the restart and then I re-connect and can see the output of the process (elevated cmd session).

Granted it doesn't do any checks for running as an administrator.

But everything works perfectly for me, every time.

affinityv commented 2 years ago

Original post (moved?) or removed? Just re-formatted here. I'll remove it if OP wants it actually removed.

THE FOLLOWING .BAT FILE COMES WITH NO/ZERO SUPPORT.

This script is useful when updating the rdpwrap.ini file when Microsoft makes changes during updates.

This script belongs in the RDPWRAP installer folder. It will stop UMRDPSERVICE, taskkill TERMSERVICE, copy the rdpwrap.ini to the C:\Program Files\RDP WRAPPER\ folder, restart the services, and finally open RDPConf.exe. A small bit of information is written to a .log file.

You must run this .bat as an administrator to stop and start the services. It verifies you are running as an administrator or closes without doing anything. It also checks for the presence of now.exe in the path. now.exe comes from the Server 2003 resource kit. No I do not know where you can currently find it. Feel free to strip these out as they add a bit of clutter.

THE FOLLOWING .BAT FILE COMES WITH NO/ZERO SUPPORT.

REM START OF UPDATE_RDPWRAPINI.BAT
REM 20211111 BJD
REM THIS SCRIPT WILL DO THE FOLLOWING:
REM 1) CHECKS FOR NOW.EXE IN THE PATH
REM 2) VERIFIES IT IS RUNNING AS AN ADMINISTRATOR
REM 3) NET STOP UMRDPSERVICE
REM 4) TASKKILLS TERMSERVICE BECAUSE NET STOP SOMETIMES FAILS
REM 5) COPIES THE RDPWRAP.INI FILE TO THE RDP WRAPPER FOLDER
REM 6) NET START TERMSERVICE AND UMRDPSERVICE
REM 7) OPENS RDPCONF.EXE
REM
REM NOTES:
REM DOS ENVIRONMENT VARIABLES
REM %~dp0 IS THE DRIVE AND PATH
REM %~n0 IS THE NAME OF THE FILE
REM %~dp0%~n0 IS THE FULL PATH AND NAME
REM %~dp0%~n0.log IS C:\DIRECTORY\FILENAME.log
REM %DATE% %TIME% IS THE DATE AND TIME
REM
REM NOW.EXE IS FROM THE SERVER 2003 RESOURCE KIT WHICH IS DIFFICULT TO FIND
REM CALLING NOW MESSAGE | find /i "--" > %~DP0%~N0.LOG GIVES YOU DATED ECHOS
REM
REM SETLOCAL ENABLEDELAYEDEXPANSION FOR /F () LOOPS
REM SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
REM UNNECESSARY BECAUSE WE ONLY LOOP ONCE BELOW
REM

REM FOR INTERACTIVE RUNS, DOESN'T MATTER FROM BATCH CALL
@echo ON

SET CLIENT=%computername%

REM DETERMINE IF NOW.EXE IS IN THE PATH OF THE BATCH FILE, DEFAULT=FALSE
REM FIND /I
REM 0 – THE STRING YOU WERE SEARCHING FOR WAS FOUND.
REM 1 – THE STRING YOU WERE SEARCHING FOR WAS NOT FOUND.
REM 2 – THIS MEANS YOU HAD A BAD SWITCH OR YOUR PARAMETERS WERE INCORRECT.
SET NOWEXISTS=FALSE
NOW TEST | FIND /I "--"
REM ERRORLEVEL=%ERRORLEVEL%
IF %ERRORLEVEL% EQU 0 (
SET NOWEXISTS=TRUE
)

REM THIS SCRIPT REQUIRES ADMINISTRATIVE OR ELEVATED PRIVILEGES. MODIFIED FROM:
REM HTTPS://STACKOVERFLOW.COM/QUESTIONS/7985755/HOW-TO-DETECT-IF-CMD-IS-RUNNING-AS-ADMINISTRATOR-HAS-ELEVATED-PRIVILEGES
REM AT IS DEPRECIATED, USE NET SESSION
NET SESSION > NUL
REM ECHO NOWEXISTS=%NOWEXISTS% ERRORLEVEL=%ERRORLEVEL%
IF %ERRORLEVEL% EQU 0 (
IF "%NOWEXISTS%" EQU "TRUE" (
NOW YOU ARE RUNNING AS AN ADMINISTRATOR WITH ELEVATED PRIVILEGES | find /i "--"
NOW YOU ARE RUNNING AS AN ADMINISTRATOR WITH ELEVATED PRIVILEGES | find /i "--" > %~dp0%~n0.log
) ELSE (
ECHO YOU ARE RUNNING AS AN ADMINISTRATOR WITH ELEVATED PRIVILEGES
ECHO YOU ARE RUNNING AS AN ADMINISTRATOR WITH ELEVATED PRIVILEGES > %~dp0%~n0.log
)
GOTO :RUNASADMINISTRATOR
) ELSE (
IF "%NOWEXISTS%" EQU "TRUE" (
NOW YOU ARE NOT RUNNING AS AN ADMINISTRATOR. EXITING IN 4 SECONDS ... | find /i "--"
NOW YOU ARE NOT RUNNING AS AN ADMINISTRATOR. EXITING IN 4 SECONDS ... | find /i "--" > %~dp0%~n0.log
) ELSE (
ECHO YOU ARE NOT RUNNING AS AN ADMINISTRATOR. EXITING IN 4 SECONDS ...
ECHO YOU ARE NOT RUNNING AS AN ADMINISTRATOR. EXITING IN 4 SECONDS ... > %~dp0%~n0.log
)
PING 127.0.0.1 > NUL 2>&1
EXIT /B 1
)
:RUNASADMINISTRATOR

SET STARTINGDATE=%DATE%
IF "%STARTINGDATE:~2,1%" == "/" (
SET MTH=%STARTINGDATE:~0,2%
SET DAY=%STARTINGDATE:~3,2%
SET YR=%STARTINGDATE:~6,4%
) ELSE (
SET MTH=%STARTINGDATE:~4,2%
SET DAY=%STARTINGDATE:~7,2%
SET YR=%STARTINGDATE:~10,4%
)
SET HR=%TIME:~0,2%
SET HR0=%TIME:~0,1%
IF "%HR0%"==" " SET HR=0%TIME:~1,1%
SET MIN=%TIME:~6,2%
ECHO %STARTINGDATE% %STARTINGDATE:~2,1% %YR%-%MTH%-%DAY%.%HR%:%MIN%

REM STOP UMRDPSERVICE AND TERMSERVICE, COPY RDPWRAP.INI AND RESTART
REM NET STOP UMRDPSERVICE ALWAYS STOPS
NET STOP UMRDPSERVICE

REM NET STOP TERMSERVICE WILL NOT ALWAYS STOP
REM NET STOP TERMSERVICE
REM LOOK IN THE TASKLIST FOR TERMSERVICE DLL, THERE IS ONLY ONE, KILL IT USING THE PID
FOR /F "TOKENS=1-3 SKIP=3 DELIMS= " %%A IN ('TASKLIST.EXE /M TERM*') DO (
ECHO A=%%A, B=%%B, C=%%C
TASKKILL /F /PID "%%B"
GOTO :EXITTASKLIST
)
:EXITTASKLIST
IF "%NOWEXISTS%" EQU "TRUE" (
NOW TERMSERVICE WAS KILLED, RDP SESSIONS WILL BE TEMPORARILY DISCONNECTED | find /i "--" >> %~dp0%~n0.log
) ELSE (
ECHO TERMSERVICE WAS KILLED, RDP SESSIONS WILL BE TEMPORARILY DISCONNECTED >> %~dp0%~n0.log
)

REM %~DP0 EXPANDS TO THE DRIVE LETTER AND PATH OF THE BATCH FILE, IT CONTAINS A TRAILING
COPY /Y "%~DP0RDPWRAP.INI" "%PROGRAMFILES%\RDP WRAPPER\RDPWRAP.INI"
NET START TERMSERVICE
NET START UMRDPSERVICE

REM OPEN RDPCONF.EXE TO SEE THE STATUS WITHOUT WAITING
REM %~DP0 EXPANDS TO THE DRIVE LETTER AND PATH OF THE BATCH FILE, IT CONTAINS A TRAILING
START %~dp0rdpconf.exe

EXIT /B 0

REM END OF UPDATE_RDPWRAPINI.BAT
affinityv commented 2 years ago

Not so sure, but please do the replies via the website and edit out the extra guff from the email!

affinityv commented 2 years ago

@BrianDent , I've re-factored the cmd file considerably, let me know what you think. Using tee via powershell, sleep instead of pings, less shouting in code, functions....

@echo off
rem SETLOCAL here is required to make sure that the change in value
rem of the NOWEXISTS parameter is correct for the cmd file's own
rem environemtn without regard to any value held before invocation
rem and so as the IF statement doesn't use a pre-runtime value.
rem - refer https://ss64.com/nt/delayedexpansion.html
SETLOCAL EnableDelayedExpansion

rem START OF UPDATE_RDPWRAPINI.BAT

rem Version History
rem 20211111 BJD
rem 20211118 AEM re-factored considerably
rem
rem THIS SCRIPT WILL DO THE FOLLOWING:
rem 1) Sets up a log file
rem 2) Checks for availability of NOW.EXE
rem 3) Checks for required EXE files
rem 4) Checks current and new INI files
rem 5) Verifies that cmd session is running with elevated privileges
rem 6) Copy new INI file to program directory
rem 7) Restart RDP Wrapper to effect new INI
rem 8) Start RDPConf

rem NOTES:
rem DOS ENVIRONMENT VARIABLES
rem %~dp0 IS THE DRIVE AND PATH
rem %~n0 IS THE NAME OF THE FILE
rem %~dp0%~n0 IS THE FULL PATH AND NAME
rem %~dp0%~n0.log IS C:\DIRECTORY\FILENAME.log
rem %DATE% %TIME% IS THE DATE AND TIME
rem
rem NOW.EXE IS FROM THE SERVER 2003 RESOURCE KIT WHICH IS DIFFICULT TO FIND
rem - also available from Windows 98 Resource Kit
rem CALLING NOW MESSAGE | find /i "--" > %~DP0%~N0.LOG GIVES YOU DATED ECHOS
rem

rem 1) Sets up a log file
rem Start with empty new log file
set LOG_FILE=%~dp0%~n0.log
if exist %LOG_FILE% del %LOG_FILE%

rem 2) Checks for availability of NOW.EXE
where now.exe > NUL &&set NOWEXISTS=TRUE||set NOWEXISTS=FALSE
call :now "set NOWEXISTS=%NOWEXISTS%"

rem 3) Checks for required EXE files
rem These are required EXEs, adjust for your OWN environment
set RDPCONF=
set RDPWInst=
for /f "usebackq" %%A in (`where "c:\bin\rdp-wrk;d:\bin\rdp-wrk:rdpconf.exe"`) do set RDPCONF=%%A
for /f "usebackq" %%A in (`where "c:\bin\rdp-wrk;d:\bin\rdp-wrk:rdpwinst.exe"`) do set RDPWInst=%%A
if not defined RDPCONF (
    call :now RDPCONF not found
    exit /b 1
)
if not defined RDPWInst (
    call :now RDPWInst not found
    exit /b 1
)
call :now RDPConf:  %RDPCONF%
call :now RDPWInst: %RDPWInst%

rem 4) Checks current and new INI files
rem Other required INI files
set ACTIVE_RDPWRAP_INI="%PROGRAMFILES%\RDP WRAPPER\RDPWRAP.INI"
set NEW_RDPWRAP_INI="%USERPROFILE%\Desktop\rdpwrap.ini"
call :now Active rdpwrap.ini file %ACTIVE_RDPWRAP_INI%
call :now NEW rdpwrap.ini file %NEW_RDPWRAP_INI%
if exist %ACTIVE_RDPWRAP_INI% (
    powershell -Command "dir '%ACTIVE_RDPWRAP_INI%'| tee -a -filepath %LOG_FILE%"
    if exist %NEW_RDPWRAP_INI% (
        powershell -Command "dir '%NEW_RDPWRAP_INI%'| tee -a -filepath %LOG_FILE%"
        powershell -Command "$scriptblock={fc.exe /l '%ACTIVE_RDPWRAP_INI%' '%NEW_RDPWRAP_INI%'};Invoke-Command -scriptblock $scriptblock |tee -a -filepath %LOG_FILE%"
        fc/l %ACTIVE_RDPWRAP_INI% %NEW_RDPWRAP_INI% >NUL && (
            call :now files are the same no work to do, exiting in 4 seconds
            call :sleep 4
            exit /b 1
        ) || (
            call :now files are different
        )
    ) else (
        call :now No new rdpwrap.ini file to work with, exiting in 4 seconds
        call :sleep 4
        exit /b 1
    )
)

goto do_it

:now
rem append message to log file
(
    if "!NOWEXISTS!" EQU "TRUE" (
        powershell -Command "$scriptblock={now.exe '%*'};Invoke-Command -scriptblock $scriptblock |tee -a -filepath %LOG_FILE%"
    ) else (
        powershell -Command "echo '%*'| tee -a -filepath %LOG_FILE%"
    )
)
goto :eof

:sleep
powershell -Command "Start-Sleep -Seconds '%1'"
goto :eof

:do_it
rem FOR INTERACTIVE RUNS, DOESN'T MATTER FROM BATCH CALL
rem...@echo ON

rem 5) Verifies that cmd session is running with elevated privileges
rem THIS SCRIPT REQUIRES ADMINISTRATIVE OR ELEVATED PRIVILEGES. MODIFIED FROM:
rem HTTPS://STACKOVERFLOW.COM/QUESTIONS/7985755/HOW-TO-DETECT-IF-CMD-IS-RUNNING-AS-ADMINISTRATOR-HAS-ELEVATED-PRIVILEGES
rem AT IS DEPRECIATED, USE NET SESSION
NET SESSION > NUL && (
    call :now YOU ARE RUNNING AS AN ADMINISTRATOR WITH ELEVATED PRIVILEGES
) || (
    call :now NOW YOU ARE NOT RUNNING AS AN ADMINISTRATOR. EXITING IN 4 SECONDS ...
    call :sleep 4
    exit /b 1
)

date/t
time/t

rem 6) Copy new INI file to program directory
echo.
xcopy %NEW_RDPWRAP_INI% %ACTIVE_RDPWRAP_INI%  /FY
echo.

rem 7) Restart RDP Wrapper to effect new INI
echo.
echo "About to restart RDP Wrapper, connection will be lost"
echo.
pause
%RDPWInst% -r

rem 8) Start RDPConf
start %RDPConf%

:end
pause
exit /b 0

rem END OF UPDATE_RDPWRAPINI.BAT
affinityv commented 2 years ago

https://www.activexperts.com/admin/reskit/ https://www.activexperts.com/admin/reskit/reskit2003/now/

This link gives the same b2sum (2e57658652ad1f9e07be338993b508f074355196ebb9c517a3f900818f69acd49835a15079f3465b0d3425ef3020b2088d64ac0fab6e5e41ca6638021f58cecf) as per what I've downloaded way, way, way back in 2003. https://technlg.net/downloads/rktools.exe

affinityv commented 2 years ago

Update on script .... new name, more logging, cleaner

@echo off
rem START OF rdpwrap-update.cmd

rem SETLOCAL here is required to make sure that the change in value
rem of the NOWEXISTS parameter is correct for the cmd file's own
rem environemtn without regard to any value held before invocation
rem and so as the IF statement doesn't use a pre-runtime value.
rem - refer https://ss64.com/nt/delayedexpansion.html
SETLOCAL EnableDelayedExpansion

rem Version History
rem 20211111 BJD
rem 20211118 AEM re-factored considerably
rem
rem THIS SCRIPT WILL DO THE FOLLOWING:
rem 1) Sets up a log file
rem 2) Checks for availability of NOW.EXE
rem 3) Checks for required EXE files
rem 4) Checks current and new INI files
rem 5) Verifies that cmd session is running with elevated privileges
rem 6) Copy new INI file to program directory
rem 7) Restart RDP Wrapper to effect new INI
rem 8) Start RDPConf

rem NOTES:
rem DOS ENVIRONMENT VARIABLES
rem %~dp0 IS THE DRIVE AND PATH
rem %~n0 IS THE NAME OF THE FILE
rem %~dp0%~n0 IS THE FULL PATH AND NAME
rem %~dp0%~n0.log IS C:\DIRECTORY\FILENAME.log
rem %DATE% %TIME% IS THE DATE AND TIME
rem
rem NOW.EXE IS FROM THE SERVER 2003 RESOURCE KIT WHICH IS DIFFICULT TO FIND
rem - also available from Windows 98 Resource Kit
rem CALLING NOW MESSAGE | find /i "--" > %~DP0%~N0.LOG GIVES YOU DATED ECHOS
rem

rem 1) Sets up a log file
rem Start with empty new log file
set LOG_FILE=%~dp0%~n0.log
if exist !LOG_FILE! del !LOG_FILE!

rem 2) Checks for availability of NOW.EXE
where now.exe > NUL &&set NOWEXISTS=TRUE||set NOWEXISTS=FALSE
call :now "set NOWEXISTS=!NOWEXISTS!"

rem 3) Checks for required EXE files
rem These are required EXEs, adjust for your OWN environment
set RDPCONF=
set RDPWInst=
for /f "usebackq" %%A in (
    `where "c:\bin\rdp-wrk;d:\bin\rdp-wrk:rdpconf.exe"`
) do set RDPCONF=%%A
for /f "usebackq" %%A in (
    `where "c:\bin\rdp-wrk;d:\bin\rdp-wrk:rdpwinst.exe"`
) do set RDPWInst=%%A
if not defined RDPCONF (
    call :now RDPCONF not found
    exit /b 1
)
if not defined RDPWInst (
    call :now RDPWInst not found
    exit /b 1
)
call :now RDPConf:  !RDPCONF!
call :now RDPWInst: !RDPWInst!

rem 4) Checks current and new INI files
rem Other required INI files
set ACTIVE_RDPWRAP_INI="%PROGRAMFILES%\RDP WRAPPER\RDPWRAP.INI"
set NEW_RDPWRAP_INI="%USERPROFILE%\Desktop\rdpwrap.ini"
call :now Active rdpwrap.ini file !ACTIVE_RDPWRAP_INI!
call :now NEW rdpwrap.ini file    !NEW_RDPWRAP_INI!
if exist !ACTIVE_RDPWRAP_INI! (
    set FN1=!ACTIVE_RDPWRAP_INI!
    set FN2=!NEW_RDPWRAP_INI!
    powershell -Command ^
        "dir '!FN1!' | tee -a -filepath !LOG_FILE!"
    if exist !NEW_RDPWRAP_INI! (
        powershell -Command ^
            "dir '!FN2!' | tee -a -filepath !LOG_FILE!"
        powershell -Command ^
            "fc.exe /l '!FN1!' '!FN2!' |tee -a -filepath !LOG_FILE!"
        fc/l !FN1! !FN2! >NUL && (
            call :now files are the same no work to do, exiting in 4 seconds
            call :sleep 4
            exit /b 1
        ) || (
            call :now files are different
        )
    ) else (
        call :now No new rdpwrap.ini file to work with, exiting in 4 seconds
        call :sleep 4
        exit /b 1
    )
)

goto do_it

:now
rem append message to log file
(
    if "!NOWEXISTS!" EQU "TRUE" (
        powershell Invoke-Command ^
            "{now.exe '%*'} | tee -a -filepath !LOG_FILE!"
    ) else (
        powershell Invoke-Command ^
            "{echo '%*'}    | tee -a -filepath !LOG_FILE!"
    )
)
goto :eof

:sleep
powershell -Command ^
    "Start-Sleep -Seconds '%1'"
goto :eof

:do_it
rem FOR INTERACTIVE RUNS, DOESN'T MATTER FROM BATCH CALL
rem...@echo ON

rem 5) Verifies that cmd session is running with elevated privileges
rem THIS SCRIPT REQUIRES ADMINISTRATIVE OR ELEVATED PRIVILEGES. MODIFIED FROM:
rem https://stackoverflow.com/QUESTIONS/7985755/HOW-TO-DETECT-IF-CMD-IS-RUNNING-AS-ADMINISTRATOR-HAS-ELEVATED-PRIVILEGES
rem AT IS DEPRECIATED, USE NET SESSION
net session 2>NUL 1> NUL && (
    call :now You are running as an administrator with elevated privileges
) || (
    call :now You are NOT running as an administrator. exiting in 4 seconds ...
    call :sleep 4
    exit /b 1
)

date/t
time/t

rem 6) Copy new INI file to program directory
echo.
xcopy !NEW_RDPWRAP_INI! !ACTIVE_RDPWRAP_INI!  /FY
echo.

rem 7) Restart RDP Wrapper to effect new INI
echo.
call :now  "About to restart RDP Wrapper, with ^`!RDPWInst! -r^` ... connection will be lost"
pause
powershell Invoke-Command ^
    "{!RDPWInst! -r} | tee -a -filepath !LOG_FILE!"
echo.

rem 8) Start RDPConf
call :now  "About to run ^`start !RDPConf!^`"
pause
powershell Invoke-Command ^
    "{start !RDPConf!} | tee -a -filepath !LOG_FILE!"

:end
exit /b 0

rem END OF rdpwrap-update.cmd