【问题标题】:How can I ReadAllByte with unique Volume ID in PowerShell?如何在 PowerShell 中使用唯一卷 ID 读取所有字节?
【发布时间】:2017-02-25 10:27:55
【问题描述】:

我试过这个命令,它工作正常:

$path = "C:\sometext.txt"
[System.IO.File]::ReadAllBytes("$path")

但是当我尝试在 PowerShell 中读取具有唯一 ID 的文件的所有字节时,它不起作用:

$path =  "\\.\Volume{3386ab07-0000-0000-0000-501f00000000}\sometext.txt"
[System.IO.File]::ReadAllBytes("$path")

我使用唯一 ID:

$listdrive = ""
$drivelistuniqueID = Get-Volume 
foreach ($item in $drivelistuniqueID)
{
    $listdrive += $item.UniqueId + "DriveLetterSplIttEr"
}
$listdrive=$listdrive.Replace("?",".")
$listdrive

如果我不将? 替换为.,PowerShell 会说:

使用“1”参数调用“ReadAllBytes”的异常:“非法字符
小路。”
在行:3 字符:1
+ [System.IO.File]::ReadAllBytes("$path")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

知道如何解决这个错误吗? 我只需要读取带有 GUID(唯一 ID)的文件。

【问题讨论】:

  • 那么问题是什么? “不起作用”很模糊,它会抛出任何错误吗?
  • @Mathias R. Jessen:我写错了。异常调用...你也可以试试
  • 是的,但是您声明当您将? 替换为. 时它也不起作用 - 在这种情况下会发生什么?
  • @Mathias R. Jessen :powershell 说路径中有非法字符
  • 所以以\\?\\\.\ 开头的路径都会出现同样的错误?

标签: .net powershell powershell-3.0 powershell-4.0


【解决方案1】:

实际上,当您想读取所有字节时,您可以直接进行。在您的计算机上进行测试,使用 GUID 制作新文件,然后尝试打开文件。你会看到你不能。如果你想使用 readallbyte,你应该钩住它。

【讨论】:

    【解决方案2】:

    您在那里混合文件和设备namespaces,我什至不确定System.IO.File 方法是否支持命名空间路径。你到底想达到什么目的?如果您想从已安装的卷中读取文件,应该可以这样:

    $guid  = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
    $drive = (Get-Volume | Where-Object {$_.ObjectId -like "*$guid*"}).DriveLetter
    $path  = Join-Path "${drive}:" 'sometext.txt'
    
    $data = [IO.File]::ReadAllBytes($path)
    

    如果驱动器尚未安装,您可能需要先安装它。

    Get-Volume |
      Where-Object {$_.ObjectId -like "*$guid*"} |
      Get-Partition |
      Set-Partition -NewDriveLetter X
    

    我认为您无法通过System.IO.File 访问未安装卷上的文件。

    【讨论】:

    • 当我写 get-childitem "\\.\Volume{3386ab07-0000-0000-0000-501f00000000}\sometext.txt" 我可以访问目录
    猜你喜欢
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 2017-08-07
    • 2014-04-23
    • 1970-01-01
    • 2012-11-30
    相关资源
    最近更新 更多