zoukankan      html  css  js  c++  java
  • PowerShell读取Windows产品密钥

    之前大多数人可能用过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 + "
    ootdefault:StdRegProv")
    $values = [byte[]]($reg.getbinaryvalue(2147483650,"SOFTWAREMicrosoftWindows NTCurrentVersion","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 .

    执行效果如下:

    image 

    作者: 付海军
    出处:http://fuhj02.cnblogs.com
    版权:本文版权归作者和博客园共有
    转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
    要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接且保证内容完整!否则必究法律责任!
    个人网站: http://www.fuhaijun.com/

  • 相关阅读:
    lua 5.3最简单plugin编写
    CMake for MFC example
    写了个自动生成vcxproj的程序
    kindle試玩
    解放双手:如何在本地调试远程服务器上的Node代码
    PM2实用入门指南
    Express使用手记:核心入门
    Node服务一键离线部署
    fis-receiver:一行命令将项目部署到远程服务器
    Reflux系列01:异步操作经验小结
  • 原文地址:https://www.cnblogs.com/fuhj02/p/3351172.html
Copyright © 2011-2022 走看看