Get-WindowsKey

Hi,

I am very new to PowerShell and am looking for a script that can be run remotely against a list of Windows Server 2008 64bit, Windows Server 2008 R2 64bit. This is the script I was given from another website, but when running it, nothing it displayed;

 

function Get-WindowsKey {
    ## function to retrieve the Windows Product Key from any PC
        param ($targets = ".")
    $hklm = 2147483650
    $regPath = "Software\Microsoft\Windows NT\CurrentVersion"
    $regValue = "DigitalProductId64"
    Foreach ($target in $targets) {
        $productKey = $null
        $win32os = $null
        $wmi = [WMIClass]"\\$target\root\default:stdRegProv"
        $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
            }
        }
        $win32os = Get-WmiObject Win32_OperatingSystem -computer $target
        $obj = New-Object Object
        $obj | Add-Member Noteproperty Computer -value $target
        $obj | Add-Member Noteproperty Caption -value $win32os.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
    }
}

I copied the script in to notepad and saved it as windowskey.ps1 and then elevated my powersheel execution to be able to run the script, but no output is shown. I was trying this on my local Windows 7 OS first, just to test it worked.

January 24th, 2011 10:05am

this is just a function, you have to call the function inorder for it to execute.

try adding this in the last line of the .ps1 file and then run it again

get-windowskey

get-windowskey localhost

thanks

Thiyagu

Free Windows Admin Tool Kit Click here and download it now
January 24th, 2011 10:21am

Absolutely perfect. Thanks for that. So if I wanted to run this against a list of remote servers, I would add the computers names in there too?
January 24th, 2011 10:36am

lets say you have a servers.txt file in your C:\

and this file has all your servers like so

server1

server2

now what you can do in your script is, go to the end

and

$servers = get-content C:\servers.txt

foreach($server in $servers)

{

get-windowskey $server

}

this will run the function for all the server in the .txt file

thanks

thiyagu

Free Windows Admin Tool Kit Click here and download it now
January 24th, 2011 10:49am

Brilliant. Thanks again
January 24th, 2011 10:58am

OK, just ran this across a 2 remote servers using the C:\servers.txt, which I populated with two 64bit servers. I get this error;

Cannot index into a null array.
At C:\Users\ashley.poxon\Documents\getproductkey.ps1:20 char:27
+                 $binArray[ <<<< $j] = [math]::truncate($k / 24)
    + CategoryInfo          : InvalidOperation: (12:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\ashley.poxon\Documents\getproductkey.ps1:19 char:47
+                 $k = $k * 256 -bxor $binArray[ <<<< $j]
    + CategoryInfo          : InvalidOperation: (12:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Free Windows Admin Tool Kit Click here and download it now
January 24th, 2011 11:09am

path looks wrong, at least from my win7 machine, I have these two

DigitalProductID and DigitalProductID4 (not 64)

January 24th, 2011 2:31pm

Hmm, that didnt seem to help with the errors mentioned above :-(
Free Windows Admin Tool Kit Click here and download it now
January 24th, 2011 2:44pm

so, in windows 7 and 2008 its DigitalPRoductID4, in windows 2003 its just DigitalProductID.

im not sure what the difference is but the ID4 returned the same thing on all my win7/2k8 systems but just the ID returned unique values... we run a KMS here so im not sure what they should be.

might want to check what OS the system is running before trying to figure out the key?

January 24th, 2011 3:06pm

I am running this from a Windows 7 32bit PC, and was trying to run the script remotely, from this PC, on some Windows Server 2008 64bit servers.
Free Windows Admin Tool Kit Click here and download it now
January 24th, 2011 3:32pm

well then the code works fine. I ran your function with the ID4 and it returned results from win7 and 2008 (remote) machines. it didnt return correctly when you just ran Get-WindowsKey (hitting local win7 machine) changing the ID64 to ID4?

January 24th, 2011 3:43pm

OK, if I leave it running long enough and ignore the lots of errors like ;


Cannot index into a null array.
At C:\Users\ashley.poxon\Documents\getproductkey.ps1:19 char:27
+                 $binArray[ <<<< $j] = [math]::truncate($k / 24)
    + CategoryInfo          : InvalidOperation: (7:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\ashley.poxon\Documents\getproductkey.ps1:18 char:47
+                 $k = $k * 256 -bxor $binArray[ <<<< $j]
    + CategoryInfo          : InvalidOperation: (6:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

Cannot index into a null array.

It does then return the product keys. There is a 32bit OS that it just returns BBBBB-BBBBB etc for the product key. Can you run this only against 32bit and 64bit individually?

Free Windows Admin Tool Kit Click here and download it now
January 24th, 2011 4:09pm

still looks good.

I think the first thing I'd do is getting working locally on your machine (dont feed it the server list) and make sure the key is populated on your machine.

run this

gi "hklm:\software\microsoft\windows nt\currentversion").digitalproductid4

you should see the digitalproductID and the ID4 as well and it should show you the first few bits... if it does, then use your code to pull it and make sure you get some data...

$regPath = "Software\Microsoft\Windows NT\CurrentVersion"
$regValue = "DigitalProductId4"
$wmi = [WMIClass]"\\.\root\default:stdRegProv"
$data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)
$data

the uValue should show a couple of numbers... if its blank, then you have a problem..

January 24th, 2011 4:19pm

If I do run this, locally, on my windows 7 32bit OS then it works with no errors;

function Get-WindowsKey {
    ## function to retrieve the Windows Product Key from any PC
        param ($targets = ".")
    $hklm = 2147483650
    $regPath = "Software\Microsoft\Windows NT\CurrentVersion"
    $regValue = "DigitalProductId4"
    Foreach ($target in $targets) {
        $productKey = $null
        $win32os = $null
        $wmi = [WMIClass]"\\$target\root\default:stdRegProv"
        $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
            }
        }
        $win32os = Get-WmiObject Win32_OperatingSystem -computer $target
        $obj = New-Object Object
        $obj | Add-Member Noteproperty Computer -value $target
        $obj | Add-Member Noteproperty Caption -value $win32os.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
    }
}

Get-WindowsKey localhost

Free Windows Admin Tool Kit Click here and download it now
January 24th, 2011 4:32pm

OK, and if I run that script locally on a Windows Server 2008 R2 64bit OS, it works as well. It just seems to be a problem running it remotely.

Any help would be muchly appreciated

January 24th, 2011 4:37pm

probably a remote registry/WMI problem.

$regPath = "Software\Microsoft\Windows NT\CurrentVersion"
$regValue = "DigitalProductId4"
$wmi = [WMIClass]"\\<<replace with a server name>>\root\default:stdRegProv"
$data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)
$data

