手机
当前位置:查字典教程网 >脚本专栏 >PowerShell >PowerShell小技巧之读取Windows产品密钥
PowerShell小技巧之读取Windows产品密钥
摘要:之前大多数人可能用过VBS读取Windows产品密钥的VBS脚本,VBS脚本通常都比较隐晦、难懂,今天忙里偷闲,随手写了一个用于读取Wind...

之前大多数人可能用过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 + "rootdefault: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 .

执行效果如下:

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

【PowerShell小技巧之读取Windows产品密钥】相关文章:

Powershell小技巧--将文件夹中的大文件分成若干份

Powershell小技巧之查找脚本中的函数

PowerShell读取文本文件指定行内容的方法

Powershell小技巧之获取MAC地址

PowerShell获取Windows用户列表、用户信息的方法

PowerShell小技巧之实现文件下载(类wget)

Powershell小技巧之查看安装的.Net framework版本信息

Powershell小技巧之屏蔽输出结果

PowerShell小技巧之启动远程桌面连接

使用PowerShell操作Windows服务的命令小结

精品推荐
分类导航