Azure コラム 第8回:Windows VM 全自動日本語化

Microsoft

2022.02.08

Azure コラム第8回は番外編的に 「Windows VM 全自動日本語化」をお届けします。

はじめに

久々に VM を新規構築したところ相変わらず日本語化が面倒だったので全自動化してしまおう、という営みの成果になります。

前提条件

  • Windows Server 2022のみで動作確認を行っています
    • 多分他のバージョンでも動くのではないかとは思います
  • ISOはLanguage and region Features on Demand (FOD) イメージです
    • スクリプト内のパスの変更が必要ですがWindows Server 2022のイメージも使用可能です
  • VMサイズはStandard_B2msで動作確認を行っています
    • Temporary Storageに6GBのISOをダウンロードしますのでご留意ください

環境構築

VMからHTTPSでアクセスできる場所にISOを置くだけです。弊社環境ではストレージアカウントの静的Webサイトにプライベートエンドポイントを付けて公開しています。

操作手順

  • 1. スクリプト冒頭のパラメータを環境に合わせて変更
  • 2. AzureポータルでVMを開く→実行コマンド→RunPowerShellScript→スクリプトをコピペ→実行

    実行コマンド

  • 3. リソース正常性で再起動2回の足跡の後に利用可能となることを確認

    リソース正常性

  • 4. 完了

処理フロー

こんな感じで動きます。

  • 1. ISOダウンロード
  • 2. Language Packのインストール
  • 3. Features on Demandのインストール
  • 4. 日本語化設定①
  • 5. RunOnceの仕込み
  • 6. AutoLogonの仕込み
  • 7. 再起動
  • 8. AutoLogon
  • 9. RunOnce起動
    • 1. 日本語化設定②
    • 2. AutoLogon解除
    • 3. 再起動
  • 10. 完了

サンプルスクリプト

$workdir = "D:\"
$isouri = "[ISOのURI]"
$autologonuser = "[自動ログオンユーザー]"
$autologonpass = "[自動ログオンユーザーパスワード]"

$log = $workdir + "setuplang.log"

# Download LanguagePack
$iso = $workdir + "lang.iso"
if(Test-Path $iso){ Remove-Item $iso -force }
try
{
    $ProgressPreference = "SilentlyContinue"
    Invoke-WebRequest -Uri $isouri -OutFile $iso
    $ProgressPreference = "Continue"
    "200" | Out-File -Append -FilePath $log
    "OK" | Out-File -Append -FilePath $log
}
catch
{
    $_.Exception.Response.StatusCode.value__ | Out-File -Append -FilePath $log
    $_.Exception.Response.StatusDescription | Out-File -Append -FilePath $log
    throw
}

$mountResult = Mount-DiskImage $iso -PassThru
$mountResult | Out-File -Append -FilePath $log
$driveLetter = ($mountResult | Get-Volume).DriveLetter

# Install Language Pack
$lppath = $driveLetter + ":\LanguagesAndOptionalFeatures\Microsoft-Windows-Server-Language-Pack_x64_ja-jp.cab"
dism /online /add-package /packagepath:$lppath >> $log

# Install Features on Demand
$fodpath = $driveLetter + ":\LanguagesAndOptionalFeatures\Microsoft-Windows-LanguageFeatures-Basic-ja-jp-Package~31bf3856ad364e35~amd64~~.cab"
dism /online /add-package /packagepath:$fodpath >> $log

Dismount-DiskImage $iso | Out-File -Append -FilePath $log
Remove-Item $iso -force

Set-WinSystemLocale -SystemLocale ja-JP
Set-TimeZone -Id "Tokyo Standard Time"

# RunOnce
$runoncecmd = $workdir + "setuplang.ps1"
$unattendxml = $workdir + "unattend.xml"
$autologonreg = "registry::\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"

$xml = @"
<gs:GlobalizationServices xmlns:gs="urn:longhornGlobalizationUnattend">
<gs:UserList>
<gs:User UserID="Current" CopySettingsToDefaultUserAcct="true" CopySettingsToSystemAcct="true"/>
</gs:UserList>
</gs:GlobalizationServices>
"@
$xml | Out-File -FilePath $unattendxml

$cmd = @"
Set-WinHomeLocation -GeoId 122
Set-WinUserLanguageList -LanguageList ja-JP -Force
Set-WinUILanguageOverride -Language ja-JP
Set-WinCultureFromLanguageListOptOut -OptOut 0
Set-WinDefaultInputMethodOverride -InputTip "0411:00000411"
Set-ItemProperty "registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\i8042prt\Parameters" -Name "LayerDriver JPN" -Value "kbd106.dll"
control.exe "intl.cpl,,/f:```"$unattendxml```""
Set-ItemProperty -Path "$autologonreg" -Name "AutoAdminLogon" -Value "0"
Set-ItemProperty -Path "$autologonreg" -Name "DefaultUserName" -Value ""
Set-ItemProperty -Path "$autologonreg" -Name "DefaultPassword" -Value ""
"Restart Computer" | Out-File -Append -FilePath "$log"
Restart-Computer -Force
"@
$cmd | Out-File -FilePath $runoncecmd

Set-ItemProperty `
	-Path "registry::\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" `
	-Name "SetupLang" `
	-Value "powershell.exe -ExecutionPolicy RemoteSigned -file `"$runoncecmd`""

"Set RunOnce" | Out-File -Append -FilePath $log

# AutoLogon
Set-ItemProperty -Path $autologonreg -Name "AutoAdminLogon" -Value "1"
Set-ItemProperty -Path $autologonreg -Name "DefaultUserName" -Value $autologonuser
Set-ItemProperty -Path $autologonreg -Name "DefaultPassword" -Value $autologonpass

"Set AutoLogon" | Out-File -Append -FilePath $log

"Restart Computer" | Out-File -Append -FilePath $log
Restart-Computer -Force

おわりに

Azureコラム第8回いかがでしたでしょうか。
いや~結構苦戦しましたけど出来上がったときは爽快でした。しょーもないもんですが自分で手を動かすのはやっぱ良いですね。

こういう痒い所に手が届く系の情報もどんどんご提供していきたいと思います。

※文章中の商品名、会社名、団体名は、各社の商標または登録商標です。

Azure コラム 第8回:Windows VM 全自動日本語化