{"id":10,"date":"2008-02-11T14:00:00","date_gmt":"2008-02-11T22:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2008\/02\/11\/remote-registry-reading-the-vbscript-way\/"},"modified":"2008-02-11T15:20:13","modified_gmt":"2008-02-11T23:20:13","slug":"remote-registry-reading-the-vbscript-way","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/02\/11\/remote-registry-reading-the-vbscript-way\/","title":{"rendered":"Remote Registry Reading the VBScript Way"},"content":{"rendered":"<p>Last week I <a target=\"_blank\" href=\"\/current\/2008\/2\/7\/registry-queries-the-easy-way.html\">blogged<\/a> about using REG.EXE in a batch file to read a registry key. The batch file I provided works, but the output is limited. You could probably parse it a bit more to get it in some other format. But I&rsquo;d probably turn to VBScript and WMI if I wanted better output control or if I had more complicated needs.<\/p>\n<p>Without resorting to third party controls, you need to use the WMI registry provider to access remote registries.<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">strComputer=&rdquo;localhost&rdquo;<\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Set objReg=GetObject(&ldquo;winmgmts:{impersonationLevel=impersonate}!\\&rdquo; &amp;_<br \/>\nstrComputer &amp; &ldquo;\\root\\default:StdRegProv&rdquo;)<\/font><\/p>\n<p>Once you have a connection you can enumerate keys or values. I&rsquo;m going to look at the method for enumerating values since my goal is to read a specified key.<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Const HKEY_LOCAL_MACHINE = &amp;H80000002<\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&lsquo;the registry path to query<br \/>\nstrKeyPath =&rdquo;Software\\Microsoft\\Windows NT\\CurrentVersion&rdquo;<br \/>\n&lsquo;the registry key value to get<br \/>\nstrValueName=&rdquo;RegisteredOwner&rdquo;<\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">objReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValues, arrTypes<\/font><\/p>\n<p>The EnumValues method takes several parameters: the registry hive to connect to (I&rsquo;m using a constant value), the registry path, and two variables for the output. The first will store all values found in the registry path and the second will store the type of each value. This is important.<\/p>\n<p>Registry keys can be of different types such as REG_SZ. You need to know the type so you can call the appropriate method. There are different methods depending on the type. The types returned will be numeric so I prefer to use constants<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Const REG_SZ = 1<br \/>\nConst REG_EXPAND_SZ = 2<br \/>\nConst REG_BINARY = 3<br \/>\nConst REG_DWORD = 4<br \/>\nConst REG_MULTI_SZ = 7<\/font><\/p>\n<p>You&rsquo;ll see how I use these in a moment.<\/p>\n<p>Suppose I determine that the key I want to read is a string, then I&rsquo;ll need to use a line like this:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">objReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<\/font><\/p>\n<p>The last parameter,I&rsquo;m using strValue, is the variable that will hold the method&rsquo;s result. All that&rsquo;s left at this point is to display the contents of strValue<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">wscript.echo strValue<\/font><\/p>\n<p>I&rsquo;ve created a VBScript you can use to make the whole process a little easier (it is also attached as a text file).<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&rsquo; NAME: ReadRegistry.vbs<br \/>\n&rsquo; VERSION: 1.0     2\/7\/2008<br \/>\n&rsquo; AUTHOR: Jeffery Hicks     jhicks@sapien.com<br \/>\n&rsquo; USAGE: cscript ReadRegistry [\/s:computername] <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&rsquo; DESCRIPTION: Use the WMI registry provider to read a remote<br \/>\n&rsquo; registry key value. If you don&rsquo;t specify a computer name the script<br \/>\n&rsquo; will default to localhost. <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&lsquo;    *********************************************************************************<br \/>\n&lsquo;    * THIS PROGRAM IS OFFERED AS IS AND MAY BE FREELY MODIFIED OR ALTERED AS        *<br \/>\n&lsquo;    * NECESSARY TO MEET YOUR NEEDS.  THE AUTHOR MAKES NO GUARANTEES OR WARRANTIES,  *<br \/>\n&lsquo;    * EXPRESS, IMPLIED OR OF ANY OTHER KIND TO THIS CODE OR ANY USER MODIFICATIONS. *<br \/>\n&lsquo;    * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED IN A SECURED LAB *<br \/>\n&lsquo;    * ENVIRONMENT. USE AT YOUR OWN RISK.                                            *<br \/>\n&lsquo;    ********************************************************************************* <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">On Error Resume Next <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Const HKEY_LOCAL_MACHINE = &amp;H80000002<br \/>\nConst REG_SZ = 1<br \/>\nConst REG_EXPAND_SZ = 2<br \/>\nConst REG_BINARY = 3<br \/>\nConst REG_DWORD = 4<br \/>\nConst REG_MULTI_SZ = 7 <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&lsquo;the registry path to query<br \/>\nstrKeyPath =&rdquo;Software\\Microsoft\\Windows NT\\CurrentVersion&rdquo;<br \/>\n&lsquo;the registry key value to get<br \/>\nstrValueName=&rdquo;RegisteredOwner&rdquo; <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">If WScript.Arguments.Named.Exists(&ldquo;S&rdquo;) Then<br \/>\nstrComputer=UCase(WScript.Arguments.Named.Item(&ldquo;S&rdquo;))<br \/>\nElse<br \/>\nstrComputer = &ldquo;LOCALHOST&rdquo;<br \/>\nEnd If <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Set objReg=GetObject(&ldquo;winmgmts:{impersonationLevel=impersonate}!\\&rdquo; &amp;_<br \/>\nstrComputer &amp; &ldquo;\\root\\default:StdRegProv&rdquo;)<br \/>\nIf Err.Number 0 Then <br \/>\nstrMsg=&rdquo;There was a problem connecting to &rdquo; &amp;_<br \/>\nstrComputer &amp; &ldquo;\\root\\default:StdRegProv&rdquo; &amp; VbCrLf &amp;_<br \/>\n&ldquo;Error &rdquo; &amp; Err.Number &amp; &rdquo; &rdquo; &amp; Err.description<br \/>\nWScript.Echo strMsg<br \/>\nWScript.Quit<br \/>\nEnd If<br \/>\nobjReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValues, arrTypes<br \/>\nFor x=0 To UBound(arrValues)-1<br \/>\nif Ucase(arrValues(x)) = UCase(strValueName) Then <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">    Select Case arrTypes(x)<br \/>\nCase REG_SZ<br \/>\nobjReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<br \/>\nCase REG_EXPAND_SZ<br \/>\nobjReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<br \/>\nCase REG_BINARY<br \/>\nobjReg.GetBinaryValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<br \/>\nCase REG_DWORD<br \/>\nobjReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<br \/>\nWscript.Echo<br \/>\nCase REG_MULTI_SZ<br \/>\nobjReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<br \/>\nEnd Select <br \/>\nEnd If <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Next <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">If VarType(strValue)=0 Then<br \/>\nWScript.Echo &ldquo;Failed to get a value for &rdquo; &amp; strKeyPath  &amp; strValueName &amp;_<br \/>\n&rdquo; on &rdquo; &amp; strComputer &amp; &ldquo;. Check your registry keys and permissions.&rdquo;<br \/>\nElse<br \/>\nIf IsArray (strValue) Then<br \/>\nWScript.Echo strComputer &amp; &rdquo; &#8211; &rdquo; &amp; strValueName<br \/>\nFor Each value In strValue<br \/>\nWScript.Echo value<br \/>\nNext<br \/>\nElse<br \/>\nWScript.Echo strComputer &amp; &rdquo; &#8211; &rdquo; &amp; strValueName &amp; &rdquo; = &rdquo; &amp; strValue<br \/>\nEnd If<br \/>\nEnd If <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&lsquo;EOF<\/font><\/p>\n<p>You&rsquo;ll need to edit the file for the registry path and key that you want to read. HKLM is really the only remote hive you can read. The script can take a computer name as a run time parameter:<\/p>\n<p><font face=\"courier new\">cscript readregistry \/s:server01<\/font><\/p>\n<p>If you don&rsquo;t specify a computer name the script will default to Localhost.<\/p>\n<p>The script connects to the remote registry and enumerates all values in the specified path. It then enumerates the collection (arrValues) looking for the value that matches the specified key<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">For x=0 To UBound(arrValues)-1<br \/>\nif Ucase(arrValues(x)) = UCase(strValueName) Then<\/font><\/p>\n<p>If it is found, the script checks the data type using a Select Case statement<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Select Case arrTypes(x)<br \/>\n<\/font><\/p>\n<p>Depending on the case, the appropriate method is called to read the value:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">  Case REG_SZ<br \/>\nobjReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<br \/>\nCase REG_EXPAND_SZ<br \/>\nobjReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<br \/>\nCase REG_BINARY<br \/>\nobjReg.GetBinaryValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<br \/>\nCase REG_DWORD<br \/>\nobjReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<br \/>\nWscript.Echo<br \/>\nCase REG_MULTI_SZ<br \/>\nobjReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue<br \/>\n<\/font><\/p>\n<p>At this point all that remains is to display the result. It is possible that the value might be an array as in the case of a multi-string or binary value so I&rsquo;ll have to check for that and handle it accordingly:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">   If IsArray (strValue) Then<br \/>\nWScript.Echo strComputer &amp; &rdquo; &#8211; &rdquo; &amp; strValueName<br \/>\nFor Each value In strValue<br \/>\nWScript.Echo value<br \/>\nNext<br \/>\nElse<br \/>\nWScript.Echo strComputer &amp; &rdquo; &#8211; &rdquo; &amp; strValueName &amp; &rdquo; = &rdquo; &amp; strValue<br \/>\nEnd If<\/font><br \/>\nMy output is pretty simple, but obviously you could do what ever you wanted with it.<\/p>\n<p>That&rsquo;s it for now.  I have one more idea to explore on this topic so stay tuned.<\/p>\n<div style=\"margin: 0px; padding: 0px; display: inline;\" id=\"scid:0767317B-992E-4b12-91E0-4F059A8CECA8:08b90572-8b04-459a-bda0-1637380aa23e\" class=\"wlWriterEditableSmartContent\"><span class=\"sizeGreater20\">Technorati Tags: <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Registry\"><span class=\"sizeGreater20\">Registry<\/span><\/a><span class=\"sizeGreater20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/VBScript\"><span class=\"sizeGreater20\">VBScript<\/span><\/a><span class=\"sizeGreater20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/WMI\"><span class=\"sizeGreater20\">WMI<\/span><\/a><span class=\"sizeGreater20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/StdReg\"><span class=\"sizeGreater20\">StdReg<\/span><\/a><span class=\"sizeGreater20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Scripting\"><span class=\"sizeGreater20\">Scripting<\/span><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Last week I blogged about using REG.EXE in a batch file to read a registry key. The batch file I provided works, but the output is limited. You could probably parse it a bit more to get it in some other format. But I&#8217;d probably turn to VBScript and WMI if I wanted better output control or if I had more complicated needs.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[24],"tags":[59,31,36,57,58,996,35],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-vbscript","tag-hklm","tag-registry","tag-remote","tag-scripting","tag-stdregprov","tag-vbscript","tag-wmi"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}