saftargholi
saftargholi

Reputation: 980

How can I ReadAllByte with unique Volume ID in PowerShell?

I tried this command, it's working fine:

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

But when I tried to read all bytes of file in PowerShell with unique ID like this code it doesn't work:

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

I take unique ID with:

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

If I don't replace ? with . PowerShell says:

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

Any idea about how I could fix this error? I need only read file with GUID (Unique ID).

Upvotes: 0

Views: 581

Answers (2)

user7160191
user7160191

Reputation:

actually when you want to read all byte you can do it directly . test this on your computer make new file wile using GUID then try to open file . you will see you can't . if you want to use readallbyte you should hook this.

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

You're mixing file and device namespaces there, and I'm not even sure if System.IO.File methods support namespaced paths at all. What are you trying to achieve anyway? If you want to read a file from a mounted volume, something like this should work:

$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)

If the drive hasn't been mounted you probably need to mount it first.

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

I don't think you can access files via System.IO.File on an unmounted volume.

Upvotes: 0

Related Questions