小弟寫了兩個bat檔案,獨立執行都正常,但是將兩個檔案合併之後卻無法正常執行
兩個檔案分別如下:
檔案1:將本機windows event log檔案匯出,並整併成一個檔案
@echo off & setlocal enabledelayedexpansion
SET WinlogPath=%USERPROFILE%\winlog
IF NOT EXIST "%WinlogPath%" MD "%WinlogPath%"
echo %WinlogPath%
pause
echo 匯出seclog
wevtutil qe security /f:text /rd:true > %USERPROFILE%\winlog\sec.txt
echo 匯出applog
wevtutil qe application /f:text /rd:true > %USERPROFILE%\winlog\app.txt
echo 匯出syslog
wevtutil qe system /f:text /rd:true > %USERPROFILE%\winlog\sys.txt
cd %WinlogPath%
copy *.txt winlog.txt
REM 每次執行前,先刪除舊的檔案
del conbine.txt
for /f "tokens=1 delims=[]" %%i in ('nbtstat -S^|find /n "乙太網路:"') do (
nbtstat -S | more +%%i >$
set /p Str=<$
del $
for /f "tokens=2 delims=[]" %%i in ('echo "!Str!"') do set LIP=%%i
)
for /f "tokens=16" %%i in ('ipconfig /all ^| find /i "IPv4 地址"') do set ip=%%i
REM 開始寫入TXT檔
echo 本機使用者名稱為: %COMPUTERNAME%>conbine.txt
REM 附加到TXT檔
echo 本機有線IP為:%LIP%>>conbine.txt
for /f "tokens=1,3 delims=," %%i in ('getmac /v /nh /fo:csv ^| findstr
"..-..-..-..-..-.."') do (
REM 附加到TXT檔
echo %%i %%j>>conbine.txt
)
REM 去除含有VMware的列,再次寫入TXT檔
type conbine.txt | (findstr /v "VMware") >conbine.txt
REM 把conbine內容放到最上面,並取名為IP.txt
copy conbine.txt+winlog.txt winlogreport.txt
=======================================================================
檔案2:連線到遠端檔案伺服器共用資料夾,下載seclog_1090218.exe到檔案1產出的合併檔案路徑下,並執行seclog_1090218.exe這個檔案
@ECHO OFF
SET Download_ServerName=192.XX.XX.XX
SET ShareFolderName=logAna
SET StartTime=%Date% %TIME%
SET ProgramPath=%USERPROFILE%\winlog
SET ExeFile=%ProgramPath%\seclog_1090218.exe
SET LogFile="%USERPROFILE%\winlog\seclog_1090218
echo %cd%
pause
IF NOT EXIST "%ProgramPath%" MD "%ProgramPath%"
REM 批次檔開始執行時間 : %StartTime% > %LogFile%
ECHO Batch file starts execution time : %StartTime% > %LogFile%
echo %cd%
pause
REM 測試連線_DownloadServer
REM 開始進行連線測試(檔案下載伺服器 %Download_ServerName% ) : %DATE% %TIME% >> %LogFile%
ECHO Start connection checkstatus.(File download Server %Download_ServerName% ): %DATE% %TIME% >> %LogFile%
ping /n 1 "%Download_ServerName%">nul||goto ErrPing
IF NOT EXIST "ExeFile" goto download ELSE goto check
:check
REM 開始進行檔案比對 : %DATE% %TIME% >> %LogFile%
ECHO Start comparing files : %DATE% %TIME% >> %LogFile%
FC "\%Download_ServerName%%ShareFolderName%\seclog_1090218.exe" "%ExeFile%" >nul
if %errorlevel% equ 0 goto run
echo %cd%
pause
:download
REM 開始進行檔案下載 : %DATE% %TIME% >> %LogFile%
ECHO Start downloading files : %DATE% %TIME% >> %LogFile%
COPY "\%Download_ServerName%%ShareFolderName%\seclog_1090218.exe" "%ExeFile%" /Y||goto ErrorCopy
echo %cd%
pause
:run
REM 執行
REM 開始執行檔案 : %DATE% %TIME% >> %LogFile%
ECHO Start executing files : %DATE% %TIME% >> %LogFile%
START %ProgramPath%\seclog_1090218.exe
ECHO Execution is complete : %DATE% %TIME% >> %LogFile%
echo %cd%
pause
:end
REM 批次檔完成時間 : %DATE% %TIME% >> %LogFile%
ECHO Batch file completion time : %DATE% %TIME% >> %LogFile%
ECHO. >> %LogFile%
echo %cd%
pause
REM 以下為錯誤訊息
:ErrPing
REM 連線失敗,批次檔結束時間 : %DATE% %TIME% >> %LogFile%
ECHO Connection failed, batch file terminated time : %DATE% %TIME% >> %LogFile%
ECHO. >> %LogFile%
echo %cd%
pause
:ErrCopy
REM 下載失敗,批次檔結束時間 : %DATE% %TIME% >> %LogFile%
ECHO Download failed, batch file terminated time : %DATE% %TIME% >> %LogFile%
ECHO. >> %LogFile%
pause
============================================================================
以上兩個檔案單獨執行都沒問題,但是合併在一起之後,只會執行檔案1,檔案2的內容無法執行
嘗試半天都無法解決,請各位大大解惑,感恩
我不認為第二個批次檔能如預期的執行
您單獨測試時是否有確認第二個批次檔可以正常執行?
問題在
IF NOT EXIST "ExeFile" goto download ELSE goto check
請修正為
IF NOT EXIST "ExeFile" (goto download) ELSE goto check
蟹大哥:謝謝你的回答
第二個檔案我在電腦上執行確實是正常的,而我依你的建議將程式碼修正,執行結果和原本相同
另外,我如果右鍵"使用管理者權限"執行合併後的檔案,那麼匯出第一個檔案正常執行,第二個檔案沒有執行的狀況,但是如果我沒有使用管理者權限執行,第一個檔案會出現seclog匯出遭拒絕(權限問題),其他皆正常,並且第二個檔案能夠正常執行,下載並執行該檔案,完全不知道問題出在哪邊
==============合併後檔案如下================
@echo off & setlocal enabledelayedexpansion
SET WinlogPath=%USERPROFILE%\winlog
IF NOT EXIST "%WinlogPath%" MD "%WinlogPath%"
echo 匯出seclog
wevtutil qe security /f:text /rd:true > %WinlogPath%\sec.txt
echo 匯出applog
wevtutil qe application /f:text /rd:true > %WinlogPath%\app.txt
echo 匯出syslog
wevtutil qe system /f:text /rd:true > %WinlogPath%\sys.txt
cd %WinlogPath%
copy *.txt winlog.txt
REM 每次執行前,先刪除舊的檔案
del conbine.txt
for /f "tokens=1 delims=[]" %%i in ('nbtstat -S^|find /n "乙太網路:"') do (
nbtstat -S | more +%%i >$
set /p Str=<$
del $
for /f "tokens=2 delims=[]" %%i in ('echo "!Str!"') do set LIP=%%i
)
for /f "tokens=16" %%i in ('ipconfig /all ^| find /i "IPv4 地址"') do set ip=%%i
REM 開始寫入TXT檔
echo 本機使用者名稱為: %COMPUTERNAME%>conbine.txt
REM 附加到TXT檔
echo 本機有線IP為:%LIP%>>conbine.txt
for /f "tokens=1,3 delims=," %%i in ('getmac /v /nh /fo:csv ^| findstr
"..-..-..-..-..-.."') do (
REM 附加到TXT檔
echo %%i %%j>>conbine.txt
)
REM 去除含有VMware的列,再次寫入TXT檔
type conbine.txt | (findstr /v "VMware") >conbine.txt
REM 把conbine內容放到最上面,並取名為IP.txt
copy conbine.txt+winlog.txt winlogreport.txt
cd "%WinlogPath%"
SET Download_ServerName=192.168.26.200
SET ShareFolderName=logAna
SET StartTime=%Date% %TIME%
SET ExeFile=%WinlogPath%\seclog_1090218.exe
SET LogFile=%WinlogPath%\seclog_1090218.txt
REM 測試連線_DownloadServer
REM 開始進行連線測試(檔案下載伺服器 %Download_ServerName% ) : %DATE% %TIME% >> %LogFile%
ECHO Start connection checkstatus.(File download Server %Download_ServerName% ): %DATE% %TIME% >> %LogFile%
ping /n 1 "%Download_ServerName%">nul||goto ErrPing
IF NOT EXIST "ExeFile" (goto download) ELSE goto check
:check
REM 開始進行檔案比對 : %DATE% %TIME% >> %LogFile%
ECHO Start comparing files : %DATE% %TIME% >> %LogFile%
FC "\%Download_ServerName%%ShareFolderName%\seclog_1090218.exe" "%ExeFile%" >nul
if %errorlevel% equ 0 goto run
:download
REM 開始進行檔案下載 : %DATE% %TIME% >> %LogFile%
ECHO Start downloading files : %DATE% %TIME% >> %LogFile%
COPY "\%Download_ServerName%%ShareFolderName%\seclog_1090218.exe" "%ExeFile%" /Y||goto ErrorCopy
:run
REM 執行
REM 開始執行檔案 : %DATE% %TIME% >> %LogFile%
ECHO Start executing files : %DATE% %TIME% >> %LogFile%
START %WinlogPath%\seclog_1090218.exe
ECHO Execution is complete : %DATE% %TIME% >> %LogFile%
:end
REM 批次檔完成時間 : %DATE% %TIME% >> %LogFile%
ECHO Batch file completion time : %DATE% %TIME% >> %LogFile%
ECHO. >> %LogFile%
Exit
REM 以下為錯誤訊息
:ErrPing
REM 連線失敗,批次檔結束時間 : %DATE% %TIME% >> %LogFile%
ECHO Connection failed, batch file terminated time : %DATE% %TIME% >> %LogFile%
ECHO. >> %LogFile%
Exit
:ErrCopy
REM 下載失敗,批次檔結束時間 : %DATE% %TIME% >> %LogFile%
ECHO Download failed, batch file terminated time : %DATE% %TIME% >> %LogFile%
ECHO. >> %LogFile%
pause
.
大概分析了您的批次檔,問題不只一處....您說能正常執行,我想知道執行結果....
IF NOT EXIST "ExeFile" (goto download) ELSE goto check
這裡應該是"%ExeFile%"
copy conbine.txt+winlog.txt winlogreport.txt
用意不明
"\%Download_ServerName%%ShareFolderName%\seclog_1090218.exe"
這裡路徑格式不對
分析後得知,第二個批次會比對檔案版本,不同時由伺服器下載檔案,然後執行
建議如果可以由來源直接執行程式或許好一些,這樣可以少寫些批次
我亂猜看看: