設想

設想

設想

要使用 powershell 自動安裝和初始化 WSL Ubuntu 18.04,我嘗試自動初始化/設定第一個使用者名稱和密碼。但是,當我第一次從 powershell 命令執行 wsl 時,powershell 會轉到 wsl 的 shell,等待使用者手動輸入密碼。

微量元素

我進行了五次不同的嘗試,在 wsl 初始化後將使用者名稱、密碼(以及再次密碼)傳送到輸入提示,這些內容包含在以下 MWE 中。

##############Required for MWE###################
# Enable wsl subsystems for linux (if powershell is ran in admin mode)
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

# Set Tls12 protocol to be able to download the wsl application
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# check to see if ubuntu1804 installation file exists and download the app otherwise
$fileToCheck = "Ubuntu1804.appx"
if (Test-Path $fileToCheck -PathType leaf) 
{"File does Exist"}
else
{Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile Ubuntu1804.appx -UseBasicParsing}

# Actually install the wsl ubuntu 18.04 app
Add-AppxPackage .\Ubuntu1804.appx
Write-Output "Installed the ubuntu18.04"

# backup installation command if the first command did not function properly
invoke-expression -Command "Add-AppxPackage .\Ubuntu1804.appx"
Write-Output "Installed the ubuntu with backup attempt"


##############Actual attempts to initialize ubuntu without prompting for user input###################
Write-Host "Trying to initialize ubuntu"
# Attempt 0: makes it start installing the wsl but hangs prompting user name
#Write-Host "wsl whoami" 
# Attempt 0 conclusion: Starts installing the wsl but then waits on user input

# Attempt 0.1: So would like to pipe a "password | password | username | whoami" in there but that does not work.
#Write-Host "wsl 'somepassword | somepassword | someusername | whoami'"
#Write-Host "wsl somepassword | somepassword | someusername | whoami" 
# Attempt 0.1 conclusion: doesn't work, still dives into the wsl shell and waits on user input


# Attempt 1: does not make it start installing
#$output = bash -c "wsl whoami"
#$output = bash -c "wsl 'somepassword | somepassword | someusername | whoami'" 
# Attempt 1 conclusion: Does not work, requires a user input to start installing (e.g. arrow down) (and then waits on user input).


# Attempt 2: try to prevent the prompt for username by setting default user to root immediatly

# Attempt 2.1: First define path to the installed ubuntu1804.exe
$str1="/Users/"
$str2="/AppData/Local/Microsoft/WindowsApps/ubuntu1804"
$hdd_name=(Get-WmiObject Win32_OperatingSystem).SystemDrive
$username=$env:UserName
[String] $ubuntu1804_path=$hdd_name+$str1+$username+$str2

# Attempt 2.2: Create command to set root as default user
$str1=" config --default-user root"
$set_user=$ubuntu1804_path+$str1

# Attempt 2.3: Create command to set root as default user and execute it
#invoke-expression -Command $set_user 
# Attempt 2.3 conclusion: Doesn't work still asks for username and waits on user input


# Attempt 3: passing a username, password, and password again as one is prompted at the startup
$strA = "test | test | root"
#$output = bash "-c" $strA
# Attempt 3 conclusion: Doesn't work, requires user input to go to the next line (e.g. arrow down)

# Attempt 4: let root be default username
$str1=" install --root"
$set_user=$ubuntu1804_path+$str1
# Attempt 4 conclusion: Doesn't work, requires user input to go to the next line (e.g. arrow down)
invoke-expression -Command $set_user 
# Attempt 4 conclusion: Pending.

Write-Host "Done with setup."

在沒有使用者乾預的情況下,嘗試 0、1、2 和 3 都未能成功自動初始化 WSL Ubuntu 18.04。嘗試的問題寫在評論的結論中。歸結為,wsl 一旦啟動就開始安裝/初始化,但隨後等待使用者在 powershell 視窗中輸入,而不將剩餘的命令傳遞到其中。

問題:

如何從 powershell 執行 WSL Ubuntu 18.04 的無人值守安裝和初始化?

假設

我預先知道 Powershell 中變數中的使用者名稱和密碼。

答案1

不使用Add-AppxPackage安裝 Appx 包,而是使用Expand-Archive cmdlet 將其解壓縮到資料夾中。然後執行ubuntu.exe來配置其餘部分。看WSL:Windows Server 安裝指南以獲得進一步的想法。

答案2

解釋

第四次嘗試使用預設使用者 root 作為參數初始化 wsl (不是ubuntu1804 config --default-user root命令)有效,且不需要輸入任何密碼。

解決方案

以下程式碼自動從 powershell 安裝 WSL Ubuntu 18.04:

##############Downloading and installing the app###################
# Enable wsl subsystems for linux (if powershell is ran in admin mode)
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

# Set Tls12 protocol to be able to download the wsl application
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# check to see if ubuntu1804 installation file exists and download the app otherwise
$fileToCheck = "Ubuntu1804.appx"
if (Test-Path $fileToCheck -PathType leaf) 
{"File does Exist"}
else
{Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1804 -OutFile Ubuntu1804.appx -UseBasicParsing}

# Actually install the wsl ubuntu 18.04 app
Add-AppxPackage .\Ubuntu1804.appx
Write-Output "Installed the ubuntu18.04"

# backup installation command if the first command did not function properly
invoke-expression -Command "Add-AppxPackage .\Ubuntu1804.appx"
Write-Output "Installed the ubuntu with backup attempt"


##############Initializing the wsl ubuntu 18.04 app without requiring user input###################

# First define path to the installed ubuntu1804.exe
$str1="/Users/"
$str2="/AppData/Local/Microsoft/WindowsApps/ubuntu1804"
$hdd_name=(Get-WmiObject Win32_OperatingSystem).SystemDrive
$username=$env:UserName
[String] $ubuntu1804_path=$hdd_name+$str1+$username+$str2

# let root be default username
$str1=" install --root"
$set_user=$ubuntu1804_path+$str1
invoke-expression -Command $set_user 

Write-Host "Done with setup."

相關內容