Search This Blog

Wednesday, December 8, 2010

Powershell Script to Get Windows Product Keys

The following script was adapted (credits embedded) for the Powershell and POWERCLI VMware vSphere environment to retrieve Windows Product Key. Primarily, error handling was added for connection and permission failures.

"Caption" is used to store the error. It would probably neater to store the error in .Error, but oh well.





param ($Computernames = ".")

function Get-WindowsKey {
## function to retrieve the Windows Product Key from any PC
## adapted from Jakob Bindslet (jakob@bindslet.dk)
param ($targets = ".")
$obj = New-Object Object

$hklm = 2147483650
$regValue = "DigitalProductId"


Foreach ($target in $targets) {
$regPath2000_2003 = "Software\Microsoft\Windows NT\CurrentVersion"
$regPath = "Software\Microsoft\Windows NT\CurrentVersion\DefaultProductKey"
$caption = "$Target retrieving..."

#check target if online
If ((test-connection $Target -Count 1 -Quiet) -eq $False){
$caption = "[ERROR] $Target (NO PING)"
write-host $caption -Foregroundcolor Yellow

$obj = New-Object Object
$obj | Add-Member Noteproperty Computer -value $target
$obj | Add-Member Noteproperty Caption -value $caption
$obj

}else {
$productKey = $null
$win32os = $null
$win32os = Get-WmiObject Win32_OperatingSystem -computer $target -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
trap {
$win32os = $null
Continue
}

#check if target cann connect
If ($win32os -eq $null){
$caption = "[ERROR] $Target (ACCESS DENIED)"
write-host $caption -Foregroundcolor RED

$obj = New-Object Object
$obj | Add-Member Noteproperty Computer -value $target
$obj | Add-Member Noteproperty Caption -value $caption
$obj

}else{
write-host $caption -ForeGroundColor Green
$caption = ""
$wmi = [WMIClass]"\\$target\root\default:stdRegProv"
if ($win32os.Caption -NotMatch "2008" -AND $win32os.Caption -NotMatch "Windows 7") {
$regPath = $regPath2000_2003
#$win32os.Caption
}
$data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)
$binArray = ($data.uValue)[52..66]
$charsArray = "B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9"
## decrypt base24 encoded binary data
For ($i = 24; $i -ge 0; $i--) {
$k = 0
For ($j = 14; $j -ge 0; $j--) {
$k = $k * 256 -bxor $binArray[$j]
$binArray[$j] = [math]::truncate($k / 24)
$k = $k % 24
}
$productKey = $charsArray[$k] + $productKey
If (($i % 5 -eq 0) -and ($i -ne 0)) { $productKey = "-" + $productKey }
} ## For $i
} #end if else

$obj = New-Object Object
$obj | Add-Member Noteproperty Computer -value $target
$obj | Add-Member Noteproperty Caption -value ($win32os.Caption + $caption )
$obj | Add-Member Noteproperty CSDVersion -value $win32os.CSDVersion
$obj | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture
$obj | Add-Member Noteproperty BuildNumber -value $win32os.BuildNumber
$obj | Add-Member Noteproperty RegisteredTo -value $win32os.RegisteredUser
$obj | Add-Member Noteproperty ProductID -value $win32os.SerialNumber
$obj | Add-Member Noteproperty ProductKey -value $productkey
$obj
} ##check target if online


}#End foreach
} #end Function Get-WindowsKey


Get-WindowsKey $Computernames








1 comment: