Sign in to follow this  
Followers 0
mistabroadway

Search Registry and change a specific key

4 posts in this topic

Hey guys,

I'm looking to do a registry search for all ODBC connections that are connecting to a specific server that is about to be retired. I then want to point those connections to the new server.

The problem I seem to be running into is the Key may be different depending on the computer.

The String I want to change should be somewhere in this area but the exact key is unknown. \\HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\[RandomKey]\

They old string is Server, REG_SZ, FTWSQL03\MSSQL3

The new string would need to be changed to FTWMSQLP19\MSSQLP19

I'm not really sure how registry search works even though I've seen a few threads referencing it.

;Search to see if the following key exists inside of an unknown key

$OldString = "Server" , "REG_SZ" , "FTWSQL03\MSSQL3")
$NewString = "Server" , "REG_SZ" , "FTWMSQLP19\MSSQLP19")


$Key1 = RegRead("HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI\[Unknown Key 1]", & $OldString
$Key2 = RegRead("HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI\[Unknown Key 2]", & $OldString
;$Key3...
;$Key4...



If
$Key1 = 1 Then
  RegWrite("HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI\[Unknown Key 1]", $NewString
Else
$Key2 = 1 Then
  RegWrite("HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI\[Unknown Key 2]", $NewString
Else
EndIf

I'm sure I'm way off on where I'm going with this since most of this is new to me. My thought was to search all the sub-keys within HKEY_CURRENT_USER\SOFTWARE\ODBC\ODBC.INI for a string that is Server=FTWSQL03\MSSQL3 then change it to Server=FTWMSQLP19\MSSQLP19

Share this post


Link to post
Share on other sites



There was a similar question two days ago and I suggested to have a look at a made for searching the registry.

Maybe this helps.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

perhaps something like this ...

Local $sKey, $sVal
Local $OldString = 'FTWSQL03MSSQL3'
Local $NewString = 'FTWMSQLP19MSSQLP19'
Local $HKCU_ODBC = 'HKEY_CURRENT_USERSOFTWAREODBCODBC.INI'

For $i = 1 To 50
    $sKey = RegEnumKey($HKCU_ODBC, $i)
    If @error <> 0 Then ExitLoop
    $sVal = RegRead($HKCU_ODBC & '' & $sKey, 'Server')
    If @error <> 0 Then ContinueLoop

    MsgBox(0, '', 'Key: ' & $sKey & @CRLF & 'Value: ' & $sVal); <-- debug (have a peek)

    If $sVal = $OldString Then
        RegWrite($HKCU_ODBC & '' & $sKey, 'Server', 'REG_SZ', $NewString)
    EndIf
Next

By the way, you can't do this in AutoIt ...

$OldString = "Server" , "REG_SZ" , "FTWSQL03MSSQL3")

Commas and brackets for a function, must reside within the function.


"Today's scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality." ~ Nikola Tesla

Share this post


Link to post
Share on other sites

Thanks ripdad! That's exactly what I was looking for.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0