Allowing BY WMS Super Users to restart/hard bounce MOCA and REFS Services while restricting RDP access to the Server

Ali Ahmed
3 min readNov 10, 2023

We’ve all been there, whether creating a new page builder, modifying an existing one, or making changes to an instance’s registry file. A hard bounce or MOCA/REFS services restart is required. However, that doesn’t mean you should or need to grant Remote Desktop access to a user to be able to do it. You could potentially write a script that can be run by a Windows Scheduled task/Cronjob to let’s say run every 1 minute and perform a certain set of tasks. The key here is to understand the MOCA Architecture well enough to understand the possible issues you can have versus the benefits you provide. This should never be done for a Production Server. However, for a demo/dev instance — I do not see a problem.

Here’s the batch script you can set up in Task Scheduler on Windows Server.

@echo off
setlocal enabledelayedexpansion
cls

::: Define Variables
set /A attempts=0
set /A maxAttempts=20
set MOCA_ENVNAME=moca.wms2022
set REFS_ENVNAME="REFS (web2022)"
set "expectedResponse=<moca-response><session-id></session-id><status>0</status></moca-response>"
set "restartFile=C:\JDA\wms2022\LES\data\restart_services"
set "tempFile=C:\Scripts\temp.txt"
set "url=http://localhost:4700/service"


::: Read File
for /f "usebackq delims=" %%a in ("%restartFile%") do set "fileContent=%%a"

if /I "%fileContent%" equ "YES" (
echo Restart Services file content is set to Yes. Setting file content back to No.
echo No > "%restartFile%"
:checkmoca
for /F "tokens=3 delims=: " %%H in ('sc query %MOCA_ENVNAME% ^| findstr " STATE"') do (
if /I "%%H" EQU "RUNNING" (
echo.
echo INFO: MOCA Service is running...
echo Stopping Service: %MOCA_ENVNAME%
net stop %MOCA_ENVNAME%


)
)
:checkrefs
for /F "tokens=3 delims=: " %%H in ('sc query %REFS_ENVNAME% ^| findstr " STATE"') do (
if /I "%%H" EQU "RUNNING" (
echo.
echo INFO: REFS Service is running...
echo Stopping Service: %REFS_ENVNAME%
net stop %REFS_ENVNAME%

)
)
) else (
ECHO.
ECHO WARNING: Restart not Allowed.
goto :endOfLoop
)

:restartmoca
net start %MOCA_ENVNAME%

:checkxmlresponse
echo.
echo Checking XML Response...
curl -s -o "%tempFile%" "%url%"

findstr /C:"%expectedResponse%" "%tempFile%" >nul
if %errorlevel% equ 0 (
echo The expected XML response was found.
goto :restartrefs
) else (
echo The expected XML response was NOT found.
timeout /t 15 /nobreak >nul
set /a attempts+=1
if !attempts! lss %maxAttempts% (
goto :checkxmlresponse
) else (
echo Maximum number of attempts reached. Exiting the loop.
goto :noxmlresponse
)
)
:restartrefs
net start %REFS_ENVNAME%
goto :endOfLoop

:noxmlresponse
echo ERROR: Unable to get XML Response back within specified time.

:endOfLoop
echo.

::: Clean up temporary file
IF EXIST %tempFile% (
del "%tempFile%"
)

:endofscript
echo ERRORLEVEL is: %ERRORLEVEL%
endlocal
exit /b %ERRORLEVEL%

Output:

Output

If you don’t want modifications to a file, you can always create a command that does it and set the ability for only certain restricted users to be able to run the command by identifying the user’s role/privileges. This will give you the option to create another set of audit trail within the database.

I would suggest setting the scheduled task to redirect standard error and standard output to a file to ensure an Audit Trail.

I have had the privilege of automating instances to:

1. automatically refresh instances to its previous state for deploying rollouts/testing configurational changes.

2. automated restarts

3. automated installation of pre-requisites and BY WMS installation/Daily Application and DB Backups all using One-Click Scripts.

4. Log Entry of Users with Datetime stamp and privilege assigned to the user and send alerts based on RDP access.

--

--

Ali Ahmed

I am an experienced IT Consultant who has a knack to solve complex problems. Want to connect? Reach out to me on: http://www.linkedin.com/in/ali-ahmed-jdawms