Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$SFTCode = @"
[DllImport("kernel32")] public static extern uint EnumSystemFirmwareTables (uint FirmwareTableProviderSignature, IntPtr pFirmwareTableBuffer, uint BufferSize);
[DllImport("kernel32")] public static extern uint GetSystemFirmwareTable (uint FirmwareTableProviderSignature, uint FimrwareTableID, IntPtr pFirmwareTableBuffer, uint BufferSize);
"@
$SFT = Add-Type -MemberDefinition $SFTCode -Name "SFTKlasse" -Language CSharp -UsingNamespace "System.Reflection", "System.Diagnostics", "System.Collections.Generic" -PassThru
# 0×41435049=ACPI ? https://github.com/michaelforney/coreboot/blob/master/src/include/cbmem.h
$firmwareTableProviderSignature = 0x41435049
$StructSize = $SFT::EnumSystemFirmwareTables($firmwareTableProviderSignature, [IntPtr]::Zero, 0)
try
{
$StructPtr = [Runtime.InteropServices.Marshal]::AllocHGlobal($StructSize)
}
catch [OutOfMemoryException]
{
throw Error[0]
}
$buffer = New-Object Byte[]($StructSize)
$SFT::EnumSystemFirmwareTables($firmwareTableProviderSignature, $StructPtr, $StructSize)
[Runtime.InteropServices.Marshal]::Copy($StructPtr, $buffer, 0, $StructSize)
[Runtime.InteropServices.Marshal]::FreeHGlobal($StructPtr)
if (([System.Text.Encoding]::ASCII).GetString($buffer).Contains("MSDM"))
{
$firmwareTableMSDMID = 0x4d44534d
$StructSize = $SFT::GetSystemFirmwareTable($firmwareTableProviderSignature, $firmwareTableMSDMID, [IntPtr]::Zero, 0)
try
{
$StructPtr = [Runtime.InteropServices.Marshal]::AllocHGlobal($StructSize)
}
catch [OutOfMemoryException]
{
throw Error[0]
}
$buffer = New-Object Byte[]($StructSize)
$SFT::GetSystemFirmwareTable($firmwareTableProviderSignature, $firmwareTableMSDMID, $StructPtr, $StructSize)
[Runtime.InteropServices.Marshal]::Copy($StructPtr, $buffer, 0, $StructSize)
[Runtime.InteropServices.Marshal]::FreeHGlobal($StructPtr)
$encoding = [System.Text.Encoding]::GetEncoding(0x4e4)
$key = $encoding.GetString($buffer, 56, 29)
}
$key
@lwalthert

This comment has been minimized.

Copy link

lwalthert commented Jun 22, 2016

Thanks, really interesting script :) It helped me confirm that you can also get this information in CIM/WMI:
PS C:\temp> Get-CimInstance -Class SoftwareLicensingService -Property OA3xOriginalProductKey | Select-Object OA3xOriginalProductKey

Maybe a bit easier to use :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.