Page 1 of 1

Reg Query - return all values from a registry key

Posted: 15 Oct 2010 02:21
by bob_carswell
I am trying to use the DOS Batch script GetRgValues to recover all the vaules from a specific registry key that holds the SQL Server Instances on my server but without success.

I am not an expert on the registry and would like some help please with what is needed to be entered for the variable %~2 in the script I have Googled for information on the Structure of Registry Values and the Microsoft Technet article cc776231 helps to understand Registry structure but is not helping me to understand what I need to enter in the script for %~2

The registry key I am using is HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL which has values for three named instances that I want to extract into either a text or csv file and I think GetRegValues will help me do this if I can get it to work.

Re: Reg Query - return all values from a registry key

Posted: 16 Oct 2010 06:53
by aGerman
Sry but I don't get it.
To get the entire content (including sub keys and all the recursive values) you could write.

Code: Select all

>reg.txt reg query "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL" /s

But what exactly should come to three different text files?
Maybe you could post the output of my example code and explain what you try to do.

Regards
aGerman

Re: Reg Query - return all values from a registry key

Posted: 18 Oct 2010 01:48
by bob_carswell
Thanks for response, the result of your code gives me this, there is a blank line at the top of the text file then the three lines of text.

<blank line>
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL
MIS REG_SZ MSSQL.1
SIMS2008 REG_SZ MSSQL10.SIMS2008



What I am trying to achieve is extracting the two SQL Server named instances of MIS and SIMS2008 out into a csv or text file to be used in another batch file as the values of a DOS variable as they point to different file directory locations for SQL Server, one is SQL Server 2005 and the other is SQL Server 2008.

I wanted to run a FOR statement and extract out only MIS and SIMS2008 (we have 30 different SQL Server installations in our organisation and each will have different SQL Server names)

When I use the Reg Query text inside a FOR statement it only give me the last line of
SIMS2008 REG_SZ MSSQL10.SIMS2008

What I want out into a csv or text file is the only
MIS
SIMS2008

Bobc

Re: Reg Query - return all values from a registry key

Posted: 18 Oct 2010 10:35
by aGerman
OK, have a look at these two lines:

Code: Select all

for /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL" /v "MIS" 2^>nul') do set "MIS=%%b"
for /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL" /v "SIMS2008" 2^>nul') do set "SIMS2008=%%b"


You will find the data of MIS in variable %MIS%, the same for SIMS2008.
If the values were not found these variables are not defined.

Now you could use the variables directly or you could redirect them to a file using ECHO.

Regards
aGerman

Re: Reg Query - return all values from a registry key

Posted: 19 Oct 2010 05:32
by bob_carswell
Hi again, thanks for your response it set me onto the path of understanding what was needed to get out the actual SQL Server Named Instance values from the HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL registry key.

Code: Select all

@echo off
SetLocal EnableDelayedExpansion
if exist %~dp0Active_Instances.csv (del %~dp0Active_Instances.csv)

for /f "skip=2" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL" /s') do (
   set str=%%a
   echo !str!>> %~dp0Active_Instances.csv
)

start %~dp0Active_Instances.csv

:END
EndLocal
exit /b


I am gratful for your help in solving the problem.

Brgds
:wink: