How to read registry value remotely in all computers in the domain

I had a task to find out what versions of PowerShell we have installed on the computers in the domain.
PowerShell version is stored in HKEY_LOCAL_MACHINESOFTWAREMicrosoftPowerShell1PowerShellEngine
There is a a string called PowerShell version and the value of the string is the version of the PowerShell that is installed on the computer.
After looking around I came up with the following script:

On Error Resume Next

Dim objGroup, objFSO, strFile, objFile

Const HKEY_LOCAL_MACHINE = &H80000002
Const ForWriting = 2
Const OpenAsASCII = 0
Const CreateIfNotExist = True
Const ADS_SCOPE_SUBTREE = 2

strFile = “c:powershell.txt”

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpenTextFile(strFile, _
ForWriting, CreateIfNotExist, OpenAsASCII)

Set objConnection = CreateObject(“ADODB.Connection”)
Set objCommand = CreateObject(“ADODB.Command”)
objConnection.Provider = “ADsDSOObject”
objConnection.Open “Active Directory Provider”
Set objCommand.ActiveConnection = objConnection

objCommand.Properties(“Page Size”) = 1000
objCommand.Properties(“Searchscope”) = ADS_SCOPE_SUBTREE

objCommand.CommandText = _
“SELECT ADsPath FROM ‘LDAP://dc=domain,dc=local’ WHERE ” & _
“objectCategory=’organizationalUnit'”

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
Set objOU = GetObject(objRecordSet.Fields(“ADsPath”).Value)

objOU.Filter = Array(“Computer”)

For Each objItem in objOU
strComputer = objItem.CN
Set objRegistry = GetObject(“winmgmts:\” & _
strComputer & “rootdefault:StdRegProv”)

strKeyPath = “SOFTWAREMicrosoftPowerShell1PowerShellEngine”
strValueName = “PowerShellVersion”
objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

If IsNull(strValue) Then
objFile.WriteLine strComputer & “, No entry”
Else
objFile.WriteLine strComputer & “, ” & strValue
End If
Next

objRecordSet.MoveNext
Loop

objFile.Close

The script scans OUs in AD for computer accounts and then tries to connect to the computer to read the value of the registry. Script outputs the computer name and the value of the string to poweshell.txt file on your C: drive. If the registry does not exist it writes No entry id the computer cannot be reached then there will be nothing next to the computer name. Here is an example of an output file:
COMP_1,
COMP_2,
COMP_3, 2.0
COMP_4, No entry

This script can be used to check any registry key with small modifications.
First you need to change the following line to enter your domain:
“SELECT ADsPath FROM ‘LDAP://dc=domain,dc=local’ WHERE ” & _

Also if you keep all you computers in the Computers container then change the line from “objectCategory=’organizationalUnit'” to “objectCategory=’container'”

Now you need to change the path to the registry key:
strKeyPath = “SOFTWAREMicrosoftPowerShell1PowerShellEngine”

And also change the string valeu name:
strValueName = “PowerShellVersion”

If the value that you are trying to read is not stored in string but lets same in DWORD or BINARY format then you’ll need to change:
objRegistry.GetStringValue
to objRegistry.GetDwordValue or objRegistry.GetBinaryValue

Well that is it.

PS. When copying text directly from the blog beware that sometime the quotes ” change and the script fails. Just delete them and replace with anew ones.

Leave a Reply

Your email address will not be published.