The Will Will Web

記載著 Will 在網路世界的學習心得與技術分享

分享一組我在啟動 Windows Sandbox 時都會執行的初始設定腳本

在本機進行 Windows 應用程式的測試,最簡易的方法就是啟動 Windows Sandbox 沙盒環境,不用十秒就可以開啟一台臨時的虛擬機,關閉後所有資料都不會保留,非常方便用來驗證許多安裝作業流程。不過 Windows 10 內建的這個 Windows Sandbox 實在是太乾淨了,每次啟動都要手動安裝許多工具才能開始使用,所以我打算用這篇文章記錄一下我會在啟動後執行的自動化命令。

如果你尚未啟用 Windows Sandbox 功能,可以在你的 Windows 10 Pro/Ent/Edu 版本執行以下命令啟用:

Enable-WindowsOptionalFeature -FeatureName "Containers-DisposableClientVM" -All -Online

在開啟 Windows Sandbox 之後,請先開啟 Windows PowerShell 視窗,貼上以下命令即可全自動設定完成:

# 調整 ExecutionPolicy 等級到 RemoteSigned
Set-ExecutionPolicy RemoteSigned -Force

# 安裝 PowerShellGet 所需的 NuGet 套件提供者,並設定信任 PSGallery
# https://learn.microsoft.com/en-us/powershell/scripting/gallery/installing-psget
Install-PackageProvider -Name NuGet -Force
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

# 變更系統語言設定並設定 zh-TW 為顯示語言
$UserLanguageList = New-WinUserLanguageList -Language "en-US"
$UserLanguageList.Add("zh-TW")
Set-WinUserLanguageList -LanguageList $UserLanguageList -Force
Set-WinUILanguageOverride -Language zh-TW

# 安裝 Chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

# 建立 $PROFILE 所需的資料夾
[System.IO.Directory]::CreateDirectory([System.IO.Path]::GetDirectoryName($PROFILE))

# 設定 PowerShell 的 ProgressPreference, TLS 1.2 與 PSReadLine 快速鍵
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables#progresspreference
@'
# 修正 PowerShell 關閉進度列提示
$ProgressPreference = 'SilentlyContinue'

# 使用 TLS 1.2 進行網路安全連線
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# 設定按下 Ctrl+d 可以退出 PowerShell 執行環境
Set-PSReadlineKeyHandler -Chord ctrl+d -Function ViExit

# 設定按下 Ctrl+w 可以刪除一個單字
Set-PSReadlineKeyHandler -Chord ctrl+w -Function BackwardDeleteWord

# 設定按下 Ctrl+e 可以移動游標到最後面(End)
Set-PSReadlineKeyHandler -Chord ctrl+e -Function EndOfLine

# 設定按下 Ctrl+a 可以移動游標到最前面(Begin)
Set-PSReadlineKeyHandler -Chord ctrl+a -Function BeginningOfLine

function hosts { notepad c:\windows\system32\drivers\etc\hosts }

# 移除內建的 curl 與 wget 命令別名
If (Test-Path Alias:curl) {Remove-Item Alias:curl}
If (Test-Path Alias:wget) {Remove-Item Alias:wget}
'@ | Out-File $PROFILE

. $PROFILE

# 安裝 Microsoft YaHei Mono 字型
Install-Module -Name PSWinGlue -Force
$tmpFolder = New-TemporaryFile | %{ rm $_; mkdir $_ }
Invoke-WebRequest -Uri "https://github.com/doggy8088/MicrosoftYaHeiMono-CP950/blob/master/MicrosoftYaHeiMono-CP950.ttf?raw=true" -OutFile "$tmpFolder\MicrosoftYaHeiMono-CP950.ttf"
Install-Font -Scope System -Path $tmpFolder

# 安装常用字型
choco install cascadiafonts -y

# 安裝常用應用程式
choco install wget 7zip notepad2 git -y

# Chocolatey 安裝 Git 後雖然有註冊 PATH 環境變數,但目前工作階段並沒有註冊
$env:Path += ';C:\Program Files\Git\cmd'

