要检索文件的部分(块),您可以创建System.IO.BinaryReader
一个方便的花花公子Read( [Byte[]] buffer, [int] offset, [int] length)
方法。这是一个使它变得容易的函数:
function Read-Bytes {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[string] $Path
, [Parameter(Mandatory = $true, Position = 1)]
[int] $Offset
, [Parameter(Mandatory = $true, Position = 2)]
[int] $Size
)
if (!(Test-Path -Path $Path)) {
throw ('Could not locate file: {0}' -f $Path);
}
# Initialize a byte array to hold the buffer
$Buffer = [Byte[]]@(0)*$Size;
# Get a reference to the file
$FileStream = (Get-Item -Path $Path).OpenRead();
if ($Offset -lt $FileStream.Length) {
$FileStream.Position = $Offset;
Write-Debug -Message ('Set FileStream position to {0}' -f $Offset);
}
else {
throw ('Failed to set $FileStream offset to {0}' -f $Offset);
}
$ReadResult = $FileStream.Read($Buffer, 0, $Size);
$FileStream.Close();
# Write buffer to PowerShell pipeline
Write-Output -InputObject $Buffer;
}
Read-Bytes -Path C:\Windows\System32\KBDIT142.DLL -Size 10 -Offset 90;