0

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...

1 Answers1

1

if bitsadmin /list | findstr syncroMSP doesn't return something, the for loop isn't executed at all and due to your logic the next executed code is that below the :success label.

Skip the else part and change your logic a bit:

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

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

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

or even better, change your logic completely:

:check
set status=unknown
for /f "tokens=1-3" %%i in ('bitsadmin /list ^| findstr syncroMSP') do set "status=%%k"
echo Status is [%status%]
if "%status%" == "SUSPENDED" (
  explorer
  echo your script worked
  pause
  exit /b
)
calc
echo no suspended transfer
pause
exit /b
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 1
    Thank you so much @Stephan. This is it. So `for` didn't find any 'tokens' to give, and because of the fact that I didn't have any transfers at all, `for` doesn't execute any of its child commands and leaves the loop straight away, moving onto my success code. I assume that if I did have a transfer but it was under a different status e.g. transferring, the code would've worked and ran the `else` as expected. But your adjustment makes sense, I will adopt it, thank you again. – Connor Dabron May 16 '19 at 22:55