7
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Windows PC キッティング(ソフトウェアインストール)の半自動化プログラム例

Last updated at Posted at 2019-10-23

This article explains how to reduce software installation by executing a batch program which captures Windows PC's language, 32/64 bit, version, etc. and only needs to answer Y/N to install software from factory reset situation. Helpful for companies without SCCM or Group Policy (and Group Policy cannot do all...).

DOSバッチを実行するだけでソフトウェアインストールを半自動で行うプログラムです。PCキッティングをイメージを焼くと陳腐化しますしライセンス費用がかかるケースがあります。10台以上PCを並べて一気にインストールし例外パターンにも対応できるという思いで作成しました。
また不要なウイルススキャンなどのアンインストールも行います。グループポリシーで配信、設定した方が楽なこともありますし、お金があるのであれば Microsoft SCCM を活用する方法もあります。が、中小企業で予算がないとか明日からまずはスタンドアロンのキッティングのベースをなんとかしたいという場合に気軽に使えるツールがなかったので内製化した例です。共有フォルダ(下記ではサーバー名をSERVERNAME としていますがIPアドレスでも構いません)

Windows 7 か Windows 10 か(10でもどのバージョンなのか)、日本語OSか英語OSか(外資系ならではでしたが)、64ビットか32ビットWindows等の判定を行い(今時32ビットのキッティングあるのかな?当時はありました)、ソフトウェアをインストールするかしないかのプロンプトを表示し、一つのインストールが終われば次のインストールのプロンプトが聞かれる仕組みです。ファクトリーリセットの状態から初期の管理者権限でPCモデルに依存しないインストーラーにしていますが、PCモデルをキャッチして特定のモデルのみドライバーなどをインストールさせるといった対応も考えられます。プロンプト表示を省略し強制インストールさせることで時間短縮を図ることもできます。

下記を .bat 拡張子で保存し判定条件やソフトウェアの置き場所、内容を変更し実行してください。


バッチプログラムの例


@echo off

rem グループポリシーを強制適用する場合
rem gpupdate /force

rem inicial process
set TMP_Folder=C:¥tmp
rem definition of variables
set OSlang=
set OSbit=
set OSver=
set latestODVer=

rem set default messages
call :set_messages

rem check language setting
call :langCheck

rem check OS Bit type setting
call :OSbitCheck

rem check OS version setting
call :OSverCheck

echo Language   : %OSlang%
echo OS version : %OSver%
echo OS bit     : %OSbit%

rem make a softwere list that has been already installed in this PC

call :getInstallList

rem you can customize descriptions below=================================================
echo ---------------------------------------------
echo install for all PC
echo ---------------------------------------------

set PGM_NAME=Movie Maker 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥InstallOnlyWindowsMovieMaker.exe
call :chkInstallList
call :chkResult
call :clearSet

set PGM_NAME=Java 8 Update 111 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥jre-8u111-windows-i586.exe
call :chkInstallList
call :chkResult
call :clearSet

set PGM_NAME=Flash Player for Bluejeans
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥install_flash_player_24_plugin.exe
call :chkInstallList
call :chkResult
call :clearSet

rem ------OneDrive-------
set PGM_NAME=OneDrive 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥OneDriveSetup.exe
set latestODVer=17.3.6390.0509
call :checkOneDrive
call :clearSet

set PGM_NAME=Adobe AIR 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥AdobeAIRInstaller.exe
call :chkInstallList
call :chkResult
call :clearSet

rem chatter Install
rem exceptional correspondence
rem to install this program, folders are copied from server 
set PGM_NAME=chatter
echo.
echo [%PGM_NAME%]

rem copy IE short cut and chatter folder and short cut 
set chatterDir=

if "%OSbit%"=="64bit" (

	if "%OSver%"=="Windows 10 " (
		set chatterDir=C:¥Program Files (x86^)¥PR-Chatter
	) else (
		set chatterDir=C:¥Program Files (x86^)¥Chatter
	)
) else (
	set chatterDir=C:¥Program Files¥Chatter
)

if not exist "%chatterDir%" (
	echo %Msg_do_exe%
	call :copyFolder
) else (
	echo %Msg_do_not_exe%
	echo.
)
call :clearSet

