gpt4 book ai didi

powershell - Powershell v4 和 v5 之间 Join-Path cmdlet 的行为

转载 作者:行者123 更新时间:2023-12-04 01:36:48 25 4
gpt4 key购买 nike

我正在一台机器上执行以下命令 G:驱动器不存在:

Join-Path "G:\" "abc.txt"

在 Powershell v5 中,这会按预期返回“G:\abc.txt”。我只是想加入子路径而不是验证它的存在。
另一方面,在 Powershell v4 中,它失败并显示以下错误:
Join-Path : Cannot find drive. A drive with the name 'G' does not exist.
At line:1 char:1
+ Join-Path "G:\" "abc.txt"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (G:String) [Join-Path], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.JoinPathCommand

由于某些原因,我一直坚持使用 Powershell v4,现在无法迁移到 Powershell v5。是否有任何开箱即用的解决方案来简单地加入 Powershell v4 中的子路径,或者我是否需要创建自定义解决方案?

最佳答案

为了避免存在检查,您可以使用 Path.Combine() :

PS C:\> [System.IO.Path]::Combine('G:\','nonexistingfile.txt')
G:\nonexistingfile.txt

关于powershell - Powershell v4 和 v5 之间 Join-Path cmdlet 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33303175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com