Jump to content

Search Registry and change a specific key


Recommended Posts

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

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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.

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...