Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1361
  • Last Modified:

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

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
Lorrec
Asked:
Lorrec
  • 3
  • 2
1 Solution
 
Shift-3Commented:
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
 
LorrecAuthor Commented:
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
 
Shift-3Commented:
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
 
LorrecAuthor Commented:
I really appreciate this. You pointed me in the right direction. Thank you for the help.
0
 
LorrecAuthor Commented:
Thanks again for all the help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now