Last active
December 28, 2020 10:28
-
-
Save newyear2006/5578386bf4793a334f85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :)