Control AOS from command line

Controlling the AOS from the command line
When automating backups, restores, testing etc, it is often useful to be able to control the stopping and starting of the Dynamics AX 4.0 AOS services from a batch file or DOS prompt.

These two simple DOS batch files allow you to do so, and wait for the action to complete before returning control to the caller.

Starting the AOS
Takes two parameters, %1 == server name (with slashes, e.g. \\SVR01), %2 = AOS name (the actual service name, which is reality is AOS$01, AOS$02 and so on)

Filename:

startAOSWait.bat

Contents:

@echo off REM %1 is server name with slashes, %2 is service name REM Try to start service sc.exe %1 start %2 | FIND "FAILED" IF NOT ERRORLEVEL 1 GOTO ENDCHECK REM Loop until service is started or max counter is reached SET /a COUNT = 1 :LOOP1 sc %1 query %2 | FIND "STATE" | FIND "RUNNING" IF NOT ERRORLEVEL 1 GOTO ENDOK REM ** Running ** SET /a COUNT=COUNT+1 IF %COUNT% GEQ 1000 GOTO ENDFAILED GOTO LOOP1 :ENDCHECK REM ** Start failed, but maybe already running so check sc %1 query %2 | FIND "STATE" | FIND "RUNNING" IF NOT ERRORLEVEL 1 GOTO ENDOK GOTO ENDFAILED :ENDOK REM ** Started OK so exist with errorlevel 0** ECHO STARTED OK EXIT /B 0 :ENDFAILED: REM ** Failed to start so exist with errorlevel 1** ECHO STARTING FAILED EXIT /B 1

This script will set an error level of 0 if the service was already running, or successfully started, and an error level of 1 otherwise. '

Stopping the AOS
Takes two parameters, %1 = server name (with slashes, e.g. \\SVR01), %2 = AOS name (the actual service name, which is in the format of AOS$01, AOS$02 and so on)

Filename:

stopAOSWait.bat

Contents:

@echo off REM %1 is server name with slashes, %2 is service name REM Try to stop service sc.exe %1 stop %2 | FIND "FAILED" IF NOT ERRORLEVEL 1 GOTO ENDCHECK REM Loop until service is stopped or 30000 counter is reached SET /a COUNT = 1 :LOOP1 sc %1 query %2 | FIND "STATE" | FIND "STOPPED" IF NOT ERRORLEVEL 1 GOTO ENDOK REM ** Running ** SET /a COUNT=COUNT+1 IF %COUNT% GEQ 1000 GOTO ENDFAILED GOTO LOOP1 :ENDCHECK REM ** Stop failed, but maybe already stopped so check sc %1 query %2 | FIND "STATE" | FIND "STOPPED" IF NOT ERRORLEVEL 1 GOTO ENDOK GOTO ENDFAILED :ENDOK REM ** Stopped OK so exist with errorlevel 0** ECHO STOPPED OK EXIT /B 0 :ENDFAILED: REM ** Failed to stop so exist with errorlevel 1** ECHO STOP FAILED EXIT /B 1

This script will set an error level of 0 if the service was already stopped, or was successfully stopped, and an error level of 1 otherwise.

Running the scripts
These scripts can be run stand-alone, or from another batch file. If you call them from another batch file then you should use the "call" syntax to do so.

call stopAOSWait.bat \\AXAPTA01 AOS$01 REM Do something here like copying application files, restoring a database etc call startAOSWait.bat \\AXAPTA01 AOS$01