Can someone tell me what is wrong with this bat-file? It works untill the 'if %ERRORLEVEL% == 0'
@echo off
tasklist /FI "IMAGENAME eq FortiClient.exe" > C:\Temp\FortiClient.txt
findstr.exe /L INFO C:\Temp\FortiClient.txt > c:\Temp\Info.txt
findstr /c:"INFO: No tasks are running which match the specified criteria." c:\Temp\Info.txt
if %ERRORLEVEL% == 0 (
GOTO install
) else (
GOTO leave
)
goto:eof
:install
start E:\Forticlient_uninstall_install.bat
timeout /t 90 /nobreak
powershell -ExecutionPolicy ByPass -Command "& {Start-Process Powershell -ArgumentList ExecutionPolicy ByPass -File Messagebox.ps1' -verb RunAs}"
goto:eof
:leave
exit
'''
CodePudding user response:
You don't stop your subroutines, so after finishing the code in install
, your script is just continuing with the next line (which is the label :leave
and is ignored) and the next line, which is the next subroutine. Pit a goto :eof
to stop the code execution, where you want to stop it (a label doesn't stop the execution)
This is how I would solve this task:
@echo off
tasklist /FI "IMAGENAME eq FortiClient.exe" | find "INFO: No tasks are running which match the specified criteria." >nul
if ERRORLEVEL 1 exit /b
call E:\Forticlient_uninstall_install.bat
powershell -ExecutionPolicy ByPass -Command "& {Start-Process Powershell -ArgumentList ExecutionPolicy ByPass -File Messagebox.ps1' -verb RunAs}"
No temporary files needed, just pass the output of tasklist
to find
.
no else
clause needed by slightly rearranging the logic.
No timeout
needed, because the install-script is called and not executed as separate (independent) process
More robust handling of ERRORLEVEL (see if /?
)
using find
instead of findstr
(easier to use; your findstr
solution looks for "one of those words" instead of "the complete string")
(To make your findstr
work as intended, you have to use the /c
switch: findstr /m /c:"INFO: No tasks are running which match the specified criteria."
)