PowerShell小技巧之讀取Windows產(chǎn)品密鑰
之前大多數(shù)人可能用過(guò)VBS讀取Windows產(chǎn)品密鑰的VBS腳本,VBS腳本通常都比較隱晦、難懂,今天忙里偷閑,隨手寫了一個(gè)用于讀取Windows產(chǎn)品密鑰的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 .
執(zhí)行效果如下:
相關(guān)文章
Windows Powershell強(qiáng)類型數(shù)組
強(qiáng)類型數(shù)組可以理解為強(qiáng)制數(shù)據(jù)類型的數(shù)組,也就是一個(gè)數(shù)組里只包含一種數(shù)據(jù)類型,強(qiáng)制轉(zhuǎn)換數(shù)組語(yǔ)法的優(yōu)勢(shì)就是如果使用分號(hào)代替逗號(hào)分隔值,PowerShell將每個(gè)值看作命令文本,PowerShell會(huì)執(zhí)行它并且存儲(chǔ)結(jié)果。2014-09-09Powershell小技巧之獲取當(dāng)前的時(shí)間并轉(zhuǎn)換為時(shí)辰
這篇文章主要介紹了使用Powershell獲取當(dāng)前的時(shí)間并轉(zhuǎn)換為時(shí)辰的方法,非常簡(jiǎn)單實(shí)用,有需要的朋友可以參考下2014-09-09類似rpm包管理器的Windows Installer PowerShell Module簡(jiǎn)介
前兩篇文章分享了使用PowerShell安裝Window程序的一些技巧。但是都是基于兼容已有命令行的調(diào)用。今天分享一個(gè)第三方組件,讓你直接可以使用Cmdlet命令來(lái)查詢,安裝,修復(fù)Windows程序2014-05-05powershell解決win10開始菜單和通知中心無(wú)法打開
這篇文章主要介紹了powershell解決win10開始菜單和通知中心無(wú)法打開的相關(guān)資料,需要的朋友可以參考下2015-10-10PowerShell腳本監(jiān)控文件夾變化實(shí)例
這篇文章主要介紹了PowerShell腳本監(jiān)控文件夾變化實(shí)例,可以監(jiān)控到文件夾內(nèi)新建文件、刪除文件、重命名文件等操作,需要的朋友可以參考下2014-08-08PowerShell 獲取系統(tǒng)信息的函數(shù)
如果你要得到本地或遠(yuǎn)程的使用配置信息,又不想浪費(fèi)太多的解決時(shí)間??梢栽赑owershell中使用systeminfo.exe提取數(shù)據(jù)2014-03-03