WSL2 Installation als Powershell-Skript

Etwas ungewöhnlich für diesen doch eher Linux bezogenen Blog, behandelt dieser Beitrag primär das Thema Windows und die Powershell. Genauer gesagt geht es um ein Powershell-Skript, welches eine automatische WSL2 Installation (unter Windows) vornimmt. Da die installierte WSL2 Distribution ein Ubuntu 22.04 darstellt, wäre aber quasi wieder die Brücke zu Linux geschlagen. 😉

Es folgt der Code. Ein paar erklärende Worte dazu gibt es dann im Anschluss:

$wsl_feature_restart_needed = $false
$vmp_feature_restart_needed = $false
$wsl_feature_enabled = (Get-WindowsOptionalFeature -FeatureName Microsoft-Windows-Subsystem-Linux -Online).State -eq 'Enabled'
$vmp_feature_enabled = (Get-WindowsOptionalFeature -FeatureName VirtualMachinePlatform -Online).State -eq 'Enabled'

Write-warning "Checking if Microsoft-Windows-Subsystem-Linux feature is enabled..."

if ($wsl_feature_enabled) {
  Write-host "Microsoft-Windows-Subsystem-Linux feature is already enabled"
  sleep 5;
}
  else {
    Write-host "Microsoft-Windows-Subsystem-Linux feature is not enabled. Enabling it now!"
    Enable-WindowsOptionalFeature -FeatureName Microsoft-Windows-Subsystem-Linux -Online -NoRestart -OutVariable results
    $wsl_feature_restart_needed = $results.RestartNeeded
  }

Write-warning "Checking if VirtualMachinePlatform feature is enabled..."

if ($vmpfeature_enabled) {
  Write-host "VirtualMachinePlatform feature is already enabled"
  sleep 5;
}
else {
  Write-host "VirtualMachinePlatform feature is not installed. Enabling it now!"
  Enable-WindowsOptionalFeature -FeatureName VirtualMachinePlatform -Online -NoRestart -OutVariable results
  $vmp_feature_restart_needed = $results.RestartNeeded
}

if ($wsl_feature_restart_needed -or $vmp_feature_restart_needed) {
    Write-warning "Feature was activated and a restart is required. Do you want to proceed?"

  while ($true) {
    $reboot = Read-Host -Prompt 'Type (Y)es for restarting the machine or (N)o to stop this script'
    if ($reboot -eq "y" -or $reboot -eq "yes") {
      Write-warning "Machine is going to reboot in 10 seconds!"
      Write-warning "Please rerun this script afterwards."
      sleep 10
      Restart-Computer
    }
    elseif ($reboot -eq "n" -or $reboot -eq "no") {
      Exit
    }
    else {
      Write-warning "Unknown input. Please type (Y)es for rebooting or (N)o to stop this script"
    }
  }
}

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

### Installation of Ubuntu 22.04
Write-warning "Installing Ubuntu 22.04 for WSL"
(New-Object Net.WebClient).DownloadFile('https://aka.ms/wslubuntu2204', "C:\windows\temp\Ubuntu2204_x64.zip")
Expand-Archive -Path "C:\windows\temp\Ubuntu2204_x64.zip" -DestinationPath "C:\windows\temp\Ubuntu2204_x64" -Force
Remove-Item "C:\windows\temp\Ubuntu2204_x64.zip"
Rename-Item -Path C:\Windows\Temp\Ubuntu2204_x64\Ubuntu_2204.1.7.0_x64.appx -NewName C:\Windows\Temp\Ubuntu2204_x64\Ubuntu_2204.1.7.0_x64.zip
Expand-Archive -Path "C:\Windows\temp\Ubuntu2204_x64\Ubuntu_2204.1.7.0_x64.zip" -DestinationPath "C:\windows\temp\Ubuntu2204_x64" -Force
$ubuntuExe = "C:\windows\temp\Ubuntu2204_x64\ubuntu.exe"
. $ubuntuExe install --root

### Create local administrator
. $ubuntuExe run adduser wsladmin --gecos `"First,Last,RoomNumber,WorkPhone,HomePhone`" --disabled-password
. $ubuntuExe run "echo 'wsladmin:Geheimes_Passwort' | sudo chpasswd"
. $ubuntuExe config --default-user wsladmin
wsl -d Ubuntu -e bash -c 'usermod -aG sudo wsladmin'

### Update/Upgrade APT packages
wsl -d Ubuntu -e bash -c '/usr/bin/sudo apt-get update'
wsl -d Ubuntu -e bash -c '/usr/bin/sudo apt-get -y dist-upgrade'

### Remove temporary data
Remove-Item -Recurse -Force C:\windows\temp\Ubuntu2204_x64\

Dieses Skript fragt das System nach den notwendigen optionalen Features Microsoft-Windows-Subsystem-Linux und VirtualMachinePlatform ab und installiert diese, sofern noch nicht vorhanden. Anschließend wird ein Neustart durchgeführt.
Wird dieses Skript nach dem Neustart anschießend erneut ausgeführt, startet die eigentliche Installation von WSL Ubuntu 22.04. Bitte bedenkt, dass im Zuge des Downloads ein Ubuntu 22.04 in der Version 1.7.0 heruntergeladen und entpackt wird. Sollte sich diese Version im Laufe der Zeit ändern, ist der entsprechende Pfad im Skript anzupassen (Zeile 57-58).
Anschließend wird der Benutzer wsladmin mit dem Passwort Geheimes_Passwort angelegt. Passt diese Parameter, ebenso wie die Informationen zu dem Benutzer entsprechend eurer Werte an (Zeile 63-66).
Zuletzt wird noch routinemäßig ein System-Update durchgeführt. Danach ist die Installation bereits verwendbar.

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Nach oben scrollen