AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1569186
Accepted
a.t.
a.t.
Asked: 2020-07-17 07:45:26 +0800 CST2020-07-17 07:45:26 +0800 CST 2020-07-17 07:45:26 +0800 CST

从 powershell 安装无人值守的 WSL Ubuntu 18.04

  • 772

设想

为了使用 powershell 自动安装和初始化 WSL Ubuntu 18.04,我正在尝试自动初始化/设置第一个用户名和密码。但是,当我第一次从 powershell 命令运行 wsl 时,powershell 会转到 wsl 的 shell,等待用户手动输入密码。

MWE

在 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 中变量中的用户名和密码。

powershell ubuntu
  • 2 2 个回答
  • 1206 Views

2 个回答

  • Voted
  1. Best Answer
    a.t.
    2020-07-17T08:12:44+08:002020-07-17T08:12:44+08:00

    解释

    第 4 次尝试使用默认用户 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."
    
    • 1
  2. Biswapriyo
    2020-07-17T08:12:50+08:002020-07-17T08:12:50+08:00

    不要使用Add-AppxPackage安装 Appx 包,而是使用Expand-Archive cmdlet 将其解压缩到文件夹中。然后执行ubuntu.exe配置其余部分。有关更多想法,请参阅WSL:Windows Server 安装指南。

    • 1

相关问题

  • Powershell 和正则表达式:Notepad++“保存时备份”文件列表。编辑名称,按上次写入时间排序

  • 将前景颜色添加到 Powershell 配置文件?

  • vmwared 共享文件夹不工作

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve