Skip to content

Instantly share code, notes, and snippets.

@alkuzad
Last active November 1, 2023 09:01
Show Gist options
  • Save alkuzad/438993ecfea601cd56536bc360e5f581 to your computer and use it in GitHub Desktop.
Save alkuzad/438993ecfea601cd56536bc360e5f581 to your computer and use it in GitHub Desktop.
Restart script to restart WSL, DockerDesktop and LxssManager. This script should unblock blocked wsl/docker-desktop.
# MIT License
# Copyright (c) 2023 Dawid Goslawski
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$CommandLine = "-NoExit -File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
Exit
}
}
if (-not (Get-InstalledModule -Name Wsl -ErrorAction SilentlyContinue)) {
Install-Module -Name Wsl -Force
}
$ErrorActionPreference = "Stop"
$LxssManagerId = (get-wmiobject Win32_service | Where-Object {$_.Started -eq "True" -and $_.ServiceType -eq "Share Process" -and $_.Name -eq "LxssManager"} | Select-Object -ExpandProperty ProcessId)
$WSLIds = (get-process -ProcessName "wsl*" | Select-Object -ExpandProperty ID)
$DockerIds = (get-process -ProcessName "*docker*" | Select-Object -ExpandProperty ID)
for ($i = 0; $i -lt 15; $i++) {
Write-Host "Cancel docker desktop restart notification prompt"
}
foreach ($id in $WSLIds + $DockerIds + $LxssManagerId) {
try {
Stop-Process -Id "$id" -Force -ErrorAction Stop
}
catch {
if (Get-Process -Id "$id" -ErrorAction SilentlyContinue) {
try {
Write-Host "Trying harder to stop process $id"
wmic process where "ProcessID=$id" delete
} catch {
Write-Host "Could not destroy process $id :"
Write-Host $_
}
}
}
}
Restart-Service com.docker.service
Restart-Service LxssManager
$wsl_distros = (Get-WslDistribution | Where-Object { $_.Default -eq $true -or $_.Name -eq "docker-desktop" } | Select-Object -ExpandProperty Name)
foreach ($distroItem in $wsl_distros) {
# Start the distro silently by running exit process
wsl --distribution $distroItem "exit"
}
& "C:\Program Files\Docker\Docker\Docker Desktop.exe"
Write-Host "Docker Desktop will appear under a minute, it takes some time to start again"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment