简体   繁体   中英

Batch File via Task Scheduler - Message Box not showing

I'm using this method to show a message box from a batch file, which when launching by clicking on the .bat file, or calling from cmd windows, works as expected and shows the message box.

However, when running the batch file via Task Scheduler, the message box doesn't show.

Here's how I've added the batch to the Task Scheduler: 在此处输入图片说明

I have verified that Task Scheduler is running the batch successfully, as all other operations within the batch occur as expected.

Perhaps related, the status of the Task seems to show as 'Running' even after I would expect that batch to have finished.

In case it's relevant, here's the batch code:

@echo off

Goto :START


=======================================================================================================================
This batch checks the CloudStation (CS) sync software on the Synology servers in Brighton and Belfast offices are
syncing correctly in both directions.



Set the BelfastIP and BrightonIP variable accordingly to correct IP's, with no railing "\"

This routine tests for:
    1. File created in Brigton is syncd to Belfast
    2. File deleted in Brigton is deleted (syncd) in Belfast
    3. File created in Belfast is syncd to Brighton
    4. File deleted in Belfast is deleted (syncd) in Brighton

...and if any of the above operations fail will provide a message box giveing results of all 4 operations.  
In the event of a failure, .txt files that should be deleted by CloudStation sync are NOT cleaned up manually because:

    1)  experience shows deleting files that CS will later try to delete it will confuse CloudStation and cause
        further sync issues (it'll get stuck trying to delete a file that was already manually deleted)
    2) it leaves an auditable trail of what/when things happened
    3) It's a way of seeing that correct syncing operation has resumed 


It is intended this batch is run every hour using Windows Task Sheduler (consider office hours only to avoid returning
to a desktop with hundreds of message boxes to clear if things go tits up over the weekend
=======================================================================================================================


:START

::++++++++++ALLOWABLE SYNC DELAY++++++++++++++++++++++++++++
rem Time allowed for sync delay
set Delay=60
::++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



echo ++++++++CLOUDSTATION SYNC OPERATION CHECK+++++++++++++++
echo( 

::++++++++++VARIABLES++++++++++++++++++++++++++++++++++

set BelfastIP=\\172.16.6.20\Haworth McCall\SyncStatus
set BrightonIP=\\192.168.1.11\Haworth McCall\SyncStatus
rem (don't include a trailing '\' in the above IP paths)


echo Checking servers are reachable...

::+++++++++CHEKC SERVERS ARE REACHABLE+++++++++++++++++

if NOT EXIST "%BelfastIP%\" (
    cscript "%~dp0\MessageBox.vbs" "SYNC CHECK FAILED.  Belfast server not reachable.  Check VPN connection"
    exit /b
) else (
    echo -Belfast server found
)


if NOT EXIST "%BrightonIP%\" (  
    cscript "%~dp0\MessageBox.vbs" "SYNC CHECK FAILED.  Brighton server not reachable."
    exit /b
) else (
    echo -Brighton server found
)

echo( 

::++++++++++CREATE FILE NAME USING DATE&TIME++++++++++++++++

for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

set datestamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
set ErrorCounter=0





::++++++++TEST BRIGHTON TO BELFAST SYNCING+++++++++++++++++++

echo +++++++Brighton to Belfast sync testing+++++++++
echo (Sync considered as FAILED if file operation not synced within %Delay%seconds)
echo(

set filename=%datestamp%_Brighton.txt

REM Create file at Brighton


@echo>"%BrightonIP%\%filename%"

echo Test File added to Brighton server:
echo %BrightonIP%\%filename%
echo(
echo Wait %Delay%seconds to allow syncing:

timeout /t %Delay% /nobreak  

echo(

rem Pause to allow time for sync  
rem /t 5 = no. seconds to wait, /nobreak means no keyboard overide, >NUL means countdown not shown



    REM Check file created in Brighton has synced to Belfast and if it has, delete file in Brighton and check deletion also syncs to Belfast

    if EXIST "%BelfastIP%\%filename%" (
        echo +++ PASS - CloudStation successfully synced Test File to Belfast server
        echo(
        echo Deleting Test File from Brighton server... 
        set Bri2Bel_create=PASS
        del "%BrightonIP%\%filename%"

            echo(
            Echo Wait %Delay%seconds to allow Belfast to sync TestFile delete..."
            timeout /t %Delay% /nobreak         


            if EXIST "%BelfastIP%\%filename%" ( 
                set Bri2Bel_delete=FAIL
                set /A ErrorCounter = %ErrorCounter%+1
                rem del "%BelfastIP%\%filename%"                
                echo +++ FAIL - CloudStation did not remove deleted file from Belfast server

            ) ELSE (
                set Bri2Bel_delete=PASS 
                echo(
                echo +++ PASS - CloudStation successfully removed Test File from Belfast server
            )


    ) ELSE (
        rem del "%BrightonIP%\%filename%"       
        set Bri2Bel_create=FAIL
        set Bri2Bel_delete=n/a
        set /A ErrorCounter = %ErrorCounter%+1
        echo +++ FAIL - Cloudstation did not sync Test File to Belfast server
    )

echo(
echo ...Brighton to Belfast testing complete
echo(
echo(


::++++++++TEST BELFAST TO BRIGHTON SYNCING+++++++++++++++++++

echo +++++++Belfast to Brighton sync testing+++++++++
echo(

set filename=%datestamp%_Belfast.txt

@echo>"%BelfastIP%\%filename%"

echo Test File added to Belfast server: 
echo %BelfastIP%\%filename%
echo(
echo Wait %Delay%seconds to allow syncing:

timeout /t %Delay% /nobreak 


    REM Check file created in Belfast has synced to Brighton

    if EXIST "%BrightonIP%\%filename%" ( 
        echo +++ PASS - CloudStation successfully synced Test File to Brighton server
        echo(
        echo Deleting Test File from Brighton server... 

        del "%BelfastIP%\%filename%"
        set Bel2Bri_create=PASS


        REM Check file deletion in Belfast has synced and deleted file in Brighton  
            echo(
            Echo Wait %Delay%seconds to allow Belfast to sync TestFile delete..."

            timeout /t %Delay% /nobreak 


            if EXIST "%BrightonIP%\%filename%" ( 
                rem del "%BrightonIP%\%filename%"
                set Bel2Bri_delete=FAIL
                set /A ErrorCounter = %ErrorCounter%+1
                echo +++ FAIL - CloudStation did not remove deleted file from Brighton server

            ) ELSE (
                set Bel2Bri_delete=PASS
                echo +++ PASS - CloudStation successfully removed Test File from Brighton server
            )


    ) ELSE (
        rem del "%BrightonIP%\%filename%"
        set Bel2Bri_create=FAIL
        set Bel2Bri_delete=n/a
        set /A ErrorCounter = %ErrorCounter%+1
        echo +++ FAIL - Cloudstation did not sync Test File to Belfast server

    )
echo(   
echo ...Belfast to Brighton sync testing complete.
echo(
echo(


::++++++++SHOW MESSAGE BOX IF ANY SYNC TESTS FAIL+++++++++++++++++++


if %ErrorCounter% GTR 0 ( 

    echo === !!! SYNC ERROR !!! ===
    rem list each operation with the true/false (success/fail) displayed next to it
    rem need to find a way to put each bit of date on a new line

    cscript "%~dp0\MessageBox.vbs" "WARNING! CLOUDSTATION SYNC FAILURE!" "" "%datestamp%" "" "" "Brighton to Belfast:" "" "File Sync = %Bri2Bel_create%" "File Delete = %Bri2Bel_delete%" ""  "Belfast to Brighton:" "" "File Sync = %Bel2Bri_create%" "File Delete = %Bel2Bri_delete%"

) else (
    Echo ===ALL SYNC TESTS WERE SUCCESSFUL===
    rem Sync appers to be functioning correctly
    cscript "%~dp0\MessageBox.vbs" "CLOUDSTATION SYNC OK"
)

timeout /t 120 >NUL

exit /b

You have to set "run only when user is logged in" for the task to be visible. Otherwise it is hidden (regardless of the setting of the "Hidden" checkbox).

If you can't see the cmd window or messageBox then, under the edit action tab , change

the program/script field to cmd

the add arguments field to /k "C:\\DSCloudSync_Test\\DS_CloudStation_SyncTest.bat"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM