2024. december 4., szerda

WSL Idiocracy 1. - Check if installed

Currently I'm writing an automated install script for Azure DevOps self-hosted Linux agent running inside a WSL2 environment on my local Windows machine. Yes, I know the idea itself is weird in the Microsoft's ClickOps word, but I'm a DevOps engineer so I will force it to work.
On the road achieving it there is some idiotic ideas of the WSL what I want to write about.

My first thought, you can check the WSL installation status from in the Windows features list. It can be achieved from PowerShell:


Just guess the WSL is installed on my PC, or not!
According to the DISM it is not installed.
But if I list the running Linux systems:


My WSL based AZDO agent is already running.
How this is possible? The DISM is not the only way to install WSL. You can install it with the following command:

wsl --install --no-distribution --web-download

The internet sources suggest to use wsl -l -v but that just show the running Linux instances. If you install it with the command above, the list will be empty.
What I figured out is the following:

$WSL_STATUS = wsl --status
if($WSL_STATUS) {
    Write-Host "WSL already installed"
} else {
    Write-Host "WSL not installed"
}

Why? The status will be empty if not installed and will report the WSL default version and other settings if installed.
Why this is required? The installation of the WSL engine itself require reboot. As I'm writing an automated script it is crucial to know if I need reboot or not.

Continued...