{"id":666,"date":"2011-03-04T10:34:36","date_gmt":"2011-03-04T10:34:36","guid":{"rendered":"http:\/\/www.kozeniauskas.com\/itblog\/?p=666"},"modified":"2011-03-04T10:35:09","modified_gmt":"2011-03-04T10:35:09","slug":"how-to-read-registry-value-remotely-in-all-computers-in-the-domain","status":"publish","type":"post","link":"https:\/\/www.kozeniauskas.com\/itblog\/2011\/03\/04\/how-to-read-registry-value-remotely-in-all-computers-in-the-domain\/","title":{"rendered":"How to read registry value remotely in all computers in the domain"},"content":{"rendered":"<p>I\u00a0had a task to find out what versions of PowerShell we have installed on the computers in the domain.<br \/>\nPowerShell version is stored in HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\PowerShell\\1\\PowerShellEngine\\<br \/>\nThere 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.<br \/>\nAfter looking around I came up with the following script:<\/p>\n<p><em>On Error Resume Next<\/em><\/p>\n<p><em>Dim objGroup, objFSO, strFile, objFile<\/em><\/p>\n<p><em>Const HKEY_LOCAL_MACHINE = &amp;H80000002<\/em><br \/>\n<em>Const ForWriting = 2<\/em><br \/>\n<em>Const OpenAsASCII = 0<\/em><br \/>\n<em>Const CreateIfNotExist = True<\/em><br \/>\n<em>Const ADS_SCOPE_SUBTREE = 2<\/em><\/p>\n<p><em>strFile = &#8220;c:\\powershell.txt&#8221;<\/em><br \/>\n<!--more--><br \/>\n<em>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/em><br \/>\n<em>Set objFile = objFSO.OpenTextFile(strFile, _<\/em><br \/>\n<em>ForWriting, CreateIfNotExist, OpenAsASCII)<\/em><\/p>\n<p><em>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)<\/em><br \/>\n<em>Set objCommand = CreateObject(&#8220;ADODB.Command&#8221;)<\/em><br \/>\n<em>objConnection.Provider = &#8220;ADsDSOObject&#8221;<\/em><br \/>\n<em>objConnection.Open &#8220;Active Directory Provider&#8221;<\/em><br \/>\n<em>Set objCommand.ActiveConnection = objConnection<\/em><\/p>\n<p><em>objCommand.Properties(&#8220;Page Size&#8221;) = 1000<\/em><br \/>\n<em>objCommand.Properties(&#8220;Searchscope&#8221;) = ADS_SCOPE_SUBTREE <\/em><\/p>\n<p><em>objCommand.CommandText = _<\/em><br \/>\n<em>&#8220;SELECT ADsPath FROM &#8216;LDAP:\/\/dc=domain,dc=local&#8217; WHERE &#8221; &amp; _<\/em><br \/>\n<em>&#8220;objectCategory=&#8217;organizationalUnit'&#8221; <\/em><\/p>\n<p><em>Set objRecordSet = objCommand.Execute<\/em><\/p>\n<p><em>objRecordSet.MoveFirst<\/em><\/p>\n<p><em>Do Until objRecordSet.EOF<\/em><br \/>\n<em>Set objOU = GetObject(objRecordSet.Fields(&#8220;ADsPath&#8221;).Value)<\/em><\/p>\n<p><em>objOU.Filter = Array(&#8220;Computer&#8221;)<\/em><\/p>\n<p><em>For Each objItem in objOU<\/em><br \/>\n<em>strComputer = objItem.CN<\/em><br \/>\n<em>Set objRegistry = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; _<\/em><br \/>\n<em>strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/em><\/p>\n<p><em>strKeyPath = &#8220;SOFTWARE\\Microsoft\\PowerShell\\1\\PowerShellEngine&#8221;<\/em><br \/>\n<em>strValueName = &#8220;PowerShellVersion&#8221;<\/em><br \/>\n<em>objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<\/em><\/p>\n<p><em>If IsNull(strValue) Then<\/em><br \/>\n<em>objFile.WriteLine strComputer &amp; &#8220;, No entry&#8221;<\/em><br \/>\n<em>Else<\/em><br \/>\n<em>objFile.WriteLine strComputer &amp; &#8220;, &#8221; &amp; strValue<\/em><br \/>\n<em>End If<\/em><br \/>\n<em>Next<\/em><\/p>\n<p><em>objRecordSet.MoveNext<\/em><br \/>\n<em>Loop<\/em><\/p>\n<p><em>objFile.Close<\/em><\/p>\n<p>The script scans OUs in AD for computer accounts and then tries to connect to the computer to read the value of the registry.\u00a0Script outputs the computer name and the value of the string\u00a0to poweshell.txt file on your C: drive. If the registry does not exist it writes <em>No entry <\/em>id the computer cannot be reached then there will be nothing next to the computer name. Here is an example of an output file:<br \/>\n<em>COMP_1,<\/em><br \/>\n<em>COMP_2,<\/em><br \/>\n<em>COMP_3, 2.0<\/em><br \/>\n<em>COMP_4, No entry<\/em><\/p>\n<p>This script can be used to check any registry key with small modifications.<br \/>\nFirst you need to change the following line to enter your domain:<br \/>\n<em>&#8220;SELECT ADsPath FROM &#8216;LDAP:\/\/dc=domain,dc=local&#8217; WHERE &#8221; &amp; _<\/em><\/p>\n<p>Also if you keep all you computers in the Computers container then change the line from <em>&#8220;objectCategory=&#8217;organizationalUnit'&#8221; <\/em>to <em><em>&#8220;objectCategory=&#8217;container'&#8221;<\/em><\/em><\/p>\n<p>Now you need to change the path to the registry key:<br \/>\n<em>strKeyPath = &#8220;SOFTWARE\\Microsoft\\PowerShell\\1\\PowerShellEngine&#8221;<\/em><\/p>\n<p>And also change the string valeu name:<br \/>\n<em>strValueName = &#8220;PowerShellVersion&#8221;<\/em><\/p>\n<p>If the value that you are trying to read is not stored in string but lets same in\u00a0DWORD or BINARY format then you&#8217;ll need to change:<em><br \/>\n<em>objRegistry.GetStringValue <\/em><\/em>to <em>objRegistry.GetDwordValue <\/em>or <em>objRegistry.GetBinaryValue<\/em><\/p>\n<p>Well that is it.<\/p>\n<p>PS. When copying text directly from the blog beware that sometime the quotes &#8221; change and the script fails. Just delete them and replace with anew ones.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u00a0had 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_MACHINE\\SOFTWARE\\Microsoft\\PowerShell\\1\\PowerShellEngine\\ 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/posts\/666"}],"collection":[{"href":"https:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/comments?post=666"}],"version-history":[{"count":2,"href":"https:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/posts\/666\/revisions"}],"predecessor-version":[{"id":668,"href":"https:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/posts\/666\/revisions\/668"}],"wp:attachment":[{"href":"https:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/media?parent=666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/categories?post=666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kozeniauskas.com\/itblog\/wp-json\/wp\/v2\/tags?post=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}