追記
ノートPCが逝ってしまい使おうと思ったけど、やっぱり新規作業ここからコピペすんの面倒だったのでGithubに移しました。
やりたかったこと
- 新しいノートPC買ったとか、ディスククリーンインストールしたときのアプリ初期インストール作業がめんどい
- chocolateyでパッケージ管理するにしてもコマンド忘れちゃうので毎回ぐぐるのめんどい
- 最初しかやらないレジストリ変更とかもすぐ忘れちゃうので毎回ぐぐるのめんどい
- Linuxならbashとyum(dnf, apt-get)で簡単にできるのにぃぃ
的な悩みをPowerShellで解決したかった。chocolateyって何?って人はGoogle先生に聞いてね!
スクリプト
無駄なコードもありますが、個人用途なので適当にこんな感じで作りました。参考にしたURLは "ref :" で記述しておりますので、合わせてご参考まで。
function install_choco { # install chocolatey: ref https://chocolatey.org/docs/installation try { iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin" } catch { Write-Error("なんかエラー: " + $_.Exception) } } # インストール対象リスト $softwares = @("dropbox", "googlejapaneseinput", "terapad", "visualstudiocode", # editor "googlechrome", "firefox", # browser "virtualbox", "libreoffice", # other "thunderbird", # mailer "unity", "blender" # VR ) function install_softwares_by_choco { $excludes = @("terapad") # 今は一旦様子見であとで入れようかなとか(上から消せばいいんだけども) foreach ($soft in $softwares) { if ($excludes.Contains($soft)) { continue; } Write-Output("[INFO] exec: cinst -y $soft") cinst -y $soft } } function update_all_by_choco { $excludes = @("unity", "blender") # まとめてバージョンアップされてほしくないとか $opt = $excludes -join "," Write-Output("[INFO] exec: choco upgrade -y all --except=$opt") choco upgrade -y all --except="$opt" } function setup_configs { # CapsとCtlの入れ替え # ref: https://mk-55.hatenablog.com/entry/2015/10/15/020000 Write-Output("[INFO] modify registory: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout") Set-ItemProperty ` "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout" ` -name "Scancode Map" -value (` 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x1D, 0x00, 0x3A, 0x00, 0x00, 0x00, 0x00, 0x00 ` ) -type binary # デスクトップアイコンの縦の間隔 Write-Output("[INFO] modify registory: HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics") # ref: https://kazooblogging.com/windows10-iconspace/ Set-ItemProperty "Registry::HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics" ` -name "IconVerticalSpacing" -value (-1030) -type string # デスクトップアイコンの横の間隔 Set-ItemProperty "Registry::HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics" ` -name "IconSpacing" -value (-1030) -type string # Windows Subsysten Linuxの有効化 # https://docs.microsoft.com/ja-jp/windows/wsl/install-win10 Write-Output("[INFO] enable windows subsystem linux") Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux Write-Output("[INFO] You need to reboot.") } ### main # debug #Set-PSDebug -Trace 1 # 管理者権限チェック ref: https://qiita.com/skkzsh/items/5e03bb7792629927acfa if (! ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(` [Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Warning "You need admin permission..." Read-Host "press enter to exit this script" exit 0 } # 引数渡されたらその動作をする、渡されなければ入力を要求する $acceptable_mode = "init|install|update|config" if ($Args[0] -match "^($acceptable_mode)$") { $mode = $Args[0] } else { $mode = Read-Host "Input mode: {$acceptable_mode} " } switch ($mode) { "init" { install_choco } "install" { install_softwares_by_choco } "update" { update_all_by_choco } "config" { setup_configs } default { Write-Warning "unexpected mode: $mode" } } # エラーとか出がちなので、実行後、勝手にウインドウが閉じないように Read-Host "press enter to exit this script"
これを setup.ps1 とかで保存して、管理者権限で開いたPowerShellにて
$ powershell -NoProfile -ExecutionPolicy Unrestricted .\setup.ps1 init $ powershell -NoProfile -ExecutionPolicy Unrestricted .\setup.ps1 install $ powershell -NoProfile -ExecutionPolicy Unrestricted .\setup.ps1 config
とかやれば、
- chocolateyのインストール
- (ぼくの)主要ソフトウェアのインストール
- CapsとCtlの入れ替え
- デスクトップのアイコン間隔の設定(狭くしてます)
- Windows Subsystem Linuxの有効化
あたりが一気にできます。めでたしめでたし。
GUIで実行したいんや
せっかくなのでマウスでポチポチするだけで実行したいという気持ちもありますが、Windowsのデフォルト設定では簡単に行かないんですよね… 以下の記事様に分かりやすくまとまっていました。
セキュリティ的な制約なので、デフォルトポリシーは変えたくない、でもGUIで実行したい… というバランスを考えたときに、以下の方法に落ち着きました。
cd /d %~dp0 powershell -NoProfile -ExecutionPolicy Unrestricted .\setup.ps1 pause > nul exit /B 0
これを executor.bat として保存しsetup.ps1と同じディレクトに置きます。そして、executor.bat を「右クリック」→「管理者権限で実行」することで、setup.ps1を管理者権限で実行できます。この場合は何を実行するか入力する必要がありますが。めでたしめでたし。
※pauseしてるのはやはりデバッグとかエラーが閉じないように… ps1と二重でやってるのがあれですが…
初期設定まだなのにどっから取ってくんだよこれ
私の場合は、Dropboxにこれらスクリプトを入れておき、EdgeでWebUIにログインして取ってきたりしてます。でもそれもめんどいので、今ブログに書きました。ログイン不要だけどコピペが必要…さてどっちが楽かな…