Batch - Reg query + if exists doesnt work together? - batch-file

I have a piece of code I want to use
reg query HKLM\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile /v EnableFirewall if %ERRORLEVEL% EQU 0 echo On
This will check if the firewall is enable if so it will echo "On".
I try testing this command but my output is:
ERROR: Invalid syntax.
Type "REG QUERY /?" for usage.
Does anybody knows how to let this code work?
Luseres

Using for you can do something like:
#echo off
for /f "tokens=3 delims= " %%i in ('reg query HKLM\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile /v EnableFirewall') do (
if "%%i" equ "0x1" (
echo activated
) else (
echo not activated
)
)

Related

.Bat/CMD - Hiding "ERROR" with reg.exe >nul 2>&1

FOR /F "tokens=2*" %%A IN (
'REG QUERY "HKLM\Software\EA GAMES\Need for Speed Most Wanted" /v InstallDir'
) DO (set HBMU=%%B)
GOTO END
how I can add If %ERRORLEVEL% == 1 goto CLOSE to my code? because when i put under ) DO (set HBMU=%%B) it does not work.
Try this, after completing the rest of the registry key on line 2:
For /F "Skip=1 Tokens=2*" %%A In (
'Reg Query "HKLM\SOFTWARE\..." /V "InstallDir" 2^>Nul'
) Do Set "HBMU=%%B"
You cannot redirect both to Nul otherwise nothing will be output from the loop as variable %%B
Edit
Here's a complete script which should do what your commented code was supposed to do. (As long as your software definitely places it's information in the registry according to the Operating System architecture).
#Echo Off
Set "EAG=EA GAMES\Need for Speed Most Wanted"
Set "RKM=\"
Reg Query "HKLM\Hardware\Description\System\CentralProcessor\0"^
/V "Identifier" 2>Nul|Find /I "x86">Nul||Set "RKM=\Wow6432Node\"
For /F "EOL=HTokens=2*" %%A In ('Reg Query "HKLM\Software%RKM%%EAG%" /V^
"InstallDir" 2^>Nul') Do Start "" "%%~B\unins000.exe"
Pause

batch command - find multiple texts in seperate lines and output them as columns

i am trying to make an inventory of installed software in domain PCs by following reg command
Reg Query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall /S ^| Find /I "DisplayName"
I am using Find /I "DisplayName" to get software-name. I also need the version number (and may be few more fields later), but its in another line as DisplayVersion.
Since I am running this on multiple computers, my script looks like this:
for /f %%i in (computers_ALL.txt) do (
for /f "tokens=1,2,*" %%j in ('psexec \\%%i Reg Query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall /S ^| Find /I "DisplayName"') do (
echo x64 %%i %%l >>%OUTPUT_FILE%
)
for /f "tokens=1,2,*" %%j in ('psexec \\%%i Reg Query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /S ^| Find /I "DisplayName"') do (
echo x86 %%i %%l >>%OUTPUT_FILE%
)
)
Now I can only find DisplayName. How can I find DisplayVersion which is on another line and add it to second column? My output will be like,
ComputerName, Platform (32-64 bit), Software Name, Software Version
I can take care upto software-name, but having difficulty to get version and put it on second column. Appreciate your help. Thanks.
Following script works against a local Windows machine. Adapting it for a list computers_ALL.txt instead of "%computername%" and adding psexec \\%_comp% should be a simple task.
#ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "OUTPUT_FILE=%temp%\41887529.txt"
for /F %%i in ("%computername%") do (
set "_comp=%%i"
call :proHive "x64" "\Software\Microsoft\Windows\CurrentVersion\Uninstall"
call :proHive "x86" "\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
)
rem debugging output
type "%OUTPUT_FILE%"
ENDLOCAL
goto :eof
:proHive
rem %1 platform
rem %2 registry key
>>"%OUTPUT_FILE%" (
for /f "tokens=*" %%G in ('
Reg Query "HKLM%~2" /S /V DisplayName ^| Find /I "HKEY_LOCAL_MACHINE%~2"
') do (
set "_DisplayName="
set "_DisplayVersion="
for /f "tokens=1,2,*" %%j in ('
Reg Query "%%G" ^| Findstr /I "DisplayName\> DisplayVersion\>"') do set "_%%j=%%l"
SETLOCAL EnableDelayedExpansion
echo %_comp%,%~1,!_DisplayVersion!,!_DisplayName!
ENDLOCAL
)
)
goto :eof
#ECHO OFF
SETLOCAL
SET "output_file=u:\report.txt"
DEL "%output_file%" /F /Q
for /f %%i in (q41887529.txt) do (
SET "compname=%%i"
CALL :clrsoft
FOR %%z IN (\ \Wow6432Node\) DO (
for /f "tokens=1,2,*" %%j in (
'Reg Query HKLM\Software%%zMicrosoft\Windows\CurrentVersion\Uninstall /S ^| Findstr /I "DisplayName DisplayVersion HKEY_LOCAL_MACHINE\\"'
) do (
IF /i "%%j"=="Displayname" (
SET "softname=%%l"
) ELSE (
IF /i "%%j"=="Displayversion" (
SET "softver=%%l"
) ELSE (
CALL :report %%z
)
)
)
REM processed all data - final report
CALL :report %%z
)
)
GOTO :EOF
:: Report routine parameters :
:: %1 indicates x64 ("\") or x86 (\Wow6432Node\)
:report
IF NOT DEFINED softname GOTO clrsoft
IF "%1"=="\" (
ECHO "%compname%",x64,"%softname%","%softver%">>"%output_file%"
) ELSE (
ECHO "%compname%",x86,"%softname%","%softver%">>"%output_file%"
)
:clrsoft
SET "softname="
SET "softver="
GOTO :EOF
Naturally, you'd need to set up your own output filename. I've also removed the psexec \\%%i and set up my machine name in q41887529.txt for testing on my machine.
Since the only difference between the queries is the node, I combined the steps and controlled them using %%z.
Clear the tempvars, then process each return line for one of the three target strings.
If it's a name, then call :report to report any accumulated data, then record the name
If it's a version, record the version and since this should now give us a pair, report it.
If it's neither, then it must be a new key, so report what we have so far.
The wart is that the version need not exist - and the final entry may be versionless, so we try reporting again to ensure any accumulated data is output.
I've revised this now and replaced the original.
It will report the machine name extracted from the file and I believe that the often-missing version number has been fixed - the order of name and version are unpredictable, so the report is triggered with the data accumulated between instances of HKEY_ and at the very end.

Conditional If Statement Syntax in Batch

I have checked several resources on conditional if statement syntax for batch scripts, and despite this, I'm unable to find my mistake. I typically avoid asking questions here that should be this simple, but it's the last task on a huge project so I'm hoping someone can help me see what I'm doing wrong. This is a script for a university project, and is merely for an exploration of concepts.
#Echo OFF
REM Check time remaining
for /f "tokens=3 delims=: " %%a in (
'cscript //nologo "%systemroot%\system32\slmgr.vbs" /dli ^| find "Time remaining: "'
) do set "timeRemainingStatus=%%a"
Echo %timeRemainingStatus%
pause
REM Check SkipRearm dword value
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform"
set VALUE_NAME=SkipRearm
FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)
Echo Value Name: %ValueName%
Echo Value Value: %ValueValue%
pause
IF %timeRemainingStatus% EQU 20160 (
Echo First level
pause
IF defined ValueName (
Echo Second Level
pause
IF %ValueValue% == "0x0" (
Echo Third Level
pause
regedit.exe /s "C:\Windows\SR.reg"
)
) ELSE (
Echo Fourth Level
pause
SLMGR /REARM
)
)
pause
I have the echos and pauses in there so I can see what code runs and to verify that the variables are retaining their values. It crashes for me after the second pause (after the Echo Value Value: %ValueValue% command). I assume it's something wrong with syntax, but I've been unable to see any errors because the command prompt closes, despite all the pauses I have.
There were a few things wrong. This is the solution I went with:
#Echo OFF
REM Check time remaining
for /f "tokens=3 delims=: " %%a in (
'cscript //nologo "%systemroot%\system32\slmgr.vbs" /dli ^| find "Time remaining: "'
) do set /a "timeRemainingStatus=%%a"
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform"
set VALUE_NAME=SkipRearm
FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)
IF %timeRemainingStatus% EQU 0 (
IF defined ValueName (
IF "%ValueValue%" == "0x0" (
regedit.exe /s "C:\Windows\SR.reg"
)
IF "%ValueValue%" == "0x1" (
SLMGR /REARM
)
)
)
I needed to add /a to set (which I thought I had before, but I guess not). Additionally, I forgot quotes when comparing %ValueValue% to "0x0". While it may not be the cleanest way of doing things, I also changed the if-else into two if statements.
I've tested this at startup on a VM and it appears to be working properly. Much appreciated to the help given.
When setting variables within for loops or if statements, you'll need to take advantage of Setlocal EnableDelayedExpansion. This also applies when referencing variables set in this context. Any variables that use EnableDelayedExpansion will need to have exclamation points around the variable instead of percentages (!Value! instead of %Value%). You'll need to change your code to take advantage of this.

