欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PowerShell小技巧之读取Windows产品密钥

程序员文章站 2022-03-29 21:42:25
之前大多数人可能用过vbs读取windows产品密钥的vbs脚本,vbs脚本通常都比较隐晦、难懂,今天忙里偷闲,随手写了一个用于读取windows产品密钥的powershe...

之前大多数人可能用过vbs读取windows产品密钥的vbs脚本,vbs脚本通常都比较隐晦、难懂,今天忙里偷闲,随手写了一个用于读取windows产品密钥的powershell脚本。

代码如下:

复制代码 代码如下:

 =====文件名:get-windowsproductkey.ps1=====
function get-windowsproductkey([string]$computer)
{

$comments
author:fuhj(powershell#live.cn ,http://fuhaijun.com)
example: get-windowsproductkey .
'@
$reg = [wmiclass] ("\\" + $computer + "\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 .

执行效果如下:

PowerShell小技巧之读取Windows产品密钥