【问题标题】:How to install Rust using power shell?如何使用 powershell 安装 Rust?
【发布时间】:2022-08-13 23:44:34
【问题描述】:

我想在 Windows 上通过命令行自动安装 rust。它是否是电源外壳并不重要,但我认为电源外壳是最简单的,无需手动安装其他东西。

我找到了https://www.powershellgallery.com/packages/AppVeyorBYOC/1.0.170/Content/scripts%5CWindows%5Cinstall_rust.ps1 但在

 Add-SessionPath \"$env:USERPROFILE\\.cargo\\bin\"

它说Add-SessionPath 是一个不存在的cmdlet。

  • 为什么不像here 描述的那样直接使用rustup?您所要做的就是下载可执行文件并运行它。无论如何,这基本上就是 PowerShell 脚本正在做的事情。
  • @Herohtar 我需要无人值守安装
  • 您需要做的就是运行./rustup-init.exe -y。如果您需要配置不同于默认值的东西,您可以使用其他命令行选项,但它完全无人值守。

标签: windows rust automation


【解决方案1】:

我有同样的错误。我把那行改成了这个,脚本使用了电源外壳 7.

$env:Path = "$env:USERPROFILE\.cargo\bin"

【讨论】:

    【解决方案2】:
    # Make new environment variables available in the current PowerShell session:
    function reload {
       foreach($level in "Machine","User") {
          [Environment]::GetEnvironmentVariables($level).GetEnumerator() | % {
             # For Path variables, append the new values, if they're not already in there
             if($_.Name -match 'Path$') { 
                $_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select -unique) -join ';'
             }
             $_
          } | Set-Content -Path { "Env:$($_.Name)" }
       }
    }
    Write-Host "Installing Rust..." -ForegroundColor Cyan
    $exePath = "$env:TEMP\rustup-init.exe"
    
    Write-Host "Downloading..."
    (New-Object Net.WebClient).DownloadFile('https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe', $exePath)
    
    Write-Host "Installing..."
    cmd /c start /wait $exePath -y
    Remove-Item $exePath
    
    $addPath = "$env:USERPROFILE\.cargo\bin"
    [Environment]::SetEnvironmentVariable
         ($addPath, $env:Path, [System.EnvironmentVariableTarget]::Machine)
    
    reload
    
    cargo --version
    rustup --version
    rustc --version
    

    【讨论】:

      猜你喜欢
      • 2013-07-23
      • 1970-01-01
      • 2022-08-13
      • 1970-01-01
      • 2018-02-23
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 2019-10-28
      相关资源
      最近更新 更多