Getting errorlevel from usebackq

i've ran into a problem with adding registry keys using reg.exe via batch.
I'm using this:
FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\iNiT" /v Basic`) DO (
SET BASICVALUE=%%A %%B
)
ECHO ERROR: %ERRORLEVEL%
EDIT:
Normaly REG.exe outputs a errorlevel when executed;
0 - Successful
1 - Failed
And i get:
ERROR:
Somehow the errorlevel gets wiped or not saved.
I need to get the %errorlevel% out of it when executed, how do i do this, this dosn't seem to work.
Can you somehow set the errorlevel to a variable? I've tested this:
FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\iNiT" /v Basic && SET ERROR=%ERRORLEVEL%`) DO (
SET BASICVALUE=%%A %%B
)
ECHO %ERROR%
Nor does that work.
%% wasn't expected
Any information would be helpful :)
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\iNiT" /v Basic > tempFile.txt
echo %errorlevel%
FOR /F "usebackq tokens=3*" %%A IN (tempFile.txt) DO (
SET BASICVALUE=%%A %%B
)
I've used the below code in one of my script and may answer your question -
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\VisualSVN\VisualSVN Server" /V RepositoriesRoot >nul 2>&1 & if %errorlevel%==1 (echo.Visual SVN not installed)
Cheers, G

