我需要测试包含路径的两个字符串,看它们是否指向相同的目录。
例如,在比较C:\Windows
和C:\Windows\
时,简单地使用字符串比较失败。
这个问题可以通过按照this StackOverflow问题使用Join-Path来解决,但它仍然省略了其他事情:
例如,\\server\share
有时可以表示为UNC\server\share
或\\<ip>\share
有没有一种适当的方法可以在不使用变通方法的情况下检查这一点?
编辑:我已经在PowerShell模块PSHelperTools中实现了答案中的代码,可以使用Install-Module PSHelperTools
下载
2条答案
按热度按时间azpvetkf1#
目前,我将此作为一种解决办法:
jecbmhm32#
有一种正确的方法可以做到这一点,就像你在这个混乱的https://stackoverflow.com/a/74201349/1964796中看到的那样
由于以下原因,没有一个答案是完全可以接受的。
出于以下原因,我列出了这里列出的每种方法的预期结果,如下所示:
正确答案是使用
GetUnresolvedProviderPathFromPSPath
,但它不能单独工作,如果您尝试直接使用它,您会得到这些结果。从这个答案https://stackoverflow.com/a/52157943/1964796。因此,我们需要做的是去除驱动程序/提供程序/UNC,然后使用
GetUnresolvedProviderPathFromPSPath
,然后将驱动程序/提供程序/UNC放回原处。不幸的是,GetUPPFP取决于当前的pwd
状态,但我们至少没有改变它。来源和测试:https://gist.github.com/Luiz-Monad/d5aea290087a89c070da6eec84b33742#file-normalize-path-ps-md