Skip to content

Instantly share code, notes, and snippets.

@AfroThundr3007730
Created October 24, 2022 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AfroThundr3007730/0824579071ea3eef2f8fd06d1b0da718 to your computer and use it in GitHub Desktop.
Save AfroThundr3007730/0824579071ea3eef2f8fd06d1b0da718 to your computer and use it in GitHub Desktop.
Edit the metadata header of a VMDK file
Set-StrictMode -Version Latest
function Edit-VMDKHeader {
<# .SYNOPSIS
Edit the metadata header of a VMDK file #>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
Param(
# The VMDK file to edit
[Parameter(Mandatory)]
[string]$VMDKFile
)
$buffer = [byte[]]::new(1024)
$tmpFile = [IO.Path]::Combine([IO.Path]::GetTempPath(), [IO.Path]::GetTempFileName())
$vmdkStream = [IO.File]::Open($VMDKFile, [IO.FileMode]::Open, [IO.FileAccess]::ReadWrite)
$vmdkStream.Position = 512
[void]$vmdkStream.Read($buffer, 0, 1024)
Write-Output 'Dumping VMDK header metadata to:' $tmpFile
$tmpStream = [IO.File]::OpenWrite($tmpFile)
$tmpStream.Write($buffer, 0, 1024)
$tmpStream.Dispose()
Write-Output 'Make necessary changes, then save and close the editor.'
(Start-Process notepad.exe -ArgumentList $tmpFile -PassThru -Wait).WaitForExit()
$tmpStream = [IO.File]::OpenRead($tmpFile)
[void]$tmpStream.Read($buffer, 0, 1024)
$tmpStream.Dispose()
Remove-Item $tmpFile
if ($PSCmdlet.ShouldProcess($VMDKFile, 'Overwrite VMDK header metadata')) {
Write-Output 'Overwriting VMDK header metadata.'
$vmdkStream.Position = 512
$vmdkStream.Write($buffer, 0, 1024)
}
$vmdkStream.Dispose()
}
@AfroThundr3007730
Copy link
Author

A function implementing the commands from this SO answer to edit VMDK metadata.
This was necessary in order to solve this problem when converting VMDK files to VHDX.

@AfroThundr3007730
Copy link
Author

Updated version available in my HelperFunctions module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment