【问题标题】:Why does VS Code PowerShell terminal map a new drive?为什么 VS Code PowerShell 终端映射新驱动器?
【发布时间】:2020-07-07 21:05:15
【问题描述】:

我在 Visual Studio Code 中制作了一个使用命令 Get-PSDrive 的 PowerShell 脚本,令我惊讶的是,在使用 VS Code 时,似乎出现了一个与我的 C:\ 驱动器相同的名为 Temp 的新驱动器。

据我所知,这个结果让我大吃一惊,除了我的主要 C:\ 驱动器外,我只连接了 2 个其他驱动器。我试图在其他终端上复制它,但除了 PowerShell 7 之外没有成功:

Windows 终端 PowerShell 5.1

PowerShell 5.1

PowerShell 5.1 (86x)

PowerShell 7 (86x)

Windows 终端 PowerShell 7


当我看到它被 PwSh 7 复制时,我决定使用 $host 变量检查每个 PowerShell 的版本,我看到了更意想不到的东西:

Version          : 5.1.19041.1

Windows 终端 PowerShell 5.1

Version          : 5.1.19041.1

PowerShell 5.1

Version          : 5.1.19041.1

PowerShell 5.1 (86x)

Version          : 7.0.2

PowerShell 7 (86x)

Version          : 7.0.2

Windows 终端 PowerShell 7

这一切看起来都很正常,但是当我检查 VSCode $host 时,我得到了结果

Version          : 2020.6.0

导致输出差异的原因是什么

Get-PSDrive | Where-Object {$_.Provider.Name -eq "FileSystem"}

在 PwSh 7、PowerShell 5.1 和 VS Code PwSh 之间?

【问题讨论】:

  • $Host 变量不一定是 PoSh/Pwsh 的版本。它是主机/控制台信息,通常是 ps 版本。您可能需要交叉检查.Version.Name。 ///// 我的 PS7 安装在控制台和 VSCode 控制台中显示 Temp: 驱动器。我认为这是 PS7 默认做的事情。我怀疑这是一种方便的功能,可以处理查找操作系统/用户临时目录的不同方法。
  • 我从$host 变量中的Version 部分获取了这个
  • 是的,这不是总是 PoSh/Pwsh 版本号。 有时它只是控制台主机版本。 PS版使用$PSVersionTable会更安全。
  • 根据这个 >>> 提供一种与平台无关的方法来确定温度。目录,例如,通过自动变量。 · 问题 #4216 · PowerShell/PowerShell — github.com/PowerShell/PowerShell/issues/4216

标签: powershell visual-studio-code powershell-5.0 powershell-7.0


【解决方案1】:

Temp: 驱动器不是“与我的C:\ 驱动器相同的新驱动器”;它的Root$Env:Temp,而不是C:\

根据PowerShell Team May 2020 Update,它实际上描述了PowerShell 7.1,这是在PowerShell 7.0中添加的...

在 PowerShell 7.0 中,我们添加了 temp: PSDrive。这适用于所有平台并自动映射到您的用户临时路径。

顺便说一句,你用来截屏的命令可以简化为Get-PSDrive -PSProvider FileSystem

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-18
    • 2022-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多