mercredi 15 mai 2019

If statement not executing as expected - batch file

I'm trying to install an RMM agent on a mass number of client machines which we currently have access to through ConnectWise ScreenConnect. I've written a batch (cmd) to simplify the download & install process while being a silent install.

However, I've run into an issue with getting "if" to listen to "for" strings.

I have tried changing the symbol after do, I've tried changing the == to EQU and switched out quotations for brackets etc. to no avail.


:check
for /f "tokens=1-3" %%i in ('cmd bitsadmin /list ^| findstr syncroMSP') do (
    if "%%k"=="SUSPENDED" (
        echo Status is [%%k]
        goto success
    ) else (
        goto fail
    )
)

:success
explorer
echo If you had a suspended transfer, your script worked.
pause
exit /b

:fail
calc
your If you didn't have any suspended transfers, your script worked.
pause
exit /b

With no transfer jobs active, I expect to see the "else" command run, and eventually, see calculator open (simple way for me to see it worked). Instead I see "Status is [%%k]" followed by explorer opening.

EDIT: This morning after running the exact same batch, I get an empty cmd window with a ticking cursor block...

Aucun commentaire:

Enregistrer un commentaire