将驱动器号放在路径中

时间:2019-02-21 10:15:55

标签: powershell

$DiskCount = (Get-Disk | Where-Object {$_.BusType -eq "USB"}).Number.Count
if ($DiskCount -eq 1) {
    filter Get-FirstResolvedPath {
       (Get-Disk |
           Where-Object {$_.BusType -eq "USB"} |
           Get-Partition |
           Get-Volume |
           Where-Object {$null -ne $_.DriveLetter}
       ).DriveLetter + ':\' | Join-Path -ChildPath $_ -Resolve -ErrorAction SilentlyContinue
    }
    'Folder\Folder\reg\Start.reg' | Get-FirstResolvedPath
}

是否还有另一种获取文件完整路径的方法,如果该文件存储在USB驱动器上,则该路径是绝对路径,而我们不知道USB磁盘盘符?

1 个答案:

答案 0 :(得分:0)

我喜欢您的解决方案。我会用不同的方式编写它,您可以将其缩短一点:

function Get-ResolvedPath {
  param ([Parameter(ValueFromPipeline=1)]$Path)
  Get-Disk |? BusType -ne USB | Get-Partition |% {Join-Path ($_.DriveLetter+":") $Path -R -EA Silent}
}

'Folder\Folder\reg\Start.reg' | Get-ResolvedPath | select -First 1