try that with a remote server.

  • Marked as answer by AshPoxon Tuesday, February 01, 2011 1:33 PM
Free Windows Admin Tool Kit Click here and download it now
January 24th, 2011 4:42pm

I think i have been extremely silly. I was running this as my non-domain admin acount :-(

Will test it from my domain admin account and try that. Cant believe I did this

January 24th, 2011 6:15pm

I tried this but i keep getting this message

Import-Module : A positional parameter cannot be found that accepts argement

'Njoku\SkyDrive\Documents\recoverykey.sp1.txt'.

At line:1 char:1

+Import-Module C:\users\Debbie Njoku\SkyDrive\Documents\recoverykey.sp1.txt; get-...

+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+categoryInfo : InvalidArgument: (:) [Import-Module], ParametrebindingException

+fullyQualifiedwrrorId :PositionalParametreNotFound,Microsoft.Powershell.Commands.ImportModlueCommand

get-windowskey : The term 'get-windowskey' is not recognized as the name of a cmdlet, function,script file,operable program. Check the spelling of the name, or if a path was included, verify the path is correct again.

At line:1 Char:77

+ Import-Module C:\Users\Debbie Njoku\SkyDrive\Documents\recoverykey.sp1.txt; Get-......

+ ~~~~

+Catergoryinfo objectnotFound:   (Get-windowskey:String) [], CommandNotFoundException

+FullyQualifiedErrorId : CommandNotFoundException

Could you please explain to me what all this means.

Regards Debbra

Free Windows Admin Tool Kit Click here and download it now
January 5th, 2015 4:38am

you are getting this error because there should be NO SPACES in the path of the file key.ps1

also , the name of the file is wrong. the extension should be ps1 not sp1 and there should be to .txt after that.

for example: key.ps1

when you save the file in notepad don't save as .txt, but save as "all files", then notepad will not automatically append the ".txt" extension to your file.

i suggest you rename the file properly, start the power shell as an administrator, and then run the command like this:

PS C:\WINDOWS\system32> Import-Module E:\RecoverKey.ps1; Get-WindowsKey

August 18th, 2015 5:56am

Thread closed for more than 4 years.

Free Windows Admin Tool Kit Click here and download it now
August 18th, 2015 6:42am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics