Product Keys Remotely

by Ramey870 at 2012-11-16 09:51:35

I am trying to run a module that pulls product keys from remote machines. It works on some machines on others it gives me an error of: "Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found…" Where the product key should be.
Here is the actual ps1. Any ideas as to why it pulls some keys from machine and not others? Thanks






function Get-ProductKey {
<#
.SYNOPSIS
Retrieves the product key and OS information from a local or remote system/s.

.DESCRIPTION
Retrieves the product key and OS information from a local or remote system/s. Queries of 64bit OS from a 32bit OS will result in
inaccurate data being returned for the Product Key. You must query a 64bit OS from a system running a 64bit OS.

.PARAMETER Computername
Name of the local or remote system/s.

.NOTES
Author: Boe Prox
Version: 1.1
-Update of function from http://powershell.com/cs/blogs/tips/arc … t-key.aspx
-Added capability to query more than one system
-Supports remote system query
-Supports querying 64bit OSes
-Shows OS description and Version in output object
-Error Handling

.EXAMPLE
Get-ProductKey -Computername Server1

OSDescription Computername OSVersion ProductKey
------------- ------------ --------- ----------
Microsoft(R) Windows(R) Server 2003, Enterprise Edition Server1 5.2.3790 bcdfg-hjklm-pqrtt-vwxyy-12345

Description
-----------
Retrieves the product key information from ‘Server1’
#>
[cmdletbinding()]
Param (
[parameter(ValueFromPipeLine=$True,ValueFromPipeLineByPropertyName=$True)]
[Alias("CN","__Server","IPAddress","Server")]
[string]$Computername = $Env:Computername
)
Begin {
$map="BCDFGHJKMPQRTVWXY2346789"
}
Process {
ForEach ($Computer in $Computername) {
Write-Verbose ("{0}: Checking network availability" -f $Computer)
If (Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
Try {
Write-Verbose ("{0}: Retrieving WMI OS information" -f $Computer)
$OS = Get-WmiObject -ComputerName $Computer Win32_OperatingSystem -ErrorAction Stop
} Catch {
$OS = New-Object PSObject -Property @{
Caption = $.Exception.Message
Version = $
.Exception.Message
}
}
Try {
Write-Verbose ("{0}: Attempting remote registry access" -f $Computer)
$remoteReg = [Microsoft.Win32.RegistryKey]]::LocalMachine,$Computer)
If ($OS.OSArchitecture -eq ‘64-bit’) {
$value = $remoteReg.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue(‘DigitalProductId4’)[0x34…0x42]
} Else {
$value = $remoteReg.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue(‘DigitalProductId’)[0x34…0x42]
}
$ProductKey = ""
Write-Verbose ("{0}: Translating data into product key" -f $Computer)
for ($i = 24; $i -ge 0; $i–) {
$r = 0
for ($j = 14; $j -ge 0; $j–) {
$r = ($r * 256) -bxor $value[$j]
$value[$j] = [math]]($r/24))
$r = $r % 24
}
$ProductKey = $map[$r] + $ProductKey
if (($i % 5) -eq 0 -and $i -ne 0) {
$ProductKey = "-" + $ProductKey
}
}
} Catch {
$ProductKey = $_.Exception.Message
}
$object = New-Object PSObject -Property @{
Computername = $Computer
ProductKey = $ProductKey
OSDescription = $os.Caption
OSVersion = $os.Version
}
$object.pstypenames.insert(0,‘ProductKey.Info’)
$object
} Else {
$object = New-Object PSObject -Property @{
Computername = $Computer
ProductKey = ‘Unreachable’
OSDescription = ‘Unreachable’
OSVersion = ‘Unreachable’
}
$object.pstypenames.insert(0,‘ProductKey.Info’)
$object
}
}
}
}
by DonJ at 2012-11-16 09:56:17
You’ll want to wrap your script in code or PowerShell tags - there are buttons on the editor toolbar for that. Otherwise it’s pretty unreadable.

I’d also suggest contacting Boe directly, since you’re basing this on his script. He’s at https://twitter.com/proxb on Twitter.
by Ramey870 at 2012-11-16 10:00:30
Sorry about that and Thank you
by DonJ at 2012-11-16 10:44:11
No worries. I pinged Boe on Twitter; not sure what time zone he’s in but give him a day or so to check this out. Hopefully he can offer some advice!

In the meantime, what if you run the command locally on a server that’s failing remotely? Does the command run okay if you just run it locally right on the box?
by Ramey870 at 2012-11-16 11:39:30
I tried it on one of the machines I couldn’t pull it from, and the command Get-Productkey worked just fine on that machine. From that machine I tried to pull the product key from my machine and a couple others that were having the same problem and got the same error.
by DonJ at 2012-11-16 11:41:59
Huh. That makes it seem like a security context thing. Will have to see what Boe says.
by proxb at 2012-11-16 17:16:02
Hi! One of the things I was going to have you do is to try running the function from the machine that you were having issues connecting to remotely. Are the systems with the issues connecting running the same operating system or a mixed bag? It does sound like some sort of security issue that is preventing it from running against the remote systems.

I was able to reproduce this issue by stopping the Remote Registry service on a remote system, which then gave the same error you are seeing. I then copied the script out to the server and was able to run it without issue. If the service is running on those systems, you might want to look at the firewall to make sure that it is not blocking the ports for the service. I found the following link that might help to ensure the service is reachable.

http://www.techrepublic.com/article/lock-down-remote-access-to-the-windows-registry/5270774

Let me know if this helps out!
by Ramey870 at 2012-11-19 07:45:32
First off let me say thanks for the script. Yes, it is a mixed bag of operating systems. I started the Remote Registry service on one of the PC’s and it pulled just fine. Thank You. Now just to find a Powershell script to turn that on for all the PC’s. lol
by proxb at 2012-11-19 19:51:16
Glad that it is working for you now! If you have any other issues, let me know!