if "%OSbit%"=="32bit" (
echo ---------------------------------------------
echo install for 32bit PC
echo ---------------------------------------------
set PGM_NAME=7-Zip 16.04 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥32¥7z1604.exe
call :chkInstallList
call :chkResult
call :clearSet

set PGM_NAME=Google Chrome 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥32¥ChromeStandaloneSetup.exe
call :chkInstallList
call :chkResult
call :clearSet
)

if "%OSbit%"=="64bit" (
echo ---------------------------------------------
echo install for 64bit PC
echo ---------------------------------------------
set PGM_NAME=7-Zip 16.04 (x64^) 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥64¥7z1604-x64.exe
call :chkInstallList
call :chkResult
call :clearSet

set PGM_NAME=Google Chrome 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥64¥ChromeStandaloneSetup64.exe
call :chkInstallList
call :chkResult
call :clearSet

set PGM_NAME=iTunes 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥64¥iTunes6464Setup.exe
call :chkInstallList
call :chkResult
call :clearSet

set PGM_NAME=Old Calculator for Windows 10 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥64¥Calculator.exe
call :chkInstallList
call :chkResult
call :clearSet


)

if "%OSlang%"=="en-us" (
if "%OSbit%"=="32bit" (
echo ---------------------------------------------
echo install for en-us and 32bit PC
echo ---------------------------------------------
set PGM_NAME=Microsoft Lync 2010 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥en-us¥32¥LyncSetup.exe
call :chkInstallList
call :chkResult
call :clearSet
)
)

if "%OSlang%"=="en-us" (
if "%OSbit%"=="64bit" (
echo ---------------------------------------------
echo install for en-us and 64bit PC
echo ---------------------------------------------
set PGM_NAME=Microsoft Lync 2010 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥en-us¥64¥LyncSetup.exe
call :chkInstallList
call :chkResult
call :clearSet

set PGM_NAME=Power BI 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥en-us¥64¥PBIDesktop_x64.msi
call :chkInstallList
call :chkResult
call :clearSet

)
)

if "%OSlang%"=="ja-jp" (
echo ---------------------------------------------
echo install for ja-jp PC
echo ---------------------------------------------

set PGM_NAME=TeraPad 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥ja-jp¥tpad109.exe
call :chkInstallList
call :chkResult
call :clearSet
)

if "%OSlang%"=="ja-jp" (
if "%OSbit%"=="32bit" (
echo ---------------------------------------------
echo install for ja-jp and 32bit PC
echo ---------------------------------------------
set PGM_NAME=Microsoft Lync 2010 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥ja-jp¥32¥LyncSetup.exe
call :chkInstallList
call :chkResult
call :clearSet
)
)

if "%OSlang%"=="ja-jp" (
if "%OSbit%"=="64bit" (
echo ---------------------------------------------
echo install for ja-jp and 64bit PC
echo ---------------------------------------------
set PGM_NAME=Microsoft Lync 2010 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥ja-jp¥64¥LyncSetup.exe
call :chkInstallList
call :chkResult
call :clearSet

set PGM_NAME=Power BI
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥ja-jp¥64¥PBIDesktop_x64.msi
call :chkInstallList
call :chkResult
call :clearSet

)
)

echo.
echo.
echo =============================================
echo required to restart right after install
echo =============================================


if "%OSlang%"=="ja-jp" (
echo ---------------------------------------------
echo install for ja-jp PC
echo ---------------------------------------------
set PGM_NAME=Adobe Acrobat Reader DC - Japanese 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥ja-jp¥AcroRdrDC1501620039_ja_JP¥setup.exe
call :chkInstallList
call :chkResult
call :clearSet

set PGM_NAME=Microsoft Office 365 
set PGM_DIR=¥¥SERVEREXAMPLE¥Install¥PC-All¥Office365¥Office2013_JP¥install.bat
call :chkInstallList
call :chkResult
call :clearSet
cd /d %TMP_Folder%
)

