PoshCode Archive  Artifact [bd7bdf48b5]

Artifact bd7bdf48b5967004070c581a1424d80c5225b2eeb37dc4b1af8c5f0f85f2587b:

  • File get-windows-product-key.ps1 — part of check-in [260e444ca6] at 2018-06-10 12:58:18 on branch trunk — Retrieve the windows product key of a specified machine using WMI — Updated for v2 and to take an array of computers (without rewriting anything) (user: karl prosser size: 1822)

# encoding: ascii
# api: powershell
# title: get windows product key
# description: Retrieve the windows product key of a specified machine using WMI — Updated for v2 and to take an array of computers (without rewriting anything)
# version: 0.1
# type: function
# author: karl prosser
# license: CC0
# function: get-windowsproductkey
# x-poshcode-id: 1544
# x-archived: 2016-08-26T03:22:52
# x-published: 2010-12-17T13:47:00
#
#
function get-windowsproductkey([Parameter(ValueFromPipeline=$true)][string[]]$computer)
{
PROCESS{
foreach($comp in $computer) {
$Reg = [WMIClass] ("\\" + $comp + "\root\default:StdRegProv")
$values = [byte[]]($reg.getbinaryvalue(2147483650,"SOFTWARE\Microsoft\Windows NT\CurrentVersion","DigitalProductId").uvalue)
$lookup = [char[]]("B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9")
$keyStartIndex = [int]52;
$keyEndIndex = [int]($keyStartIndex + 15);
$decodeLength = [int]29
$decodeStringLength = [int]15
$decodedChars = new-object char[] $decodeLength 
$hexPid = new-object System.Collections.ArrayList
for ($i = $keyStartIndex; $i -le $keyEndIndex; $i++){ [void]$hexPid.Add($values[$i]) }
for ( $i = $decodeLength - 1; $i -ge 0; $i--)
    {                
     if (($i + 1) % 6 -eq 0){$decodedChars[$i] = '-'}
     else
       {
        $digitMapIndex = [int]0
        for ($j = $decodeStringLength - 1; $j -ge 0; $j--)
        {
            $byteValue = [int](($digitMapIndex * [int]256) -bor [byte]$hexPid[$j]);
            $hexPid[$j] = [byte] ([math]::Floor($byteValue / 24));
            $digitMapIndex = $byteValue % 24;
            $decodedChars[$i] = $lookup[$digitMapIndex];
         }
        }
     }
$STR = ''     
$decodedChars | % { $str+=$_}
$STR
}}}
get-windowsproductkey .