问题
This script is only a portion of about 200-300 registry statements from the original full file. It sets all registry settings as per each reg add statement.However, applying this script obviously could break something or everything. The script will be applied on a Windows 2008 R2 server. Original statement
reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa" /v RestrictAnonymous /t REG_DWORD /d 1 /f
What I want to accomplish before applying this script is: 1. Query each key to check if key exists with the correct data 2. If key and correct data exists, no action needed 3. If key nor data exists, redirect output to file a. I would like for the result to be displayed along with the registry key that does not match The redirected output for the non-matching registry keys will be the ones I will manually review.
The following represents my attempt to derive the data I am requesting. I have applied many different switches (/f /d |
(pipe) with findstr
) and redirections in many different ways all with no luck. I also reviewed the reg compare command but it is not applicable because I am not using another client to compare the registry against. For some reason, I cannot get any combinations of switches and commands to work as desired. If I get the desired output, the findstr
is not validating correctly. Or, if the findstr
validates correctly, the output does not show. The findstr
results seems to be the logical statement but the results are not validating correctly
ex. >> C:\Users\AIODUDE\Documents\results.csv
reg query"HKLM\Software\_reg_test" /v STIG_test | findstr /E "0"
if %ERRORLEVEL% EQU 0 echo I match >> C:\Users\AIODUDE\Documents\results.csv
if %ERRORLEVEL% NEQ 0 echo No match >> C:\Users\AIODUDE\Documents\results2.csv
Remember, this script has about 200-300 registry add statements.
::all reg add keys are only set to do not read during testing
::reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa" /v RestrictAnonymous /t REG_DWORD /d 1 /f
reg query "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa" /v RestrictAnonymous > D:\results.csv
if %ERRORLEVEL% EQU 0 echo I match >> D:\results.csv
if %ERRORLEVEL% NEQ 0 echo No match >> D:\results.csv
::reg add "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\EventLog\Setup" /v MaxSize /t REG_DWORD /d 32768 /f
reg query "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\EventLog\Setup" /v MaxSize >> D:\results.csv
if %ERRORLEVEL% EQU 0 echo I match >> D:\results.csv
if %ERRORLEVEL% NEQ 0 echo No match >> D:\results.csv
::reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Installer" /v AlwaysInstallElevated /t REG_DWORD /d 0 /f
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Installer" /v AlwaysInstallElevated >> D:\results.csv
if %ERRORLEVEL% EQU 0 echo I match >> D:\results.csv
if %ERRORLEVEL% NEQ 0 echo No match >> D:\results.csv
pause
Endoro--I answered here because of the comments limitation.
Your statement gives me most of the data I need thus far--thanks for that!. I have tried dozens of various constructs! The findstr
matches the exact data that I need. The below examples work with the exception of redirecting the output if the key is not found. I decided to use set statements to make editing the hundreds of lines a lot easier:
set _results=C:\Users\AIODUDE\Documents\xresultsd.txt
set _NEQ=%_results% 2>&1 && echo success || echo FAIL >> %_results%
set _EQU=%_results% 2>&1 && echo success || echo FAIL >> %_results%
THIS EXAMPLE IS GOOD If it matches, the output states (I match--so this key is good and no further action).
reg query "HKLM\Software\_reg_test" /f STIG_test /e >>%_results%
reg query "HKLM\Software\_reg_test" /v STIG_test | findstr "0"
if %ERRORLEVEL% NEQ 0 echo No match >>%_NEQ%
if %ERRORLEVEL% EQU 0 echo I match >>%_EQU%
THIS EXAMPLE HAS A NON-MATCHING DATA VLAUE Action is needed.
REG Query "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa" /f RestrictAnonymous /e >>%_results%
reg query "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa" /v RestrictAnonymous | findstr "1"
if %ERRORLEVEL% NEQ 0 echo No match >>%_NEQ%
if %ERRORLEVEL% EQU 0 echo I match >>%_EQU%
In this example, the first reg query statement searches only the specific key and prints out the key and data value. However, it does not ID what the correct value should be hence... The second reg query statement does ID the correct value but does not print out what that value should be (this statement will need action--in this case this case data value should be 0.
THIS EXAMPLE REGISTRY KEY IS NOT FOUND Action is needed.
reg query "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa" /f LmCompatibilityLevel /e >>%_results%
reg query "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa" /v LmCompatibilityLevel | findstr "5"
if %ERRORLEVEL% NEQ 0 echo No match >>%_NEQ%
if %ERRORLEVEL% EQU 0 echo I match >>%_EQU%
So the findstr
(shows me the specific data I need to add or modify), if it could print from all statements would be the ideal and limit my search and editing to only the value data that does not match and if the key is not already present.
回答1:
try somewhat like this:
reg query "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa" /v RestrictAnonymous >results.csv 2>&1 && echo success || echo FAIL >> results.csv
type results.csv
来源:https://stackoverflow.com/questions/17185197/how-to-redirect-batch-reg-query-output-with-findstr