I have multiple registry keys called
O365BusinessRetail - en-us
O365BusinessRetail - de-de
... and so on for many languages
I want to check if the registry keys exist or not. But this command will not work "Registry key could not be found"
reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\O365BusinessRetail*"
IF %ERRORLEVEL% == 1 exit 1
If %ERRORLEVEL% == 0 exit 0
I also tried
reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall" /v ? | findstr /C "O365BusinessRetail*"
IF %ERRORLEVEL% == 1 exit 1
If %ERRORLEVEL% == 0 exit 0
However with Powershell it works but I can't use it because Powershell is blocked on our side
if (Test-Path -Path registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\O365BusinessRetail*) {
exit 1 # same as: Write-Output 1
}
else {
exit 0 # same as: Write-Output 0
}
What is the best way to do that in CMD?
Thanks
Your submitted code should look like this:
#SystemRoot%\System32\reg.exe Query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /F "O365BusinessRetail*" /K 1>NUL 2>&1
#Exit %Errorlevel%
If one or more subkeys beginning with the string O365BusinessRetail exist, the script will exit with the 0 error code. If none exist, it will exit with a 1 error code.
Please also note that it is possible that the applications are registered under another uninstall key branch or root key.
Related
I am executing a windows bat script through jenkins. Batch file is giving the desired output,however build is failing.My batch file is..
cd /d D:\\Bank\\Member\\ID
if %errorlevel% neq 0 exit /b %errorlevel%
mkdir OTP
if %errorlevel% neq 0 exit /b %errorlevel%
robocopy C:\Corporate D:\\Bank\\Member\\ID\ /E /XF *.bat
if %errorlevel% neq 1 exit /b %errorlevel%
cd /d D:\\Bank\\Staff\\ID
ROBOCOPY GIVES EXIT CODE 1 AFTER SUCESSFULLY COPYING FILES.
BUT JENKINS FAILS BUILD AND GIVING BELOW ERROR:
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
I Want the build to be successful if robocopy exits code 1.
My best advise would be to use jenkins-pipeline, try/catch block, and use bat commands as few as possible (or do not use at all).
But considering your case there's a simple solution as well: just set the field "ERRORLEVEL to set build unstable" to 1 (or other suitable number). The field appears if you click "Advanced" button under the "Execute Windows batch command" block:
This method will check your build as "Unstable", but will continue to execute.
please use like following to avoid:
bat "robocopy /s source dest & EXIT /B 0"
The above will continue the jenkins build even if robocopy returns non-zero errorlevel. Robocopy does not return 0 for various reasons even after successfull copy, as it compared the two folders. Please lookup for it's return code to know more details
As mentioned here, the first criteria to check is the account used to run Jenkins.
Type services.msc to open the Windows services and look for the Jenkins service.
Instead of "Local Service Account", use your own account: that will avoid any right issue.
But: the other criteria is to display the error code.
As mentioned here:
All exit codes up to '3' are fine.
So after robocopy, you can add:
#echo robocopy exit code: %ERRORLEVEL%
#if %ERRORLEVEL% GTR 3 ( echo robocopy ERROR )
#if %ERRORLEVEL% GTR 3 ( exit %ERRORLEVEL% )
#set ERRORLEVEL=0
REM at the end:
exit /b 0
That will ensure Jenkins don't fail the batch step, even though the original error level for robocopy was 1.
in C:\Easy_ERROR there is only 3 files. when only in 1 file you can find the string 'alexm'
#echo off
#break off
#color 0a
#cls
FOR %%a IN (C:\Easy_ERROR\EIM*.txt) DO (
find /c /i "ALEXM" C:\Easy_ERROR\%%~nxa
IF %errorlevel% EQU 0 ECHO FOUND
)
)
pause
exit
when you run it, the statement:
IF %errorlevel% EQU 0 ECHO FOUND
always writing me "FOUND" for all 3 files!
its only an exemple for something else that im trying to do. but the same case!
Variables are expanded at load time. Your entire FOR construct is loaded as 1 line... regardless of how many lines it contains. So the value for %errorlevel% is NOT the run time value that you are expecting. Replace this line
IF %errorlevel% EQU 0 ECHO FOUND
with this
IF not errorlevel 1 ECHO FOUND
See IF /?
Note that this specifies a true condition if errorlevel is NOT equal to or greater than 1. So this would not work if a negative value was returned, as it too is less than 1.
I have a simply windows batch command (robocopy) that returns zero errors but is always marked as a failure in Jenkins. I would like to know why?
D:\Jenkins\jobs\Jenkins Config Backup\workspace>exit 1
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
robocopy returns a bit map
For details see here: http://ss64.com/nt/robocopy-exit.html
In summary: All exit codes up to '3' are fine.
This is the batch file code I usually use:
set SOURCE= ...
set DESTINATION= ...
robocopy /MIR %SOURCE% %DESTINATION%
#echo robocopy exit code: %ERRORLEVEL%
#if %ERRORLEVEL% GTR 3 ( echo robocopy ERROR )
#if %ERRORLEVEL% GTR 3 ( exit %ERRORLEVEL% )
#set ERRORLEVEL=0
You could also do a "goto" and not exit.
Jenkins marks a build as failed when the exist code of a batch script is not 0. If robocopy is the last command in your script, the robocopy exit code will be taken.
Robocopy does not adhere to the standard that an exit code other then 0 means a failed build. You need to evaluate the robocopy exit code and end your script with exit 0 or exit 1 depending on the success of robocopy.
Have a look at the robocopy exit codes.
Updating this question for jenkins pipelines - this works for me:
def robocopy(cmd)
{
// robocopy uses non-zero exit code even on success, status below 3 is fine
def status = bat returnStatus: true, script: "ROBOCOPY ${cmd}"
println "ROBOCOPY returned ${status}"
if (status < 0 || status > 3)
{
error("ROBOCOPY failed")
}
}
Alternatively, you may want to look at the File Operations Plugin
i'm trying to check if there is a java installation on the machine with a batch file,and install java if necessary. However, the pipe doesnt work, and i get the reg query's result on the screen. Here is the code:
#ECHO OFF
cls
:checkjava
reg query "HKLM\Software\JavaSoft\Java Runtime Environment" | find "ERROR" > NUL
if %errorlevel% == 0
...
reg query "HKLM\Software\JavaSoft\Java Runtime Environment" 2>&1 | find "ERROR" > NUL
the error message is printed in error stream so you need to redirect it in the &1.
Here's more info : http://www.robvanderwoude.com/redirection.php
try this:
reg query "HKLM\Software\JavaSoft\Java Runtime Environment" >nul 2>&1 && goto:OK || goto:fail
:OK
echo reg key found
pause
goto:eof
:fail
echo ERROR key not found!
pause
goto:eof
Error messages are sent to Standard Error and to Standard Output hence you need to redirect Standard Error to Standard Output before running find on the output.
reg query "HKLM\Software\JavaSoft\Java Runtime Environment" 2>&1 | find "ERROR" > nul
Another thing is that reg query itself returns 0 or 1 on success or failure
Return Code: (Except for REG COMPARE)
0 - Successful
1 - Failed
So you may not have to use find at all.
reg query "HKLM\Software\JavaSoft\Java Runtime Environment" > nul 2> nul
if %errorlevel% == 0 goto success
echo "Not found"
goto end
:success
echo "Found"
:end
I am just printing Found/Not found - but you can take whatever action you want.
I'm trying to make a .bat toggler for certain Explorer settings. To do this I need the batch file to query the Registry key's data and then set the key accordingly. For example, in ActionScript 3 or JavaScript it would be something along the lines of this:
if (HideFileExt == "00000000"){
HideFileExt = 00000001;
else {
HideFileExt = 00000000;
}
This way, every time it runs it will set the key's data to be the opposite of what it currently is - a toggler.
I have Google-d this extensively and after quite a long time of chopping up and splicing multiple examples, I eventually got this:
REG QUERY HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced /v HideFileExt /t REG_DWORD /d 00000000
if errorlevel 1 (
echo Num 1
) else (
echo Num 2
)
rem The "echo Num"s are there just so that I could eventually figure out what the errorlevel does
which returns the error:
ERROR: Invalid syntax.
Type "REG QUERY /? for usage.
num 1
If I remove the /d 00000000 from the REG QUERY then it returns the accurate data value of the key without error. I have also tried it with /d 0, /d 0x0 and /d 0x00000000 and they didn't work either.
The /d switch doesn't do what you think. It is a modifier to the /f switch, which is used to specify a search pattern. Unfortunately, /v already defines a search pattern, and they do not get along.
To check whether HideFileExt is set to 0, you can pipe reg's result to find:
reg query HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced /v HideFileExt | find "0x0"
if errorlevel 1 echo "HideFileExt is 0"
if errorlevel 0 echo "HideFileExt is not 0"
The Answer from Dennis is correct, but I thought id paste the whole batch file so you can see it all working.
REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "HideFileExt" | Find "0x0"
IF %ERRORLEVEL% == 1 goto turnoff
If %ERRORLEVEL% == 0 goto turnon
goto end
:turnon
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v
HideFileExt /t REG_DWORD /f /D 1
goto end
:turnoff
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v
HideFileExt /t REG_DWORD /f /D 0
goto end
:end
#exit
In 2020 this is how you can do it:
In cmd write:
powershell -executionpolicy unrestricted C:\path_to_powershell_file.ps1
Make a powershell file with the following code and save it (Example to check if chrome is default browser):
$path = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.html\UserChoice'
$value = 'ProgId'
$path_verif = Get-itemProperty -Path $path | Select-Object -ExpandProperty $value
if($path_verif -match 'chrome'){
Write-Host "Chrome is default browser" -ForegroundColor Green
} else{
Write-Host "Chrome is NOT default browser. -ForegroundColor Red
}