# PowerShell script to get remote registry keys.

• ### Question

• $computers = Get-Content ".\Servers.txt"$key = 'SOFTWARE\Wow6432Node\MyApp\Registration\MyAppKey'
$valuename = 'ProductID'$computers = Get-Content Servers.txt
foreach ($computer in$computers) {

$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$computer)
$MachineName =$computer
$regkey =$reg.opensubkey($key) Write-host$MachineName "," $regkey.getvalue($valuename) rn

}

The objective of the code is to pull the ProductID values  from a remote server list stored in servers.txt running on my Windows 10 laptop. I do not have the application locally but it is checking my Registry key for it and gives an error. It gets the two servers in the servers.txt list but complains when It cannot find the key on the local machine.
Example Output:

SRV-2012 , 3CF4AF6F7310DCA04777002387EEE4F657D4C2171346

SRV-2016 , 3CF4AF6F7310DCA0477700235C463C7057D4C2171346

You cannot call a method on a null-valued expression.
At C:\Registration\Get-ProductID.ps1:12 char:2
+

Tuesday, September 17, 2019 8:29 PM

• You have bank lines in your file.

\_(ツ)_/

• Proposed as answer by Wednesday, September 18, 2019 5:24 PM
• Marked as answer by Wednesday, September 18, 2019 6:12 PM
Wednesday, September 18, 2019 4:54 PM
• You were correct, I backspaced out the blank lines and it stopped checking
the local host.

Thanks,

Doug

• Marked as answer by Wednesday, September 18, 2019 5:14 PM
Wednesday, September 18, 2019 5:14 PM

### All replies

• If the key doesn't exist then you will get that error.  Be sure the server that you are querying has the key specified.

The key requested will only exist on 64 bit architectures.  You will need to be sure the systems are 64 bit systems.  If 32 bit then you will have to directly access the key.

\$key = 'SOFTWARE\MyApp\Registration\MyAppKey'

\_(ツ)_/

Tuesday, September 17, 2019 9:25 PM
• My appoligies,

I should have spoken more clearly.
I want the script to only check the servers that are listed in Servers.txt.

However , the script always includes the Windows Workstation that I run it from
and the application is not installed on the Workstation. I do not want it to check the local machine.

Thanks

Doug

Wednesday, September 18, 2019 10:51 AM
• So remove the local machine from the file.

\_(ツ)_/

Wednesday, September 18, 2019 11:36 AM
• The local machines name is jd11 which is a windows 10 laptop.

It is not listed in ".\Servers.txt", so I do not understand why the foreach loop is looking in the local registry.
Note if I install the application on jd11 , I do not get any errors but list the ProductID of the local host for each time the loop takes place.  If servers.txt has  3 servers then I would have 3 instances of the lines that list the local host ProductID.

C:\Registration>powershell -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -File .\em.ps1
SRV-2012 , 3CF4AF6F7310DCA04777002387EEE4F657D4C2171346

SRV-2016 , 3CF4AF6F7310DCA0477700235C463C7057D4C2171346

, 3CF4AF6F7310DCA0477700236A6A34B857D4C2171346

, 3CF4AF6F7310DCA0477700236A6A34B857D4C2171346

Wednesday, September 18, 2019 4:03 PM
• You have bank lines in your file.

\_(ツ)_/

• Proposed as answer by Wednesday, September 18, 2019 5:24 PM
• Marked as answer by Wednesday, September 18, 2019 6:12 PM
Wednesday, September 18, 2019 4:54 PM
• You were correct, I backspaced out the blank lines and it stopped checking
the local host.

Thanks,

Doug

• Marked as answer by Wednesday, September 18, 2019 5:14 PM
Wednesday, September 18, 2019 5:14 PM