{"id":259,"date":"2008-02-14T08:00:00","date_gmt":"2008-02-14T16:00:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/02\/14\/read-remote-registry-function\/"},"modified":"2008-04-24T13:26:19","modified_gmt":"2008-04-24T21:26:19","slug":"read-remote-registry-function","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/02\/14\/read-remote-registry-function\/","title":{"rendered":"Read Remote Registry Function"},"content":{"rendered":"<p>Hopefully you found my <a target=\"_blank\" href=\"\/current\/2008\/2\/11\/remote-registry-reading-the-vbscript-way.html\">last entry<\/a> valuable. But I always like to take things a step further, especially when it comes to scripting and automation. So I turned my last script into a function that returns the value of a specified registry key. Here&rsquo;s a sample script ( ) that uses the function:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&#8216; NAME: ReadRegistryFunctionDemo.vbs<br \/>\n&rsquo; VERSION: 1.0     2\/7\/2008<br \/>\n&rsquo; AUTHOR: Jeffery Hicks     jhicks@sapien.com<br \/>\n&rsquo; USAGE: cscript ReadRegistryFunctionDemo.vbs <\/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. This sample script parses an array of<br \/>\n&rsquo; server names and returns a CSV line of results. You could<br \/>\n&rsquo; easily revise this script to write the results to a text <br \/>\n&rsquo; file. <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&rsquo;*********************************************************************************<br \/>\n&rsquo;* THIS PROGRAM IS OFFERED AS IS AND MAY BE FREELY MODIFIED OR ALTERED AS        *<br \/>\n&rsquo;* NECESSARY TO MEET YOUR NEEDS.  THE AUTHOR MAKES NO GUARANTEES OR WARRANTIES,  *<br \/>\n&rsquo;* EXPRESS, IMPLIED OR OF ANY OTHER KIND TO THIS CODE OR ANY USER MODIFICATIONS. *<br \/>\n&rsquo;* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED IN A SECURED LAB *<br \/>\n&rsquo;* ENVIRONMENT. USE AT YOUR OWN RISK.                                            *<br \/>\n&rsquo;*********************************************************************************<br \/>\nOn Error Resume Next<br \/>\n&lsquo;this script demo assumes the registry keys will not return multi-line values <\/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; <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&lsquo;create an array of computers to query. You could just<br \/>\n&lsquo;as easily use the FileSystemObject to read a list of<br \/>\n&lsquo;computer names.  <br \/>\narrComputers=Array(&ldquo;server01&rdquo;,&rdquo;server02&rdquo;,&rdquo;server03&rdquo;,&rdquo;server04&rdquo;) <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&lsquo;this is a comma separated list of registry values to read<br \/>\n&lsquo;under the registry path specified above<br \/>\nstrNames=&rdquo;RegisteredOwner,RegisteredOrganization,ProductName&rdquo; <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&lsquo;turn the list into an array<br \/>\narrNames=Split(strNames,&rdquo;,&rdquo;) <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">WScript.Echo &ldquo;Computer,&rdquo; &amp; strNames<br \/>\nFor Each strComputer In arrComputers  <br \/>\nstrLine= UCase(strComputer)<br \/>\n&lsquo;get values for each registry key name<br \/>\nFor Each strValueName In arrNames<br \/>\n&lsquo;build a result string separated by commas. I use<br \/>\n&lsquo;quotes &ldquo;&rdquo; around each returned value just in case<br \/>\n&lsquo;the value has a comma<br \/>\nstrLine=strLine &amp; &ldquo;,&rdquo; &amp; Chr(34) &amp;_<br \/>\nTrim(GetRegistryValue(strComputer,strKeyPath,strValueName)) &amp;_<br \/>\nChr(34)<br \/>\nNext<br \/>\n&lsquo;display the result<br \/>\nWScript.Echo strLine<br \/>\nNext <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&lsquo;end main script <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Function GetRegistryValue(strComputer,strKeyPath,strValueName) <\/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);\">Set objReg=GetObject(&ldquo;winmgmts:{impersonationLevel=impersonate}!\\&rdquo; &amp;_<br \/>\nstrComputer &amp; &ldquo;\\root\\default:StdRegProv&rdquo;)<br \/>\nIf Err.Number 0 Then <br \/>\n&lsquo;you can uncomment this section if you want to see the error message<br \/>\n&lsquo;  strMsg=&rdquo;There was a problem connecting to &rdquo; &amp;_<br \/>\n&lsquo;   strComputer &amp; &ldquo;\\root\\default:StdRegProv&rdquo; &amp; VbCrLf &amp;_<br \/>\n&lsquo;  &ldquo;Error &rdquo; &amp; Err.Number &amp; &rdquo; &rdquo; &amp; Err.description<br \/>\n&lsquo;  WScript.echo strMsg<br \/>\nGetRegistryValue = &ldquo;NotFound&rdquo;<br \/>\nExit function<br \/>\nEnd If <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">objReg.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 \/>\nstrData=&rdquo;NotFound&rdquo;<br \/>\nElse<br \/>\nIf IsArray (strValue) Then<br \/>\nFor Each value In strValue<br \/>\nstrData = strData &amp; VbCrLf &amp; value<br \/>\nNext<br \/>\nElse<br \/>\nstrData = strValue<br \/>\nEnd If<br \/>\nEnd If <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">GetRegistryValue = strData <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">End Function <\/font><\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">&lsquo;EOF<\/font><\/p>\n<p>The demo script will parse an array of computer names and registry keys and return a comma separated value like this:<\/p>\n<p><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">Computer,RegisteredOwner,RegisteredOrganization,ProductName<br \/>\nGODOT,&rdquo;Jeffery Hicks&rdquo;,&rdquo;SAPIEN Technologies&rdquo;,&rdquo;Microsoft Windows XP&rdquo;<br \/>\n<\/font><font face=\"Consolas\" color=\"#0000ff\" style=\"color: rgb(0, 0, 255);\">PUCK,&rdquo;Jeffery Hicks&rdquo;,&rdquo;SAPIEN Technologies&rdquo;,&rdquo;Windows Vista (TM) Ultimate&rdquo;<\/font><\/p>\n<p>I think you see where this is going. You could easily revise the script to send the output to a CSV text file using the FileSystemObject. Look through the rest of the script comments to understand what each section is doing.<\/p>\n<div style=\"margin: 0px; padding: 0px; display: inline;\" id=\"scid:0767317B-992E-4b12-91E0-4F059A8CECA8:2091641b-f4de-40d8-80b2-3b878b6b4f2f\" class=\"wlWriterSmartContent\"><span class=\"sizeLess20\">Technorati Tags: <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Registry\"><span class=\"sizeLess20\">Registry<\/span><\/a><span class=\"sizeLess20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/VBScript\"><span class=\"sizeLess20\">VBScript<\/span><\/a><span class=\"sizeLess20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/Scripting\"><span class=\"sizeLess20\">Scripting<\/span><\/a><span class=\"sizeLess20\">, <\/span><a rel=\"tag\" href=\"http:\/\/technorati.com\/tags\/FileSystemObject\"><span class=\"sizeLess20\">FileSystemObject<\/span><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hopefully you found my last entry valuable. But I always like to take things a step further, especially when it comes to scripting and automation. So I turned my last script into a function that returns the value of a specified registry key<\/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":[67,68,59,31,36,57,58,996,35],"class_list":["post-259","post","type-post","status-publish","format-standard","hentry","category-vbscript","tag-array","tag-csv","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\/259","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=259"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/259\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}