if "%OSlang%"=="en-us" (
echo ---------------------------------------------
echo install for en-us PC
echo ---------------------------------------------
set PGM_NAME=Adobe Acrobat Reader DC 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥en-us¥AcroRdrDC1501620039_en_US¥setup.exe
call :chkInstallList
call :chkResult
call :clearSet

set PGM_NAME=Microsoft Office 365 
set PGM_DIR=¥¥SERVEREXAMPLE¥Install¥PC-All¥Office365¥Office2013_EN¥install.bat
call :chkInstallList
call :chkResult
call :clearSet
rem due to call .bat, it's nessecary to do cd 
cd /d %TMP_Folder%

)

echo ----------------------------------------------
echo install for all PC
echo ----------------------------------------------

set PGM_NAME=paint.net 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥paint.net.4.0.9.install.exe
call :chkInstallList
call :chkResult
call :clearSet

set PGM_NAME=Check Point VPN 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥E80-62.msi
call :chkInstallList
call :chkResult 
call :clearSet


set PGM_NAME=Flash Player for Chrome
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥install_flash_player_24_ppapi.exe
call :chkInstallList
call :chkResult
call :clearSet

if "%OSbit%"=="32bit" (
echo ---------------------------------------------
echo install for 32bit PC
echo ---------------------------------------------

set PGM_NAME=Symantec Endpoint Protection 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥32¥32-setup.exe
call :chkInstallList
call :chkResult
call :clearSet

)

if "%OSbit%"=="64bit" (
echo ---------------------------------------------
echo install for 64bit PC
echo ---------------------------------------------

set PGM_NAME=Symantec Endpoint Protection 
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥64¥64-setup.exe
call :chkInstallList
call :chkResult
call :clearSet
)

rem copy IE, chatter, OutLook short cut to "C:¥Users¥Public"
echo ---------------------------------------------
echo Copy short-cuts (chatter, IE, outlook2013) to C:¥Users¥Public¥Desktop
echo ---------------------------------------------
if "%OSbit%"=="64bit" (

	if "%OSver%"=="Windows 10 " (
		copy "¥¥SERVEREXAMPLE¥Group-Policy¥64¥Chatter Desktop.lnk" "C:¥Users¥Public¥Desktop"
		copy "¥¥SERVEREXAMPLE¥Group-Policy¥64¥Internet Explorer.lnk" "C:¥Users¥Public¥Desktop"
		copy "¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥Outlook 2013.lnk" "C:¥Users¥Public¥Desktop"
	) else (
		copy "¥¥SERVEREXAMPLE¥Group-Policy¥64¥Chatter Desktop.lnk" "C:¥Users¥Public¥Desktop"
		copy "¥¥SERVEREXAMPLE¥Group-Policy¥64¥Internet Explorer.lnk" "C:¥Users¥Public¥Desktop"
		copy "¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥Outlook 2013.lnk" "C:¥Users¥Public¥Desktop"
	)
) else (
	copy "¥¥SERVEREXAMPLE¥Group-Policy¥32¥Chatter Desktop.lnk" "C:¥Users¥Public¥Desktop"
	copy "¥¥SERVEREXAMPLE¥Group-Policy¥64¥Internet Explorer.lnk" "C:¥Users¥Public¥Desktop"
	copy "¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥Outlook 2013.lnk" "C:¥Users¥Public¥Desktop"
)

echo.
echo ---------------------------------------------
echo Uninstall programs 
echo these programs should be removed.
echo ---------------------------------------------
if "%OSlang%"=="ja-jp" (
set PGM_NAME=マカフィー リブセーフ - インターネットセキュリティ 
) else (
set PGM_NAME=McAfee LiveSafe - Internet Security 
)
echo %PGM_NAME%
set PGM_DIR=¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥MCPR.exe
call :chkInstallList
rem in this time, decision criteria is opposite

if %result% == 1 (
set result=0
set Msg_do_exe=program has been installed.
set Msg_call_exe="Do you want to uninstall (y/n)?  ":

) else (	
set result=1
set Msg_do_not_exe=program has already been removed.
)
call :chkResult
call :set_messages
call :clearSet

rem you can customize descriptions above----------------------------------------------


echo.
echo.
echo This batch process is completed.

pause
exit

:set_messages
set Msg_do_not_exe=program has already been installed.
set Msg_do_exe= program has NOT been installed.
set Msg_call_exe="Do you want to install (y/n)?  ":

exit /b

:copyFolder

set pushKey=
set /p pushKey="%Msg_call_exe%" %pushKey%
rem echo [%pushKey%]
	if "%pushKey%"=="y" (
	echo this process will be continued when the window closed.
	mkdir "%chatterDir%"
	echo "%chatterDir%"
	robocopy "¥¥SERVEREXAMPLE¥Group-Policy¥32or64¥PR-Chatter" "%chatterDir%" /e
	) else if "%pushKey%"=="n" ( 
	echo cannceled
	) else if "%pushKey%"=="Y" ( 
	echo type half-size "y" or cancel
	) else (
	echo cannceled
	)
	echo.


exit /b


:langCheck
rem get Language code 
for /f "tokens=3 delims= " %%a in ('chcp') do set lang=%%a

rem cases that the code is japanese or not
if "%lang%" == "932" ( goto :ViewJPN ) else ( goto :ViewDefault )

:ViewJPN
cls
rem echo Japanese
set OSlang=ja-jp
goto :EOF

@rem except Japanese
:ViewDefault
cls
rem echo English
set OSlang=en-us
goto :EOF

exit /b

:OSverCheck

For /f "tokens=2 delims=[]" %%G in ('ver') Do (set _version=%%G) 
For /f "tokens=2,3,4 delims=. " %%G in ('echo %_version%') Do (set _major=%%G& set _minor=%%H& set _build=%%I) 
 
if "%_major%"=="5" (
  if "%_minor%"=="0" ( set OSver=Windows 2000 )
  if "%_minor%"=="1" ( set OSver=Windows XP )
)
 
if "%_major%"=="6" (
  if "%_minor%"=="0" ( set OSver=Windows Vista )
  if "%_minor%"=="1" ( set OSver=Windows 7 )
  if "%_minor%"=="2" ( set OSver=Windows 8 )
  if "%_minor%"=="3" ( set OSver=Windows 8.1 )
)
 
if "%_major%"=="10" (
  if "%_minor%"=="0" ( set OSver=Windows 10 )
)

exit /b

:OSbitCheck
if "%PROCESSOR_ARCHITECTURE%" == "x86" (
    if "%PROCESSOR_ARCHITEW6432%" == "AMD64" (
rem echo 32bit process on 64bit OS - WOW64
        set OSbit=32bit
    ) else (
rem echo 32bit process on 32bit OS
        set OSbit=32bit
    )
) else (
rem echo 64bit process on 64bit OS
    set OSbit=64bit
)

exit /b


:chkResult
rem if exists the same softwere name on the list, the result returns 1

if %result% == 0 (
	echo %Msg_do_exe%
	set log_message=%Msg_do_exe%
	call :excInstall
) else (
	echo %Msg_do_not_exe%
	set log_message=%Msg_do_not_exe%
)

exit /b

:clearSet
call :writeLog 
set log_message=
set PGM_NAME=
set PGM_DIR=

exit /b

rem checking existance of temporary folder「C:¥tmp」and create install list
:getInstallList
if not exist "%TMP_Folder%" (
rem make temporary folder
	md %TMP_Folder%
) else (
	del %TMP_Folder%¥install_list.txt
)
cd /d %TMP_Folder%

rem specify a Path which is written softwere names installed
echo "HKEY_LOCAL_MACHINE¥SOFTWARE¥Microsoft¥Windows¥CurrentVersion¥Uninstall" > %TMP_Folder%¥registry_path.txt
if %OSbit%==64bit (
echo "HKEY_LOCAL_MACHINE¥SOFTWARE¥Wow6432Node¥Microsoft¥Windows¥CurrentVersion¥Uninstall" >> %TMP_Folder%¥registry_path.txt
)
rem output softwere names to the textfile 
for /F "delims=" %%A IN (registry_path.txt) DO (
	for /F "tokens=2,*" %%I in ('reg query %%A /s ^| findstr "¥<DisplayName" ^| findstr /V "更新 修正 (KB"') do (
		echo %%J >> %TMP_Folder%¥install_list.txt
	)
)

rem sort softwere name in the text
sort %TMP_Folder%¥install_list.txt > %TMP_Folder%¥install_sort_list.txt
rem delete duplicate softwere names
type nul > install_list.txt
for /f "delims=" %%I in (install_sort_list.txt) do findstr /X /C:"%%I" install_list.txt >NUL || (echo;%%I) >> install_list.txt
 
rem delete temporary text files 
del %TMP_Folder%¥registry_path.txt
del %TMP_Folder%¥install_sort_list.txt

exit /b

rem find softwere names from the list
:chkInstallList

echo.
echo [%PGM_NAME%]

cd c:/tmp
set result=
for /f "tokens=3 delims= " %%a in ('find /C "%PGM_NAME%" install_list.txt') do set RESULT=%%a

exit /b

rem call .exe file
:excInstall

set pushKey=
set /p pushKey="%Msg_call_exe%" %pushKey%
rem echo [%pushKey%]
	if "%pushKey%"=="y" (
	echo this process will be continued when the window closed.
	%PGM_DIR% > NUL 2>&1
	) else if "%pushKey%"=="n" ( 
	echo cannceled
	) else if "%pushKey%"=="Y" ( 
	echo type half-size "y" or cancel
	) else (
	echo cannceled
	)
	echo.
exit /b

:checkOneDrive
echo.
echo [checking OneDrive status... ]
rem checking existance of Onedrive
echo if an error occured, there OneDrive has not been installed.
REG QUERY HKEY_CURRENT_USER¥Software¥Microsoft¥OneDrive /v Version
rem echo %ERRORLEVEL%

IF %ERRORLEVEL%==1 ( 
    GOTO notODExist 
) else ( 
echo an error was not occurred.
    GOTO ODExist
)
exit /b
	
:ODExist

FOR /F "tokens=3 delims= " %%i in ('REG QUERY HKEY_CURRENT_USER¥Software¥Microsoft¥OneDrive /v Version') do set ODver=%%i
echo latest version:%latestODVer% 

if %ODver%==%latestODVer% (	

rem goto :exitOnedrive
) else (

call :confarmUninstall


)
exit /b

:notODExist
rem echo notODExist
call :chkInstallList
call :chkResult

exit /b

:confarmUninstall

echo OneDrive version isn't latest.
echo that may be updated automatically after a few moments.
set pushKey=
set /p pushKey="Do you want to update OneDrive manually now (y/n)?  :" %pushKey%
echo [%pushKey%]
if "%pushKey%"=="y" (

rem uninstall OneDrive  
call :uninstallOnedrive

rem install OneDrive
call :chkInstallList
call :chkResult
)
exit /b

:uninstallOnedrive

 
rem set x86="%SYSTEMROOT%¥System32¥OneDriveSetup.exe"
set x64="%SYSTEMROOT%¥SysWOW64¥OneDriveSetup.exe"

echo Closing OneDrive process.
echo.
taskkill /f /im OneDrive.exe > NUL 2>&1
ping 127.0.0.1 -n 5 > NUL 2>&1

echo Uninstalling OneDrive.
echo.
if exist %x64% (
%x64% /uninstall
) else (
REG DELETE "HKEY_CURRENT_USER¥Software¥Microsoft¥OneDrive" /f > NUL 2>&1
rem %x86% /uninstall
)
ping 127.0.0.1 -n 10 > NUL 2>&1

rem echo Removing OneDrive leftovers.
rem echo.
rd "%USERPROFILE%¥OneDrive" /Q /S > NUL 2>&1
rd "C:¥OneDriveTemp" /Q /S > NUL 2>&1
rd "%LOCALAPPDATA%¥Microsoft¥OneDrive" /Q /S > NUL 2>&1
rd "%PROGRAMDATA%¥Microsoft OneDrive" /Q /S > NUL 2>&1 

rem echo Removing OneDrive from the Explorer Side Panel.
rem echo.
REG DELETE "HKEY_CLASSES_ROOT¥CLSID¥{018D5C66-4533-4307-9B53-224DE2ED1FE6}" /f > NUL 2>&1
REG DELETE "HKEY_CLASSES_ROOT¥Wow6432Node¥CLSID¥{018D5C66-4533-4307-9B53-224DE2ED1FE6}" /f > NUL 2>&1

echo OneDrive has removed.
echo.

exit /b

:writeLog
echo %date%_%time%:[%PGM_NAME%] >> %TMP_Folder%¥log.txt
echo %log_message% >> %TMP_Folder%¥log.txt

exit /b

7
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?