# 設定預設 Git Alias
git config --global alias.ci   commit
git config --global alias.cm   "commit --amend -C HEAD"
git config --global alias.co   checkout
git config --global alias.st   status
git config --global alias.sts  "status -s"
git config --global alias.br   branch
git config --global alias.re   remote
git config --global alias.di   diff
git config --global alias.type "cat-file -t"
git config --global alias.dump "cat-file -p"
git config --global alias.lo   "log --oneline"
git config --global alias.ls   "log --show-signature"
git config --global alias.ll   "log --pretty=format:'%h %ad | %s%d [%Cgreen%an%Creset]' --graph --date=short"
git config --global alias.lg   "log --graph --pretty=format:'%Cred%h%Creset %ad |%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset [%Cgreen%an%Creset]' --abbrev-commit --date=short"
git config --global alias.alias "config --get-regexp ^alias\."
git config --global alias.ignore "!gi() { curl -sL https://www.gitignore.io/api/\$@ ;}; gi"
git config --global alias.iac  "!giac() { git init && git add . && git commit -m 'Initial commit' ;}; giac"

# 設定 git 預設編輯器為 notepad
git config --global core.editor "notepad"

# 直接設定 Git 預設 user.name 與 user.email
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

安裝好之後,建議手動調整 Windows PowerShell命令提示字元 (Command Prompt) 視窗的字型為 Microsoft YaHei Mono 字型!

安裝 .NET SDK (LTS) 開發環境

# 安裝 LTS 版本的 .NET SDK (LTS)
# https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script?WT.mc_id=DT-MVP-4015686
Invoke-WebRequest https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.ps1 -outfile $env:temp\dotnet-install.ps1
. $env:temp\dotnet-install.ps1 -Channel LTS

# 下載設定環境變數的利器 SetEnv
Invoke-WebRequest -Uri "https://github.com/doggy8088/SetEnv/releases/download/1.0/SetEnv.exe" -OutFile "$env:temp\SetEnv.exe"

# 設定使用者 PATH 環境變數
. $env:temp\SetEnv.exe -ua PATH %"$env:LOCALAPPDATA\Microsoft\dotnet"

# 查看 .NET 安裝版本
dotnet --info

安裝開發環境

  • 安裝 Visual Studio Code 與 Node.js

    choco install vscode nodejs-lts -y
    $env:Path = 'C:\Program Files\nodejs;C:\Program Files\Microsoft VS Code\bin;' + $env:Path
    
  • 安裝 Angular CLI 與其他常用工具

    npm i -g @angular/cli rimraf json-server lite-server source-map-explorer
    
  • 安裝 Angular 在 VS Code 的擴充套件

    code --install-extension doggy8088.angular-extension-pack
    code --install-extension ms-vscode.js-debug
    code --install-extension nrwl.angular-console
    code --install-extension esbenp.prettier-vscode
    code --install-extension doggy8088.git-extension-pack
    code --install-extension johnpapa.vscode-peacock
    code --install-extension IBM.output-colorizer
    code --install-extension ms-azuretools.vscode-bicep
    

安裝其他非必要的應用程式

  • PowerShell Core

    choco install powershell-core -y
    

    安裝好之後,建議使用 PowerShell Core 當成主要的命令提示字元環境,問題會少很多,主要差異在:

    1. 所有 In/Out 預設皆使用 UTF-8 編碼

      參見: 分享幾個在 Windows 與 Linux 常見的編碼問題與解決方案

    2. 預設選用 Unicode 字型,除了比較好看外,可以選擇的字型也比較多!

      Windows 10 繁體中文版的內建的 Windows Sandbox 預設 non-Unicode 字集為 Chinese (Traditional, Taiwan),這會導致你的命令提示字元選擇 MS Gothic 字型,所有 \ 都會顯示成 ¥ 符號,非常不美觀。

  • 系統工具

    choco install openssh unxutils sysinternals jq -y
    
  • 瀏覽器

    choco install googlechrome googlechrome.canary firefox safari brave --ignorechecksums -y
    

    因為透過 Chocolatey 安裝這些瀏覽器,預設都會安裝最新版,因此常有 Checksum Mismatch 的問題出現,所以要加上 --ignorechecksums 參數。

  • Azure CLI

    choco install azure-cli bicep -y
    
  • AWS CLI

    choco install awscli awssamcli -y
    
  • 其他

    choco install notepadplusplus -y
    

相關連結

留言評論