param( [Parameter(Mandatory = $true)] [int] $ProcessId, [Parameter(Mandatory = $true)] [UInt64] $Address, [Parameter(Mandatory = $true)] [int] $Length, [Parameter(Mandatory = $true)] [string] $OutputPath ) Add-Type -TypeDefinition @' using System; using System.Runtime.InteropServices; public static class ProcessMemoryDumpNative { [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr OpenProcess(uint access, bool inheritHandle, int processId); [DllImport("kernel32.dll", SetLastError = true)] public static extern bool ReadProcessMemory(IntPtr process, UIntPtr address, byte[] buffer, UIntPtr size, out UIntPtr bytesRead); [DllImport("kernel32.dll")] public static extern bool CloseHandle(IntPtr handle); } '@ $handle = [ProcessMemoryDumpNative]::OpenProcess(0x410, $false, $ProcessId) if ($handle -eq [IntPtr]::Zero) { throw "OpenProcess failed: $([Runtime.InteropServices.Marshal]::GetLastWin32Error())" } try { $buffer = [byte[]]::new($Length) $bytesRead = [UIntPtr]::Zero if (-not [ProcessMemoryDumpNative]::ReadProcessMemory( $handle, [UIntPtr]::new($Address), $buffer, [UIntPtr]::new([UInt64]$Length), [ref]$bytesRead)) { throw "ReadProcessMemory failed: $([Runtime.InteropServices.Marshal]::GetLastWin32Error())" } if ($bytesRead.ToUInt64() -ne [UInt64]$Length) { throw "Short read: requested $Length bytes, got $($bytesRead.ToUInt64())" } [IO.File]::WriteAllBytes((Join-Path (Get-Location) $OutputPath), $buffer) } finally { [void][ProcessMemoryDumpNative]::CloseHandle($handle) }