Solved

Use a vbscript to change a registry key based on information in an array

Posted on 2008-06-13
5
1,318 Views
Last Modified: 2012-06-21
My company uses a config.xml file to determine where packages will be installed from. The location of the config file that each system should use is set through a registry key. Recently, we discovered that a lot of the systems did not have the correct registry key and need to be corrected.

Here is an example of my problem.
A system will look in HKLM\Software\Custom\Config and see \\USAT001\Custom\ccpconfig.xml as the location for the config file. However, this is incorrect and should be  \\USME032\Custom\ccpconfig.xml. Each system at my company uses the first four letter as the location in the computer name. So any system starting with USME should use  the \\USME032\Custom\ccpconfig.xml.

How can I script it so a the computer name is examined? Once the computer name is determined, it changes the registry key to the correct location based on the first four letter.

Any help would be appreciate greatly.
0
Comment
Question by:Lorrec
  • 3
  • 2
5 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 21781520
Paste the script below into a text file with a .vbs extension.  Running it will modify the string value Config under the key HKLM\Software\Custom.


Const HKEY_LOCAL_MACHINE = &H80000002
 

Set WSHShell = CreateObject("WScript.Shell")

strComputer = WshShell.ExpandEnvironmentStrings("%computername%")

strPrefix = Left(strComputer, 4)
 

Set objRegistry = GetObject _

    ("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
 

strKeyPath = "Software\Custom"

strValueName = "Config"

strData = "\\" & strPrefix & "032\Custom\ccpconfig.xml"
 

errReturn = objRegistry.SetStringValue _

    (HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strData)

Open in new window

0
 

Author Comment

by:Lorrec
ID: 21781831
Thank you for the quick response. This gets me started.

I have one more question if you do not mind.
Not all of the registry keys should be set to USME032\Custom\ccpconfig.xml. Would it be possible to have the different changes in the script so it can loop through them them and choose which is correct based on computer name.

For example,

First 4 of Computer Name                             Location of Config file
SCAB                                                      \\SCAB001\custom\config.xml
USMD                                                       \\USMD005\custom\config.xml

Would it be possible to have it look at the computer name and then decide based on the first 4 of that computer name what to change the registry to?
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 21782322
Ok, I think I see what you're saying.  

The revised code below allows you to specify a location for each 4-letter prefix.  Add further locations by following the format of lines 5 and 6.


Const HKEY_LOCAL_MACHINE = &H80000002
 

set objLocations = CreateObject("Scripting.Dictionary")

objLocations.CompareMode = VBTextCompare

objLocations.Add "SCAB", "\\SCAB001\custom\config.xml"

objLocations.Add "USMD", "\\USMD005\custom\config.xml"
 

Set WSHShell = CreateObject("WScript.Shell")

strComputer = WshShell.ExpandEnvironmentStrings("%computername%")

strPrefix = Left(strComputer, 4)

strLocation = objLocations.Item(strPrefix)
 

Set objRegistry = GetObject _

    ("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
 

strKeyPath = "Software\Custom"

strValueName = "Config"

strData = strLocation
 

errReturn = objRegistry.SetStringValue _

    (HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strData)

Open in new window

0
 

Author Comment

by:Lorrec
ID: 21782369
I really appreciate this. You pointed me in the right direction. Thank you for the help.
0
 

Author Closing Comment

by:Lorrec
ID: 31467007
Thanks again for all the help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now