【问题标题】:Put the drive letter in the path将驱动器号放在路径中
【发布时间】:2019-02-21 10:15:55
【问题描述】:
$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
}

有没有其他方法可以获取文件的完整路径,如果存储在U盘上,路径是绝对的,我们不知道U盘盘符?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    我喜欢你的解决方案。我会写不同的,你可以缩短一点:

    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
    

    【讨论】:

    • 我知道 -first 1,但如果某个文件 (start.reg) 在“seecond”驱动器上,脚本将无法正确运行。所以我在寻找合理的方法......
    • @farag 指定“运行不正确”?我想不出这不起作用的原因。
    • 非常感谢!但是,如果我愿意,如何使用它 (Get-Disk | Where-Object {$_.BusType -eq "USB"} | Get-Partition).DriveLetter 构造?
    • @farag (Get-Disk | Where-Object {$_.BusType -eq "USB"} | Get-Partition).DriveLetter |% {Join-Path ($_+":") $Path -R -EA Silent} 但我不明白你为什么要使用它。
    猜你喜欢
    • 2016-02-15
    • 1970-01-01
    • 2010-10-08
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多