如何在 PowerShell 中读取具有唯一卷 ID 的 ReadAllByte?

How can I ReadAllByte with unique Volume ID in PowerShell?

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

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

但是当我尝试使用唯一 ID 读取 PowerShell 中文件的所有字节时,就像这段代码一样,它不起作用:

$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 会说:

Exception calling "ReadAllBytes" with "1" argument(s): "Illegal characters in
path."
At line:3 char:1
+ [System.IO.File]::ReadAllBytes("$path")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

知道如何修复此错误吗? 我只需要读取带有 GUID(唯一 ID)的文件。

你在那里混合了文件和设备 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 在未安装的卷上访问文件。

实际上当你想读取所有字节时你可以直接这样做。在您的计算机上对此进行测试,使用 GUID 创建新文件,然后尝试打开文件。你会发现你做不到。如果你想使用 readallbyte,你应该挂钩这个。