Using a variable in Reg Queries

I have the below which is meant to do the following:
Work out all the SIDs in HKEY_USERS and then use that variable in reg query to check for the existance of a key for each HKEY_USER. However, it is telling me it is an invalid key because it basically just misses out the %%~na when it sets hkeyuserpath and then fails on the reg query. What am I doing wrong?
for /f %%a in ('reg query HKEY_USERS') do (
echo %%~na
set hkeyuserpath="HKEY_USERS\%%~na\Software\Microsoft\Windows\CurrentVersion\Run"
reg query %hkeyuserpath% /v *WhatIamLookingfor*
if "%ERRORLEVEL%" EQU "0" goto HELLO
if "%ERRORLEVEL%" EQU "1" goto GOODBYE
:HELLO
echo Hello
GOTO END
:GOODBYE
GOTO END
)
:END
pause
You're setting hkeyuserpath inside of a for loop, so you have to use delayedexpansion to access the variable.
setlocal enabledelayedexpansion
for /f %%a in ('reg query HKEY_USERS') do (
echo %%~na
set hkeyuserpath="HKEY_USERS\%%~na\Software\Microsoft\Windows\CurrentVersion\Run"
reg query "!hkeyuserpath!" /v *WhatIamLookingFor*
if not errorlevel 1 (
Echo(Hello & goto :end
) ELSE (
Echo(Goodbye
)
)
:end
pause

Resources