PowerShell小技巧之读取Windows产品密钥
2019/7/10 21:24:41
本文主要是介绍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 + "\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产品密钥的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-09-14SharePoint 2019 用 PowerShell将启用发布功能站点网站集另存为模板
- 2022-08-30PowerShell教程 - 程序性能和BUG分析工具
- 2022-08-30PowerShell教程 - 模块管理(Modules Management)
- 2022-08-29PowerShell教程 - Web requests(Web请求)
- 2022-08-26PowerShell教程 - 日期时间管理(Date & Time Management)
- 2022-08-25PowerShell教程 - 磁盘与硬件管理(Disk & Hardware Management)
- 2022-08-25PowerShell教程 - 系统事件管理(System Event Management)
- 2022-08-25PowerShell教程 - 文件系统管理(File System Management)
- 2022-08-24PowerShell教程 - 网络管理(Network Management)
- 2022-08-24PowerShell