Link to home
Start Free TrialLog in
Avatar of nssasikumar
nssasikumarFlag for India

asked on

Reading a Value Data from Registry key list

Hi here is my scenario,
I need to return the Data from Registry Key  and export in to html report by getting Input from txt file with Reg Key and Reg Value separate by commas.
I manage get script read and export to html (Script1), but need to modify the Script2 to read from file which I tried but missing out something. I am not getting Value Data for each Reg Key in the reginput.txt

Here is my script:
Script1: 
FOR /F "skip=2 tokens=2,*" %%A IN ('reg query "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa" /v "restrictanonymous"') DO set "Output=%%B"
ECHO Result = %Output%

Script2:
@echo off & setlocal enabledelayedexpansion
for /F "tokens=1-2 delims=," %%A in (.\reginput.txt) do (
Set _var1=%%A
  Set _var2=%%B
echo !_var1!
echo !_var2!
(
FOR /F "skip=2 tokens=2,*" %%i IN ('reg query !_var1! /v !_var2!') DO set /a "OPT=%%B"
))
ECHO Output = %OPT%

Open in new window

Attached the Text file having the Reg Key and Reg Value
reginput.txt
Avatar of Bill Prew
Bill Prew

Give this a try:

@echo off
setlocal EnableDelayedExpansion

for /F "tokens=1-2 delims=," %%A in (.\reginput.txt) do (
  set key=%%A
  set val=%%B
  for /f "skip=2 tokens=2,*" %%i in ('reg query !key! /v !val!') do (
    set /a "OPT=%%j"
    echo Output = !OPT!
  )
)

Open in new window

Which can even be condensed to:
@echo off
setlocal EnableDelayedExpansion

for /F "tokens=1-2 delims=," %%A in (.\reginput.txt) do (
  for /f "skip=2 tokens=2,*" %%i in ('reg query %%A /v %%B') do (
    set /a "OPT=%%j"
    echo Output = !OPT!
  )
)

Open in new window

~bp
Avatar of nssasikumar

ASKER

Hi Bill,
Thanks for correcting the script, I got the output for Numeric value, but I need output as follows:
1. Need to return string values
2. If RegKey or RegValue is not avail it should return msg as not avail
3. Also need to assign the return values to a variable and printout
eg: the value for Regkey1 is 1
      The value for Regkey2 is 0
Do you want me to raise separate question?, Need help on the same
Can you explain a bit more about how this code will be used?  That would help me create a solution that better meets your needs.

In addition, a few questions on your feedback:

1. Need to return string values

What other types of registry data do you need to handle, I went by the actual values you provided in your post, which were both numeric.  The following data types exist in registry values, which ones are you needing?

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724884%28v=vs.85%29.aspx

2. If RegKey or RegValue is not avail it should return msg as not avail

Is this message just written to the console?

3. Also need to assign the return values to a variable and printout
eg: the value for Regkey1 is 1
      The value for Regkey2 is 0


What names should be used for each BAT variable created?  If they are just a sequential ordered numbering (like Var1, Var2, Var3, ...) how will you know what they mean later in the BAT script when you need to reference them?

Again, if you describe the larger picture of how you intend to use this it will save time and get us to a better solution I think.

~bp
Just to give you some additional ideas on what we could do, here is another version that reports missing reg entries, and will handle strings.  It also places the found values into environment variables var1, var2, var3, ...

@echo off
setlocal EnableDelayedExpansion

set i=1
for /F "tokens=1-2 delims=," %%A in (.\reginput.txt) do (
  set var!i!=
  for /f "tokens=2*" %%i in ('reg query "%%~A" /v "%%~B" 2^>nul ^|findstr /I /L /C:"REG_"') do (
    set "var!i!=%%j"
  )
  if defined var!i! (
    set /a i+=1
  ) else (
    echo.Registry entry not found for:
    echo.  KEY:%%~A
    echo.  VALUE:%%~B
  )
)

set var

Open in new window

~bp
Hi Bill,

1) I need to generate HTML report by returning the Data from Registry  Value (REG_SZ, REG_DWORD).
2) If the Registry Value not avail it should given me MSG "Reg Value not avail", If avail it should return the Data
3) Can use any variable name, I need the output in HTML like:
HKLM\System\CurrentControlSet\Services\eventlog\Application,MaxSize :  20971520
HKLM\System\CurrentControlSet\Services\eventlog\Application,DisplayNameFile : %systemroot%\system32\wevtapi.dll
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial