Boxstarter で快適なセットアップを

__本情報の内容（添付文書、リンク先などを含む）は、作成日時点でのものであり、予告なく変更される場合があります__

# はじめに
同僚が、__クライアント PC のトラブル続きで再セットアップが大変__と嘆いているのを見て、なぜ [Chocolatey](https://chocolatey.org/) + [Boxstarter](https://boxstarter.org/) を使わないんだろうとかふと思ったのです。たとえば、お時給でお仕事していて手塩にかけて長時間工数をかけてセットアップするのが好きな人なんて世の中にいないと思います。Boxstarter は単純に日本語のドキュメントがさほど多くもなく敷居がやたら高く見えてしまうのか、知名度の問題なのかもしれなかったので、自分がよく使う Azure 上の踏み台サーバーの作業環境の作り方をベースに簡単にご紹介します。

# 技術要素について
## Chocolatey とは
Windows の OS の機能やこれまでの MSIS や NSIS などのインストーラーパッケージと連携してソフトウェアのパッケージのダウンロード、インストール、設定、アップグレード、アンインストールのすべてのプロセスを PowerShell から行うことができるツールです。
macOS の brew に近い存在だといった方がわかりやすいでしょうか。PowerShell で使うことにします。

## Boxstarter とは
リモートでセットアップ可能な Chocolatey style の NuGet パッケージです。PowerShell DSC やAnsible, Chef, Puppet がサイレントインストールを行うものという位置づけだとしたら、Boxstarter では サイレントインストールも行うことができるのですが、簡単なスクリプトを準備しておくことで Chocolatey でのインストールとセットアップを一気に自動化することができるものという感じです。
今回はパッケージそのものをつくるのではなく、OSの設定とツールのインストールを順番に行うスクリプトを実行させます。まぁ、能書きはともかく、やってみましょう。

# 準備事項
## 前提環境について
Azure の仮想マシンで使うときの[制限事項](https://boxstarter.org/VMIntegration)を確認します。私の場合、Windows 10 か Windows Server 2016/2019 しか使わないのと、仮想マシンの作成からすべて実行するわけではないので特に問題になることはなさそうです。

> Boxstarter's Windows Azure VM support is limited to:
PowerShell Version 3 or above. Boxstarter leverages the Windows Azure PowerShell toolkit which requires at least PowerShell version 3. This is automatically installed on Windows 8, 8.1 and Server 2012 and 2012 R2. Windows 7 and Server 2008 R2 can be upgraded to Version 3 (or 4) by installing version 3 or 4 of the Windows Management Framework.
The default PowerShell remoting and endpoints that Azure creates on new VMs mus be present. By default, Windows Azure enables PowerShell remoting on all Windows VMs and it creates an endpoint that maps to the WINRM secure port.

## Chocolatey のインストール
PowerShell を管理者モードで起動して下記コマンドを実行します。

```powershell
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
```
[https://chocolatey.org/install](https://chocolatey.org/install)　参照

実行例を下記に貼っておきます。

```powershell
Getting latest version of the Chocolatey package for download.
Getting Chocolatey from https://chocolatey.org/api/v2/package/chocolatey/0.10.11.
Downloading 7-Zip commandline tool prior to extraction.
Extracting C:\Users\kailu\AppData\Local\Temp\2\chocolatey\chocInstall\chocolatey.zip to C:\Users\kailu\AppData\Local\Temp\2\chocolatey\chocInstall...
Installing chocolatey on this machine
Creating ChocolateyInstall as an environment variable (targeting 'Machine')
  Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'
WARNING: It's very likely you will need to close and reopen your shell
  before you can use choco.
Restricting write permissions to Administrators
We are setting up the Chocolatey package repository.
The packages themselves go to 'C:\ProgramData\chocolatey\lib'
  (i.e. C:\ProgramData\chocolatey\lib\yourPackageName).
A shim file for the command line goes to 'C:\ProgramData\chocolatey\bin'
  and points to an executable in 'C:\ProgramData\chocolatey\lib\yourPackageName'.

Creating Chocolatey folders if they do not already exist.

WARNING: You can safely ignore errors related to missing log files when
  upgrading from a version of Chocolatey less than 0.9.9.
  'Batch file could not be found' is also safe to ignore.
  'The system cannot find the file specified' - also safe.
chocolatey.nupkg file not installed in lib.
 Attempting to locate it from bootstrapper.
PATH environment variable does not have C:\ProgramData\chocolatey\bin in it. Adding...
WARNING: Not setting tab completion: Profile file does not exist at
'C:\Users\kailu\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'.
Chocolatey (choco.exe) is now ready.
You can call choco from anywhere, command line or powershell by typing choco.
Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles
 first prior to using choco.
Ensuring chocolatey commands are on the path
Ensuring chocolatey.nupkg is in the lib folder
```


## Boxstarter の準備
### Boxstarter のインストール
PowerShell を管理者モードで起動して Chocolatey を使用して Boxstarter をイントールします。具体的には、下記コマンドを実行します。

```powershell
CINST Boxstarter
```

実行例は下記になります。途中で何度か__"Do you want to run the script?"__　で入力を求められます。その時は Yes の Y を入力します。
面倒だなって思う場合は __cinst -y boxstarter__ としてください。

```powershell
PS C:\Users\kailu> cinst boxstarter
Chocolatey v0.10.11
Installing the following packages:
boxstarter
By installing you accept licenses for the packages.
Progress: Downloading BoxStarter.Common 2.12.0... 100%
Progress: Downloading boxstarter 2.12.0... 100%
Progress: Downloading BoxStarter.WinConfig 2.12.0... 100%
Progress: Downloading boxstarter.bootstrapper 2.12.0... 100%
Progress: Downloading boxstarter.chocolatey 2.12.0... 100%
Progress: Downloading Boxstarter.HyperV 2.12.0... 100%

BoxStarter.Common v2.12.0 [Approved]
boxstarter.common package files install completed. Performing other installation steps.
The package BoxStarter.Common wants to run 'chocolateyinstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[N]o/[P]rint): Y

C:\ProgramData\Boxstarter
C:\ProgramData\Boxstarter\BuildPackages
The Boxstarter.Common Module has been copied to C:\ProgramData\Boxstarter and added to your Module path.
You will need to open a new console for the path to be visible.
Use 'Get-Module Boxstarter.* -ListAvailable' to list all Boxstarter Modules.
To list all available Boxstarter Commands, use:
PS:>Import-Module Boxstarter.Common
PS:>Get-Command -Module Boxstarter.*

To find more info visit https://Boxstarter.org or use:
PS:>Import-Module Boxstarter.Common
PS:>Get-Help Boxstarter
Environment Vars (like PATH) have changed. Close/reopen your shell to
 see the changes (or in powershell/cmd.exe just type `refreshenv`).
 The install of boxstarter.common was successful.
  Software install location not explicitly set, could be in package or
  default install location if installer.

BoxStarter.WinConfig v2.12.0 [Approved]
boxstarter.winconfig package files install completed. Performing other installation steps.
The package BoxStarter.WinConfig wants to run 'chocolateyinstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[N]o/[P]rint): Y

The Boxstarter.WinConfig Module has been copied to C:\ProgramData\Boxstarter and added to your Module path.
You will need to open a new console for the path to be visible.
Use 'Get-Module Boxstarter.* -ListAvailable' to list all Boxstarter Modules.
To list all available Boxstarter Commands, use:
PS:>Import-Module Boxstarter.WinConfig
PS:>Get-Command -Module Boxstarter.*

To find more info visit https://Boxstarter.org or use:
PS:>Import-Module Boxstarter.WinConfig
PS:>Get-Help Boxstarter
 The install of boxstarter.winconfig was successful.
  Software install location not explicitly set, could be in package or
  default install location if installer.

boxstarter.bootstrapper v2.12.0 [Approved]
boxstarter.bootstrapper package files install completed. Performing other installation steps.
The package boxstarter.bootstrapper wants to run 'chocolateyinstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[N]o/[P]rint): Y

The Boxstarter.Bootstrapper Module has been copied to C:\ProgramData\Boxstarter and added to your Module path.
You will need to open a new console for the path to be visible.
Use 'Get-Module Boxstarter.* -ListAvailable' to list all Boxstarter Modules.
To list all available Boxstarter Commands, use:
PS:>Import-Module Boxstarter.Bootstrapper
PS:>Get-Command -Module Boxstarter.*

To find more info visit https://Boxstarter.org or use:
PS:>Import-Module Boxstarter.Bootstrapper
PS:>Get-Help Boxstarter
 The install of boxstarter.bootstrapper was successful.
  Software install location not explicitly set, could be in package or
  default install location if installer.

boxstarter.chocolatey v2.12.0 [Approved]
boxstarter.chocolatey package files install completed. Performing other installation steps.
The package boxstarter.chocolatey wants to run 'chocolateyinstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[N]o/[P]rint): Y

The Boxstarter.Chocolatey Module has been copied to C:\ProgramData\Boxstarter and added to your Module path.
You will need to open a new console for the path to be visible.
Use 'Get-Module Boxstarter.* -ListAvailable' to list all Boxstarter Modules.
To list all available Boxstarter Commands, use:
PS:>Import-Module Boxstarter.Chocolatey
PS:>Get-Command -Module Boxstarter.*

To find more info visit https://Boxstarter.org or use:
PS:>Import-Module Boxstarter.Chocolatey
PS:>Get-Help Boxstarter
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Boxstarter
 The install of boxstarter.chocolatey was successful.
  Software install location not explicitly set, could be in package or
  default install location if installer.

Boxstarter.HyperV v2.12.0 [Approved]
boxstarter.hyperv package files install completed. Performing other installation steps.
The package Boxstarter.HyperV wants to run 'chocolateyinstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[N]o/[P]rint): Y

The Boxstarter.HyperV Module has been copied to C:\ProgramData\Boxstarter and added to your Module path.
You will need to open a new console for the path to be visible.
Use 'Get-Module Boxstarter.* -ListAvailable' to list all Boxstarter Modules.
To list all available Boxstarter Commands, use:
PS:>Import-Module Boxstarter.HyperV
PS:>Get-Command -Module Boxstarter.*

To find more info visit https://Boxstarter.org or use:
PS:>Import-Module Boxstarter.HyperV
PS:>Get-Help Boxstarter
 The install of boxstarter.hyperv was successful.
  Software install location not explicitly set, could be in package or
  default install location if installer.

boxstarter v2.12.0 [Approved]
boxstarter package files install completed. Performing other installation steps.
The package boxstarter wants to run 'chocolateyinstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[N]o/[P]rint): Y

To load all Boxstarter Modules immediately, just enter 'BoxstarterShell'.
Interested in Windows Azure VM integration? Run CINST Boxstarter.Azure to install Boxstarter's Azure integration.
 The install of boxstarter was successful.
  Software install location not explicitly set, could be in package or
  default install location if installer.

Chocolatey installed 6/6 packages.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

Installed:
 - boxstarter.bootstrapper v2.12.0
 - boxstarter.hyperv v2.12.0
 - boxstarter.winconfig v2.12.0
 - boxstarter v2.12.0
 - boxstarter.chocolatey v2.12.0
 - boxstarter.common v2.12.0
```

### Boxstarter で自動化するスクリプトの準備
テキストエディタ等で下記のようなスクリプトを作成します。この作業は初回だけ。セットアップする都度このファイルを使いまわします。[こちらに公開](https://github.com/miyamam/boxstarter)しましたので、そのまま使うこともできます。

```powershell
#
# Setup for data platform demo environment
# Learn more: http://boxstarter.org/Learn/WebLauncher
# 
# Install manually

#---- セットアップ用の一時的な設定---
Update-ExecutionPolicy Unrestricted
Disable-UAC

#--- Windows Settings ---
Disable-GameBarTips
Disable-InternetExplorerESC
Set-WindowsExplorerOptions -EnableShowHiddenFilesFoldersDrives -EnableShowFileExtensions
Set-TaskbarOptions -Size Small -Dock Bottom -Combine Full -Lock
Set-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced -Name NavPaneShowAllFolders -Value 1

#--- Windows Subsystems/Features ---
# choco install = CINST
# WSL 
CINST Microsoft-Windows-Subsystem-Linux -source windowsfeatures

#--- Tools ---
CINST sysinternals
CINST git.install
CINST vscode
CINST sql-server-management-studio 
CINST azure-cli
CINST microsoftazurestorageexplorer


#--- Apps ---
CINST googlechrome
CINST powerbi


Install-ChocolateyPinnedTaskBarItem "$env:windir\system32\mstsc.exe"
Install-ChocolateyPinnedTaskBarItem "$env:programfiles\console\console.exe"


#--- Restore Temporary Settings ---
Enable-UAC
Enable-MicrosoftUpdate
Install-WindowsUpdate -acceptEula
```



### Boxstarter コマンドの実行
PowerShell の管理者ウィンドウで、下記コマンドを実行します。

```powershell
BOXSTARTERSHELL
```

実行例はこんな感じ

```powershell
PS C:\Users\kailu> BOXSTARTERSHELL

C:\Users\kailu>powershell.exe -ExecutionPolicy bypass -NoExit -Command "&'C:\ProgramData\Boxstarter\BoxstarterShell.ps1'"
Welcome to the Boxstarter shell!
The Boxstarter commands have been imported from C:\ProgramData\Boxstarter and are available for you to run in this shell.
You may also import them into the shell of your choice.

Here are some commands to get you started:
Install a Package:   Install-BoxstarterPackage
Create a Package:    New-BoxstarterPackage
Build a Package:     Invoke-BoxstarterBuild
Enable a VM:         Enable-BoxstarterVM
For Command help:    Get-Help <Command Name> -Full

For Boxstarter documentation, source code, to report bugs or participate in discussions, please visit https://boxstarter.org
```

Boxstartershell が起動したら、作成したスクリプトを実行するようにコマンドラインから指定します。途中、何度か再起動しますのでステータスを確認してログインしなおすとセットアップが再開されます。

```powershell
Install-BoxstarterPackage -PackageName "C:\Users\kailu\Desktop\boxstarter.txt"
```

GitHub に置いたものを直接指定することも可能です。

```powershell
Install-BoxstarterPackage -PackageName https://github.com/miyamam/boxstarter/raw/master/boxstarter.txt
```
インストールスクリプト実行をすべて実行し終えると下記のような画面が表示されます。
![image.png](https://qiita-image-store.s3.amazonaws.com/0/135357/32630b09-62b2-81c5-c4ba-886377ae5d4d.png)


### インストールパッケージの確認
最後にちゃんと入っているかをPowerShellのプロンプトを起動し、下記コマンドを実行して確認します。

```powershell
 clist -lo
```

実行例はこちら

```powershell
PS C:\Users\kailu> clist -lo
Chocolatey v0.10.11
azure-cli 2.0.60
azurepowershell 6.9.0
Boxstarter 2.12.0
Boxstarter.Azure 2.12.0
Boxstarter.Bootstrapper 2.12.0
Boxstarter.Chocolatey 2.12.0
Boxstarter.Common 2.12.0
Boxstarter.HyperV 2.12.0
Boxstarter.WinConfig 2.12.0
chocolatey 0.10.11
chocolatey-core.extension 1.3.3
DotNet4.5.2 4.5.2.20140902
DotNet4.6.1 4.6.01055.20170308
git.install 2.21.0
GoogleChrome 73.0.3683.75
KB2919355 1.0.20160915
KB2919442 1.0.20160915
microsoftazurestorageexplorer 1.7.0
PowerBI 2.67.5404.581
sql-server-management-studio 14.0.17289.1
sysinternals 2019.3.5
tmp9E7E.tmp 1.0.0
vscode 1.32.1
WindowsAzurePowershell 1.6.0.20180408
24 packages installed.
```

Enjoy your sweets life :)


# 参考リンク
- [https://boxstarter.org/](https://boxstarter.org/)
- [https://chocolatey.org/](https://chocolatey.org/)
- [https://github.com/miyamam/boxstarter](https://github.com/miyamam/boxstarter)