我同意这是一个错误,但是:
因此,为了清除此类错误值,您可以按文件的属性.Target
过滤文件:.LinkType
Get-ChildItem | Where-Object LinkType -eq SymbolicLink # now, .Targets are valid
另外,如果您正在寻找一种将基于替代驱动器的路径转换为其底层物理路径的方法:
不幸的是,似乎Convert-Path
也不知道替代驱动器(使用创建) - 即使在 PowerShell 7.0 中也不知道 - 所以你必须推出自己的翻译命令:Get-PSDrive
subst.exe
& {
$fullName = Convert-Path -LiteralPath $args[0]
$drive = Split-Path -Qualifier $fullName
if ($drive.Length -eq 2 -and ($substDef = @(subst.exe) -match "^$drive")) {
Join-Path ($substDef -split ' ', 3)[-1] $fullName.Substring($drive.Length)
} else {
$fullName
}
} 'K:\'
以上内容应C:\test\
在您的情况下返回。
注意:由于使用Convert-Path
,以上仅适用于现有路径;使其支持不存在的路径需要更多的工作(见下文)。
请注意,长期存在的GitHub 功能请求 #2993要求增强Convert-Path
以也可以使用不存在的路径。
在此期间,这里有填补空白的高级功能。Convert-PathEx
定义后,您可以改为执行以下操作:
PS> Convert-PathEx K:\
C:\test\
function Convert-PathEx {
<#
.SYNOPSIS
Converts file-system paths to absolute, native paths.
.DESCRIPTION
An enhanced version of Convert-Path, which, however only supports *literal* paths.
For wildcard expansion, pipe from Get-ChildItem or Get-Item.
The enhancements are:
* Support for non-existent paths.
* On Windows, support for translating paths based on substituted drives
(created with subst.exe) to physical paths.
#>
[CmdletBinding(PositionalBinding = $false)]
param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('PSPath', 'LP')]
[string[]] $LiteralPath
)
begin {
$isWin = $env:OS -eq 'Windows_NT'
# Helper function for ignoring .Substring() exceptions.
function fromPos ($str, $ndx) {
try { return $str.Substring($ndx) } catch { return '' }
}
}
process {
foreach ($path in $LiteralPath) {
$path = $path -replace '^.+::' # strip any PS provider prefix, such as 'FileSystem::' or 'Microsoft.PowerShell.Core\FileSystem::'
# Analyze drive information.
$driveSpec = Split-Path -ErrorAction Ignore -Qualifier $path
$driveObj = if ($driveSpec) { (Get-PSDrive -ErrorAction Ignore -PSProvider FileSystem -Name $driveSpec.Substring(0, $driveSpec.Length - 1)) | Select-Object -First 1 } # !! Get-PSDrive can report *case-sensitive variations* of the same drive, so we ensure we only get *one* object back.
if ($driveSpec -and -not $driveObj) {
Write-Error "Path has unknown file-system drive: $path" -Category InvalidArgument
continue
}
$rest = if ($driveObj) { fromPos $path $driveSpec.Length } else { $path }
$startsFromRoot = $rest -match '^[\\/]'
if ($startsFromRoot) { $rest = fromPos $rest 1 } # Strip the initial separator, so that [IO.Path]::Combine() works correctly (with an initial "\" or "/", it ignores attempts to prepend a drive).
$isAbsolute = $startsFromRoot -and ($driveObj -or -not $isWin) # /... paths on Unix are absolute paths.
$fullName =
if ($isAbsolute) {
if ($driveObj) {
# Prepend the path underlying the drive.
[IO.Path]::Combine($driveObj.Root, $rest)
} else {
# Unix: Already a full, native path - pass it through.
$path
}
} else {
# Non-absolute path, which can have one three forms:
# relative: "foo", "./foo"
# drive-qualified relative (rare): "c:foo"
# Windows drive-qualified relative (rare): "c:foo"
if ($startsFromRoot) {
[IO.Path]::Combine($PWD.Drive.Root, $rest)
} elseif ($driveObj) {
# drive-qualified relative path: prepend the current dir *on the targeted drive*.
# Note: .CurrentLocation is the location relative to the drive root, *wihtout* an initial "\" or "/"
[IO.Path]::Combine($driveObj.Root, $driveObj.CurrentLocation, $rest)
} else {
# relative path, prepend the provider-native $PWD path.
[IO.Path]::Combine($PWD.ProviderPath, $rest)
}
}
# On Windows: Also check if the path is defined in terms of a
# *substituted* drive (created with `subst.exe`) and translate
# it to the underlying path.
if ($isWin) {
# Note: [IO.Path]::GetPathRoot() only works with single-letter drives, which is all we're interested in here.
# Also, it *includes a trailing separator*, so skipping the length of $diveSpec.Length works correctly with [IO.Path]::Combine().
$driveSpec = [IO.Path]::GetPathRoot($fullName)
if ($driveSpec -and ($substDef = @(subst.exe) -like "$driveSpec*")) {
$fullName = [IO.Path]::Combine(($substDef -split ' ', 3)[-1], (fromPos $fullName $driveSpec.Length))
}
}
# Finally, now that we have a native path, we can use [IO.Path]::GetFullPath() in order
# to *normalize* paths with components such as "./" and ".."
[IO.Path]::GetFullPath($fullName)
} # foreach
}
}