2019-02-16 14:12:09 Other

Vistaを初期化した直後の環境において、WindowsUpdateを自動で行うバッチファイル(+J

Copy Copied! Full
@if(0)==(0) rem /* @echo off cd /d %~dp0 if not exist %temp% goto End if not exist %temp%\updates md %temp%\updates whoami /groups | find "S-1-16-12288" > nul if not %errorlevel%==0 ( cscript.exe //nologo //E:JScript "%~f0" uac exit /b ) SET PHASE=%1 IF '%PHASE%'=='' SET PHASE=1 TITLE Updater phase %PHASE% FOR /F "tokens=*" %%A IN ('WMIC OS Get ServicePackMajorVersion^,Caption /Value ^| find "="') DO (SET OS.%%A) FOR /F "tokens=*" %%A IN ('WMIC CPU Get AddressWidth /Value ^| find "="') DO (SET CPU.%%A) echo %OS.Caption%|find "Windows Vista">NUL if not %errorlevel%==0 ( echo このバッチファイルは Vista 用です。 exit /b ) set REBOOT=NO SET /a NEXTSPNO=%OS.ServicePackMajorVersion%+1 goto SP%OS.ServicePackMajorVersion% :SP0 SET URL32=http://www.download.windowsupdate.com/msdownload/update/software/svpk/2008/04/windows6.0-kb936330-x86_b8a3fa8f819269e37d8acde799e7a9aea3dd4529.exe SET URL64=http://www.download.windowsupdate.com/msdownload/update/software/svpk/2008/04/windows6.0-kb936330-x64_12eed6cf0a842ce2a609c622b843afc289a8f4b9.exe goto InstSP :SP1 SET URL32=http://download.windowsupdate.com/msdownload/update/software/svpk/2009/06/windows6.0-kb948465-x86_55f17352b4398ecb4f0cc20e3737631420ca1609.exe SET URL64=http://download.windowsupdate.com/msdownload/update/software/svpk/2009/06/windows6.0-kb948465-x64_2eedca0bfa5ae8d1b0acf2117ddc4f15ac5183c9.exe goto InstSP :SP2 goto CheckIE :InstSP call set URL=%%URL%CPU.AddressWidth%%% SET FILE="%temp%\updates\VistaSP%NEXTSPNO%_%CPU.AddressWidth%.EXE" call :GetFile %FILE% %URL% echo Now installing SP%NEXTSPNO%... %FILE% /quiet /norestart if not %errorlevel%==3010 goto InstFailed goto InstSpSucceeded :InstSpSucceeded echo Service pack installation is succeeded. goto Reboot :CheckIE for /f "tokens=1,2,*" %%I in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer" /v "Version"') do ( if "%%I"=="Version" SET IE_VERSION=%%K ) echo %IE_VERSION%|find "9.0.">NUL if not %errorlevel%==0 goto InstIE9 echo Internet Explorer 9 has been installed. goto CheckDotnet35SP1 :InstIE9 SET URL32=http://download.microsoft.com/download/6/C/0/6C003240-6F63-4FC0-82EE-C451BF00A50D/IE9-WindowsVista-x86-jpn.exe SET URL64=http://download.microsoft.com/download/5/1/F/51F0DA3B-831A-46D6-9132-6329FAEB0379/IE9-WindowsVista-x64-jpn.exe call set URL=%%URL%CPU.AddressWidth%%% SET FILE="%temp%\updates\IE9_%CPU.AddressWidth%.exe" call :GetFile %FILE% %URL% echo Now installing IE9... %FILE% /quiet /norestart if not %errorlevel%==3010 goto InstFailed goto InstIeSucceeded :InstIeSucceeded echo Internet explorer 9 installation is succeeded. goto Reboot :CheckDotnet35SP1 reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" /v "Install" 2>nul | find /I "Install" 2>nul | find /I "0x1" 1>nul 2>nul if not %errorlevel%==0 goto InstDotnet35SP1 reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" /v "SP" 2>nul | find /I "SP" 2>nul | find /I "0x1" 1>nul 2>nul if not %errorlevel%==0 goto InstDotnet35SP1 echo Microsoft .NET Framework 3.5 Service Pack 1 has been installed. goto CheckDotnet35SP1LP :InstDotnet35SP1 SET URL32=http://download.windowsupdate.com/msdownload/update/software/svpk/2009/04/dotnetfx35_x86_1aab3734ef80ad9a9ea342c887c3ea76340961e8.exe SET URL64=http://download.windowsupdate.com/msdownload/update/software/svpk/2009/04/dotnetfx35_x64_ff482fb7d13ef287bd8e470aeecbbb1a7cb3bb7a.exe call set URL=%%URL%CPU.AddressWidth%%% SET FILE="%temp%\updates\DOTNET35SP1_%CPU.AddressWidth%.EXE" call :GetFile %FILE% %URL% echo Now installing Microsoft .NET framework 3.5 Serice pack 1... %FILE% /qb /nopatch /norestart /lang:ENU if not %errorlevel%==0 if not %errorlevel%==3010 goto InstFailed if %errorlevel%==3010 goto Reboot goto CheckDotnet35SP1LP :CheckDotnet35SP1LP reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5\1041" /v "Install" 2>nul | find /I "Install" 2>nul | find /I "0x1" 1>nul 2>nul if not %errorlevel%==0 goto InstDotnet35SP1Lp reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5\1041" /v "SP" 2>nul | find /I "SP" 2>nul | find /I "0x1" 1>nul 2>nul if not %errorlevel%==0 goto InstDotnet35SP1Lp echo Microsoft .NET Framework 3.5 Service Pack 1 Language Pack has been installed. goto CheckDotnet46 :InstDotnet35SP1Lp SET URL32=http://download.windowsupdate.com/msdownload/update/software/dflt/2009/01/dotnetfx35langpack_x86ja_855ecfa327fcbf2b7d84999c1e39c627835b19dc.exe SET URL64=http://download.windowsupdate.com/msdownload/update/software/dflt/2009/01/dotnetfx35langpack_x64ja_798b8bc2dd9ca5d96a2c460838cc71b6a5589e9e.exe call set URL=%%URL%CPU.AddressWidth%%% SET FILE="%temp%\updates\DOTNET35SP1LP_%CPU.AddressWidth%.EXE" call :GetFile %FILE% %URL% echo Now installing Microsoft .NET framework 3.5 Service pack 1 language pack... %FILE% /qb /norestart if not %errorlevel%==0 if not %errorlevel%==3010 goto InstFailed if %errorlevel%==3010 goto Reboot goto CheckDotnet46 :CheckDotnet46 reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" /v "Install" 2>nul | find /I "Install" 2>nul | find /I "0x1" 1>nul 2>nul if not %errorlevel%==0 goto InstDotnet46 reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" /v "Version" 2>nul | find /I "Version" 2>nul | find /I "4.6." 1>nul 2>nul if not %errorlevel%==0 goto InstDotnet46 echo Microsoft .NET Framework 4.6 has been installed. goto CheckDotnet46LP :InstDotnet46 SET URL=http://download.microsoft.com/download/C/3/A/C3A5200B-D33C-47E9-9D70-2F7C65DAAD94/NDP46-KB3045557-x86-x64-AllOS-ENU.exe SET FILE="%temp%\updates\DOTNET46.EXE" call :GetFile %FILE% %URL% echo Now installing Microsoft .NET framework 4.6... %FILE% /q /norestart if not %errorlevel%==0 if not %errorlevel%==3010 goto InstFailed if %errorlevel%==3010 goto Reboot goto CheckDotnet46LP :CheckDotnet46LP reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\1041" /v "Install" 2>nul | find /I "Install" 2>nul | find /I "0x1" 1>nul 2>nul if not %errorlevel%==0 goto InstDotnet46LP reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\1041" /v "Version" 2>nul | find /I "Version" 2>nul | find /I "4.6." 1>nul 2>nul if not %errorlevel%==0 goto InstDotnet46LP echo Microsoft .NET Framework 4.6 Language Pack has been installed. goto CheckWUA :InstDotnet46LP SET URL=http://download.microsoft.com/download/5/A/0/5A055608-2D3C-40D3-B0A8-5AF98A83D2E0/NDP46-KB3045557-x86-x64-AllOS-JPN.exe SET FILE="%temp%\updates\DOTNET46LP.EXE" call :GetFile %FILE% %URL% echo Now installing Microsoft .NET framework 4.6 language pack... %FILE% /q /norestart if not %errorlevel%==0 if not %errorlevel%==3010 goto InstFailed if %errorlevel%==3010 goto Reboot goto CheckWUA :CheckWUA cscript.exe //nologo //E:JScript "%~f0" checkWUA if %errorlevel%==1 goto InstWUA echo Windows Update Agent ^(Newest^) has been installed. goto CheckKBs :InstWUA set URL32=http://download.windowsupdate.com/windowsupdate/redist/standalone/7.6.7600.320/WindowsUpdateAgent-7.6-x86.exe set URL64=http://download.windowsupdate.com/windowsupdate/redist/standalone/7.6.7600.320/WindowsUpdateAgent-7.6-x64.exe call set URL=%%URL%CPU.AddressWidth%%% SET FILE="%temp%\updates\WUAGENT_%CPU.AddressWidth%.EXE" call :GetFile %FILE% %URL% echo Now installing Windows Update Agent... %FILE% /wuforce /quiet /norestart if not %errorlevel%==0 if not %errorlevel%==3010 goto InstFailed if %errorlevel%==3010 set REBOOT=YES goto CheckKBs :CheckKBs echo Listing up installed KBs... if exist %temp%\list.txt del %temp%\list.txt wmic qfe list>%temp%\list.txt call :CheckMSU KB3205638 http://download.windowsupdate.com/d/msdownload/update/software/secu/2016/11/windows6.0-kb3205638-x86_e2211e9a6523061972decd158980301fc4c32a47.msu http://download.windowsupdate.com/d/msdownload/update/software/secu/2016/11/windows6.0-kb3205638-x64_a52aaa009ee56ca941e21a6009c00bc4c88cbb7c.msu call :CheckMSU KB4012583 http://download.windowsupdate.com/c/msdownload/update/software/secu/2017/02/windows6.0-kb4012583-x86_1887cb5393b62cbd2dbb6a6ff6b136e809a2fbd0.msu http://download.windowsupdate.com/c/msdownload/update/software/secu/2017/02/windows6.0-kb4012583-x64_f63c9a85aa877d86c886e432560fdcfad53b752d.msu call :CheckMSU KB4015380 http://download.windowsupdate.com/d/msdownload/update/software/secu/2017/03/windows6.0-kb4015380-x86_3f3548db24cf61d6f47d2365c298d739e6cb069a.msu http://download.windowsupdate.com/c/msdownload/update/software/secu/2017/03/windows6.0-kb4015380-x64_959aedbe0403d160be89f4dac057e2a0cd0c6d40.msu call :CheckMSU KB4019204 http://download.windowsupdate.com/c/csa/csa/secu/2017/05/windows6.0-kb4019204-x86-custom_cc1a90841c15759e36c5095580dfb0b32b34eb8a.msu http://download.windowsupdate.com/c/csa/csa/secu/2017/05/windows6.0-kb4019204-x64-custom_d9d9d6baa3ea706ff7148ca2c0a06f861c1d77c4.msu call :CheckMSU KB4018271 http://download.windowsupdate.com/c/csa/csa/secu/2017/05/ie9-windows6.0-kb4018271-x86-custom_cd47df4c2de16304e79a9bb03da882180182c3db.msu http://download.windowsupdate.com/c/csa/csa/secu/2017/05/ie9-windows6.0-kb4018271-x64-custom_03cc4a6e57cecbe73ea5c9f45dc90a0cfe15eecb.msu if "%REBOOT%"=="YES" goto Reboot goto RunWindowsUpdate :RunWindowsUpdate echo ^(Re^)starting Windows Update Service... net stop wuauserv 1>nul 2>nul cscript.exe //nologo //E:JScript "%~f0" disableMU net start wuauserv 1>nul 2>nul cscript.exe //nologo //E:JScript "%~f0" WU SET RESULT=%ERRORLEVEL% if %RESULT%==-1 goto InstFailed if %RESULT%==2 goto Reboot if %RESULT%==1 goto RunWindowsUpdate goto CheckAdditionalUpdate :CheckAdditionalUpdate call :CheckMSU KB4018466 http://download.windowsupdate.com/c/csa/csa/secu/2017/04/windows6.0-kb4018466-x86-custom_ff895d127be20344a0905d8f5bac2712b15d5c42.msu http://download.windowsupdate.com/c/csa/csa/secu/2017/04/windows6.0-kb4018466-x64-custom_f745d7719f346e656afb0cb2fae119d303a689a0.msu call :CheckMSU KB4021903 http://download.windowsupdate.com/c/msdownload/update/software/secu/2017/05/windows6.0-kb4021903-x86_e242c183d5161a316b402855f03c57150ef59cf4.msu http://download.windowsupdate.com/c/msdownload/update/software/secu/2017/05/windows6.0-kb4021903-x64_d945e443391871f55a9d01d3fdd4c6c48370ecec.msu call :CheckMSU KB4024402 http://download.windowsupdate.com/c/msdownload/update/software/secu/2017/06/windows6.0-kb4024402-x86-custom_fd416b75f530305df455c1ea9098eccd48acb103.msu http://download.windowsupdate.com/c/msdownload/update/software/secu/2017/06/windows6.0-kb4024402-x64-custom_a53e6cda8028f207a3664e12ee23e401914e55a9.msu if "%REBOOT%"=="YES" goto Reboot goto End :CheckMSU type %temp%\list.txt | find "%1" 1>nul 2>nul if %errorlevel%==0 ( echo %1 has been installed. goto EOF ) set URL32=%2 set URL64=%3 call set URL=%%URL%CPU.AddressWidth%%% SET FILE="%temp%\updates\%1_%CPU.AddressWidth%.MSU" call :GetFile %FILE% %URL% echo Now installing %1... wusa %FILE% /quiet /norestart if not %errorlevel%==0 if not %errorlevel%==3010 goto InstFailed if %errorlevel%==3010 set REBOOT=YES goto EOF :End if exist %temp%\list.txt del %temp%\list.txt if exist %temp%\updates rd/s/q %temp%\updates echo. echo Update finish. shutdown -s -t 0 exit :InstFailed echo Install failure: %FILE% pause exit :Reboot SET /A PHASE+=1 REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnceEx\%PHASE%" /ve /t REG_SZ /d "Updater phase %PHASE%">NUL REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnceEx\%PHASE%" /v 1 /t REG_SZ /d "\"%~f0\" %PHASE%">NUL shutdown -r -t 0 exit :GetFile if not exist %1 ( echo Now downloading %~2 to %~1 bitsadmin.exe /TRANSFER getfile %2 %1 >NUL 2>&1 ) goto EOF :EOF exit /b rem */ @end (function(args){ var MBSA = { uac: function(){ var cmd = '"/c ""' + WScript.ScriptFullName + '" '; for (var i = 0; i < WScript.Arguments.Length; i++) { var arg = WScript.Arguments(i); if(arg!="uac") cmd += '"' + arg + '" '; } cmd += ' "'; (new ActiveXObject('Shell.Application')).ShellExecute('cmd.exe', cmd, '', 'runas', 1); WScript.Quit(0); }, checkWUA: function(){ var objFileSystem = WScript.CreateObject("Scripting.FileSystemObject"); var wshShell = WScript.CreateObject("WScript.Shell"); var strSystemFolder = wshShell.ExpandEnvironmentStrings("%SystemRoot%") + "\\system32"; var strWUAFileName = strSystemFolder + "\\wuaueng.dll"; var GetFileVersion,tmp=[0,0,0,0]; if(objFileSystem.FileExists(strWUAFileName)){ GetFileVersion = objFileSystem.GetFileVersion(strWUAFileName); tmp = GetFileVersion.split("."); } WScript.Quit((tmp[0]-0>=7&&tmp[1]-0>=6&&tmp[2]-0>=7600&&tmp[3]-0>=256)?0:1); }, WU:function(){ try{ var HiddenList = { "087b85de-3627-4a1f-bf1b-e6d3bcea03f0": "Windows Live Essentials 2011 (KB2434419)", "cd208f12-7a88-4118-bdc0-58346dc9f966": "Microsoft Security Essentials - 4.4.304.0 (KB2902907)", "555822da-0fd7-4444-bf17-8249c7767bdf": "Feature Pack - Microsoft Security Essentials - 4.9.218.0 (KB3140479)", "66aa199d-6dc6-43eb-a41c-54db322b23b4": "Update for Microsoft Security Essentials - 4.10.209.0 (KB3205972)", "_": "" // dummy }; var updateSession = new ActiveXObject("Microsoft.Update.Session"); updateSession.ClientApplicationID = "My test script"; var updateSearcher = updateSession.CreateUpdateSearcher(); WScript.Echo("Searching for updates...\n"); var searchResult = updateSearcher.Search("IsInstalled=0 and IsHidden=0 and Type='Software'"); WScript.Echo("List of applicable items on the machine:"); var count = 0; var str = ""; for(var I = 0; I<searchResult.Updates.Count; I++){ var update = searchResult.Updates.Item(I); var arr = []; arr.push((I + 1)+ "> " + update.Title); arr.push(" Type: " + update.Type); arr.push(" Category0Type: " + update.Categories.Item(0).Type); arr.push(" Category0Name: " + update.Categories.Item(0).Name); arr.push(" Category1Type: " + update.Categories.Item(1).Type); arr.push(" Category1Name: " + update.Categories.Item(1).Name); arr.push(" UpdateID: " + update.Identity.UpdateID); arr.push(" BrowseOnly: " + update.BrowseOnly); arr.push(" AutoSelectOnWebSites: " + update.AutoSelectOnWebSites); WScript.Echo(arr.join("\n")); str += arr.join("\n")+"\n"; if(HiddenList[update.Identity.UpdateID]){ update.IsHidden = true; } else count++; } if(str!=""){ var fso = new ActiveXObject("Scripting.FileSystemObject"); var cur = fso.getParentFolderName(WScript.ScriptFullName); var fname = fso.BuildPath(cur,"wulist.txt"); var f = fso.CreateTextFile(fname, true, true); f.write(str); f.close(); } if(count == 0){ WScript.Echo("There are no applicable updates."); WScript.Quit(0); } WScript.Echo("\nCreating collection of updates to download:"); var updatesToDownload = new ActiveXObject("Microsoft.Update.UpdateColl"); var strInput; for(var I = 0; I < searchResult.Updates.Count; I++){ var update = searchResult.Updates.Item(I); if(update.IsHidden == true) continue; if(update.Categories.Item(1).Name=="Windows Vista Ultimate Language Packs") continue; //if(update.Categories.Item(1).Name=="Windows Ultimate Extras") continue; var addThisUpdate = false if(update.InstallationBehavior.CanRequestUserInput == true){ WScript.Echo((I + 1) + "> skipping: " + update.Title + " because it requires user input"); } else { if(update.EulaAccepted == false){ WScript.Echo((I + 1) + "> note: " + update.Title + " has a license agreement that must be accepted:"); WScript.Echo(update.EulaText); WScript.Echo("Do you accept this license agreement? (Y/N)"); strInput = WScript.StdIn.Readline(); WScript.Echo(""); if (strInput.toUpperCase() == "Y") { update.AcceptEula(); addThisUpdate = true; } else { WScript.Echo((I + 1) + "> skipping: " + update.Title + " because the license agreement was declined"); } } else { addThisUpdate = true } } if(addThisUpdate == true) { WScript.Echo((I + 1) + "> adding: " + update.Title); updatesToDownload.Add(update); } } if(updatesToDownload.Count == 0) { WScript.Echo("All applicable updates were skipped."); WScript.Quit(0); } WScript.Echo("\nDownloading updates..."); var downloader = updateSession.CreateUpdateDownloader(); downloader.Updates = updatesToDownload; downloader.Download(); WScript.Echo("\nSuccessfully downloaded updates:"); //WScript.Quit(0); // test var updatesToInstall = new ActiveXObject("Microsoft.Update.UpdateColl"); var rebootMayBeRequired = false; for(var I = 0; I < searchResult.Updates.Count; I++){ var update = searchResult.Updates.Item(I); if(update.IsHidden == false && update.IsDownloaded == true && updatesToInstall.Count < 50) { WScript.Echo((I + 1) + "> " + update.Title); updatesToInstall.Add(update); if(update.InstallationBehavior.RebootBehavior > 0) { rebootMayBeRequired = true; } } } if(updatesToInstall.Count == 0) { WScript.Echo("No updates were successfully downloaded."); WScript.Quit(0); } if(rebootMayBeRequired == true) { WScript.Echo("\nThese updates may require a reboot."); } var rebootRequired = false; WScript.Echo("Installing updates..."); var installer = updateSession.CreateUpdateInstaller(); installer.Updates = updatesToInstall; var installationResult = installer.Install(); //Output results of install WScript.Echo("Installation Result: " + installationResult.ResultCode); rebootRequired = installationResult.RebootRequired; WScript.Echo("Reboot Required: " + rebootRequired +"\n"); WScript.Echo("Listing of updates installed and individual installation results:"); for(var I = 0; I < updatesToInstall.Count; I++) { WScript.Echo((I + 1) + "> " + updatesToInstall.Item(I).Title + ": " + installationResult.GetUpdateResult(I).ResultCode); } WScript.Quit(rebootRequired ? 2 : 1); } catch(e){ WScript.Quit(-1); } }, enableMU: function(){ var svm = WScript.CreateObject("Microsoft.Update.ServiceManager"); svm.AddService2("7971f918-a847-4430-9279-4a52d1efe18d",7,""); }, disableMU: function(){ try{ var svm = WScript.CreateObject("Microsoft.Update.ServiceManager"); svm.RemoveService("7971f918-a847-4430-9279-4a52d1efe18d"); }catch(e){} }, dummy: function(){} }; if(args.length > 0) { var arg = args.Item(0); if(typeof(MBSA[arg])==="function") { MBSA[arg](); } } })(WScript.Arguments);
Vistaを初期化した直後の環境において、WindowsUpdateを自動で行うバッチファイル(+JSCRIPT)を作ってみました。