系統補丁檢測並安裝批處理

2021-06-16 09:08:02 字數 4896 閱讀 9469

命令systeminfo,可以列出打過的補丁,當然,也可以使用下面的方法來檢測和打補丁。

@echo off

rem by leo

set title=windows 補丁安裝指令碼 v0.070408

title %title%

setlocal enabledelayedexpansion

set patchflag=kb

set cat=%patchflag%*.cat

set patchlist="%temp%\patcheslist.tmp"

set installed=√ 已安裝

set notinstalled=× 未安裝

set delims=-----------------------------------------

set patch_total=0

set patch_notinstalled=0

set flag_installed=1*

set flag_notinstalled=0*

:main

title 正在搜尋... -- %title%

echo 正在搜尋當前目錄"%cd%"及其子目錄下的補丁...

echo 如果想在搜尋完成後立即安裝未安裝的補丁,請按回車。

echo.

call :pfind || (call :error 1& goto :eof)

echo.

:confirm

title 請選擇要安裝的補丁 -- %title%

echo 搜尋到如上%patch_total%個補丁,其中%patch_notinstalled%個未安裝。安裝全部請輸入all,否則請直接按回車安裝尚未安裝的補丁。

set confirm=

set /p confirm=

if /i "%confirm%" == "all" (set confirm=

) else if not defined confirm (set confirm=%flag_installed:~0,1%

) else goto confirm

echo %delims%

echo.

echo 正在安裝,請稍候。安裝程式不會搶占視窗焦點,所以你可以幹點別的:)

echo.

call :setup %confirm%

del %patchlist% 2>nul >nul

title 安裝已結束 -- %title%

echo %delims%

echo.

echo 安裝已結束。要使補丁生效,你可能需要手動重新啟動計算機。

echo 按任意鍵退出。

set title=

endlocal

pause >nul

title %comspec%

goto :eof

rem 安裝補丁

:setup

if "%1" == "" (set patchsum=%patch_total%) else set patchsum=%patch_notinstalled%

set patch_counter=0

for /f "eol=%1 usebackq tokens=2,*" %%i in (%patchlist%) do (

set /a patch_counter+=1

title !patch_counter!/%patchsum%-%%i -- %title%

set nobackup=nobackup

echo %%i | find /i "%patchflag%8" 2>nul >nul && set nobackup=n

%%j /quiet /passive /norestart /!nobackup! 2>nul >nul

echo !patch_counter!/%patchsum% %%i √)

goto :eof

rem 搜尋當前目錄下的補丁,返回非零值為失敗。

:pfind

set reg=hkey_local_machine\software\microsoft\windows\currentversion\uninstall

set listinreg="%temp%\listinreg.tmp"

reg query %reg%>%listinreg%

del %patchlist% 2>nul >nul

rem 補丁是乙個壓縮包

for /r %%i in (*%patchflag%*.exe) do call :pfind.sub "%%~ni" "%%~fi" "%%~sfi"

rem 補丁在壓縮包展開後的目錄內

for /r %%i in (%cat%) do if exist %%~dpiupdate.exe for /f %%j in ("%%~dpiupdate.exe") do call :pfind.sub "%%~ni" "%%~fj" "%%~sfj"

if not exist %patchlist% exit /b 1

sort %patchlist% /o %patchlist%

rem 得到補丁個數

for /f "tokens=3 delims= " %%i in ('find /c /i "%flag_notinstalled%" %patchlist%') do set patch_notinstalled=%%i

for /f "tokens=3 delims= " %%i in ('find /c /i "%flag_installed%" %patchlist%') do set /a patch_total=%%i + %patch_notinstalled%

del %listinreg% 2>nul >nul

if not defined patch_total exit /b 2

if %patch_total% lss 1 exit /b 3

exit /b 0

goto :eof

rem 看看補丁安裝了沒,然後寫到標準輸出和%patchlist%內

:pfind.sub

if "%~3" == "" goto :eof

call :getkbnum %1

set id=!errorlevel!

if not "!id!" == "-1" (

find /i "!id!" %listinreg% 2>nul >nul && (

set status=%flag_installed%& set isinstalled=%installed%

)||(set status=%flag_notinstalled%& set isinstalled=%notinstalled%)

echo !status! %patchflag%!id! %2>>%patchlist%

echo !isinstalled! %patchflag%!id! %3

)goto :eof

rem 返回給定字串中的kb號,返回-1表示失敗。

:getkbnum

setlocal enabledelayedexpansion

set str=%~1

if not defined str exit /b -1

if not defined patchflag set patchflag=kb

echo %patchflag%>getsize.tmp

for %%i in (getsize.tmp) do set /a offset=%%~zi-2

del getsize.tmp 2>nul >nul

set start=0

rem 僅有%patchflag%則返回-1

if /i "%str%" == "%patchflag%" exit /b -1

rem 刪除%patchflag%前的字元,刪除失敗則返回-1

:getkbnum.findkb

if "%~1" == "!str!" (if "!str:~%start%,%offset%!" == "" (exit /b -1

) else if /i "!str:~%start%,%offset%!" == "%patchflag%" (set str=!str:~%start%!

) else (set /a start+=1 & goto getkbnum.findkb))

rem 僅有%patchflag%則返回-1

set str=!str:~%offset%!

if "%str%" == "" exit /b -1

set start=0

rem 保證%patchflag%後的第乙個字元不是數字時,仍能返回-1

if "!str:~%start%,1!" gtr "9" exit /b -1

if "!str:~%start%,1!" lss "0" exit /b -1

rem 返回%patchflag%後的數字

:getkbnum.findnum

if "!str:~%start%,1!" gtr "9" exit /b !str:~0,%start%!

if "!str:~%start%,1!" lss "0" exit /b !str:~0,%start%!

set /a start+=1

goto getkbnum.findnum

endlocal

exit /b -1

:error

title error -- %title%

if "%1" == "1" (echo 沒有找到補丁。使用的搜尋規則是:cat:%cat%,patchflag:%patchflag%。程式已結束。)

set title=

pause >nul

title %comspec%

goto :eof

批處理安裝補丁

windows什麼時候一年只有 一 兩個bug被發現則下面這些文字就成廢品了。寫乙個指令碼來一次安裝n個補丁,並且全部自動設定為安裝後不重起。新建乙個指令碼檔案 allinone.cmd 指令碼檔案和不定的目錄結構如下 folder allinone.cmd hotfix windowsserver...

用批處理bat一次安裝所有的系統更新補丁

首先,將要安裝的更新程式複製到乙個資料夾 例如e hotfix 然後仿照下面的格式將指令碼檔案的內容輸入 記事本 echo off setlocal set pathtofixes e hotfix pathtofixes windowsxp kb890859 x86 chs z passive p...

批處理檢測執行結果並返回成功或失敗

result.txt 檔案中寫入了執行結果 文字 如果檔案文字包含failed,則認為執行失敗 否則認為執行成功。echo off rem result check rem first edit 2019 set targetpath e jenkins workspace smoke set ta...