有人能告訴我這個 bat 檔案有什么問題嗎?它一直作業到 '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
'''
uj5u.com熱心網友回復:
您不會停止子例程,因此在完成 中的代碼后install,您的腳本將繼續執行下一行(即標簽:leave并被忽略)和下一行,即下一個子例程。坑 agoto :eof停止代碼執行,你想停止它的地方(一個標簽不會停止執行)
這就是我將如何解決此任務:
@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}"
不需要臨時檔案,只需將輸出傳遞tasklist給find.
無需else通過稍微重新排列邏輯即可。
不需要timeout,因為安裝腳本被呼叫而不是作為單獨的(獨立的)行程執行
ERRORLEVEL 的更穩健處理(請參閱 參考資料if /?)
使用find而不是findstr(更容易使用;您的findstr解決方案尋找“這些詞之一”而不是“完整的字串”)
(為了使您的findstr作業按計劃,你必須使用的/c開關:findstr /m /c:"INFO: No tasks are running which match the specified criteria.")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/392193.html
標籤:批处理文件
上一篇:查找驅動器名稱...不是驅動器號...使用批處理檔案
下一篇:命令輸出設定為變數
