bat執行時自己隱藏黑框,而不是用vbs來呼叫自己

事了了了發表於2020-11-26
//autoStart.bat
@echo off 
if "%1" == "h" goto begin 
mshta vbscript:createobject("wscript.shell").run("""%~nx0"" h",0)(window.close)&&exit 
:begin 
//上面是新增隱藏黑框框的
//下面是需要執行的命令
::auth: linght
::date: 2018-01-09
::system: windows
::auto start windows exe

@set sleepTime=2
@set exeName=center_flextrbo.exe
@set logPath=%~dp0

@REM exePath "C:\Program Files (x86)\Jiketong\simpleTrbo\center\"
@set exePath=%logPath:centerAutoRestart=center%
@set runLogPath="%exePath%runLog.log"

:loop
@set aTxtfilePath="%logPath%a.txt"
@tasklist | findstr %exeName% > %aTxtfilePath%
@for /f "tokens=3 delims=:" %%a in (' find /c /v "" %aTxtfilePath% ') do @set/a result=%%a+0
@if %result% LSS 1 goto reStart
@if %result% equ 1 goto normal
@if %result% gtr 1 goto killExe

@echo off
:restart
echo %exeName% is stop RunNum: %result%, auto start %exeName%
rem start your program
set d=%date:~0,10%
set t=%time:~0,8%
@set startTxtfilePath="%logPath%start.log"
echo %d% %t%: start /b /d  %exePath% %exeName% >> %startTxtfilePath%
REM start /b /d "%exePath%" %exeName% | tee %runLogPath%
ping 0.0.0.0  -n %sleepTime% > "%logPath%null"
start /b /d "%exePath%" %exeName% > %runLogPath%
@goto sleep

:normal
rem echo %exeName% is normal runing......  RunNum: %result%
@goto sleep

:killExe
echo %exeName% is running, RunNum: %result%, too mush, kill and restart
taskkill /f /im %exeName% /t
echo auto restart %exeName% 
goto reStart
@goto sleep
@echo on

:sleep
@echo off
rem sleep 10s
ping 0.0.0.0  -n %sleepTime% > "%logPath%null"
goto loop
echo on

:over

 

這個方法相當巴適,比用vbs呼叫bat來隱藏黑框框棒多了。

其實,vbs呼叫bat來隱藏黑框框也還是可以的,不過呢,我遇到的場景是:我用vbs(如:autoStart.bat)呼叫bat(如:autoStart.bat)來隱藏黑框框。

但是呢,我的bat(如:autoStart.bat)需要增加授予管理員許可權執行(看此連結),以便PC機任何使用者登入都可以正確的執行。但是呢,我把授權的幾行命令加在當前bat最上頭的時候啊,bat內部呼叫程式就變成找不到檔案(如上面加粗並標紅的start命令)。

這個就惱火了,因為同樣的授予管理員許可權方式,別的bat(如:start_mysql.bat)就可以好好的執行。

這下啷個辦呢,就把授權的命令,單獨的儲存為一個bat(如:grantAdmin.bat),然後在grantAdmin.bat裡呼叫我的bat(autoStart.bat)。還是用vbs呼叫bat來隱藏黑框框(如:autoStart.vbs呼叫grantAdmin.bat)。問題來了,grantAdmin.bat的黑框框確是被隱藏了,autoStart.bat的黑框並沒有隱藏,說以啊,在網上找啊找,尋尋覓覓兩天時間,終於搞定,那就是用當前這邊文章的方法來隱藏黑框框。棒的很。

 

//grantAdmin.bat
@ echo off
%1 %2
ver|find "5.">nul&&goto :Admin
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :Admin","","runas",1)(window.close)&goto :eof
:Admin
start autoStart.bat
//autoStart.vbs
public const vbQuote=""""
temp="C:\A_WORK\Release_D1.1.11\SimpleTrbo_Release1012\centerAutoRestart\grantAdmin.bat"
batPath=vbQuote & temp & vbQuote
Set wshell=CreateObject("WScript.Shell")
wshell.Run batPath,0,false
Set wshell=Nothing

 

相關文章