I am writing a windows batch script that will Install a service. First, I need to find if the service already exists. If the service exists, it has to check the state. If the state is running, it has to stop and delete the service.
This is my code : test.bat. I am running this from command line.
for /F "tokens=3 delims=: " %%H in ('sc query "IBMLibertyProfile" ^| findstr "STATE" ') do (
if /I "%%H" EQ "RUNNING" (
sc stop "IBMLibertyProfile"
)
)
I am getting error :
C:>test1.bat EQ was unexpected at this time.
C:> if /I "%H" EQ "RUNNING" (
How to solve this error?
Try this:
#rem If the service doesn't exist, exit.
#sc query IBMLibertyProfile > NUL 2>&1
#if %ERRORLEVEL% neq 0 #exit /b 0
#rem If the service is already stopped, delete it.
#sc query IBMLibertyProfile | findstr /s "STOPPED" > NUL 2>&1
#if %ERRORLEVEL% neq 0 #goto :DeleteService
#rem No matter it's state, tell it to stop.
#sc stop IBMLibertyProfile
#rem Wait for it to stop.
#set _WaitLoopCount=0
:StoppedWait
#if _WaitLoopCount equ 10 #goto :ServiceWaitTimeout
#timeout /t 3 > NUL 2>&1
#sc query IBMLibertyProfile | findstr /s "STOPPED" > NUL 2>&1
#if %ERRORLEVEL% neq 0 #goto :StoppedWait
#rem Delete the service and exit.
:DeleteService
#sc delete IBMLibertyProfile
#exit /b 0
:ServiceWaitTimeout
#echo Service failed to reach the STOPPED state. Reboot the computer and try again.
NOTE: If your service is not well behaved, the script might hang. I'll leave it to you to work out how to deal with sc delete failures.
Related
I am trying to delete a folder on Windows server if a certain condition is met. If it is not met, then a wait for 10 seconds and loop around, check for the condition again. I also need to make sure that I am not in the loop forever. (Check if I am in the loop for more than 60 seconds, then get out of the loop). The batch file looks something like this:
C:\postgresql\uninstall-postgresql.exe --mode unattended
set TIMESTAMP1=%TIME%
:deleteFolder
tasklist /V |findstr /i "_uninstall*" >nul
if %errorlevel% == 0 (timeout /T /10 >nul
set TIMESTAMP2=%TIME%
**REM I want to make sure that we get out of this loop if the diff b/w TIMESTAMP2
AND TIMESTAMP1 IS MORE THEN 60 SECONDS**
goto deleteFolder
) ELSE (
if exists C:\postgresql RD /Q /S C:\postgresql)
Command 1
Command 2
Command 3
So, I am trying to uninstall Postgresql from a windows server, making sure that the uninstall is complete by checking the tasklist and then delete the basedir (C:\postgresql). If the uninstall process is still running, then wait for 10 seconds and check the tasklist again. I just want to make sure that I am not stuck in the loop forever.
Thanks in advance
Using timestamps and calculating time difference:
#echo off
setlocal EnableDelayedExpansion
C:\postgresql\uninstall-postgresql.exe --mode unattended
set "startTime=%time: =0%"
set "endTime="
:deleteFolder
tasklist /V |findstr /i "_uninstall*" >nul
if %errorlevel% == 0 (
goto waitAndDeleteFolder
) else (
goto cleanup
)
:waitAndDeleteFolder
timeout /T 10
set "endTime=%time: =0%"
set "end=!endTime:%time:~8,1%=%%100)*100+1!" & set "start=!startTime:%time:~8,1%=%%100)*100+1!"
set /A "elap=((((10!end:%time:~2,1%=%%100)*60+1!%%100)-((((10!start:%time:~2,1%=%%100)*60+1!%%100), elap-=(elap>>31)*24*60*60*100"
if !elap! gtr 6000 goto done
goto deleteFolder
:cleanup
if exist "C:\postgresql" RD /Q /S "C:\postgresql"
goto done
:done
The simpler method would be to utitlise a count to break the loop
#Echo off & Set Count=0
C:\postgresql\uninstall-postgresql.exe --mode unattended
:deleteFolder
If "%Count%"=="6" Goto :Failed
tasklist /V |%__AppDir_%findstr.exe /lic:"_uninstall" >nul 2> Nul && (Timeout /T 10 /Nobreak > Nul & <Nul Set /P "=." & Set /A "count+=1" & Goto :deleteFolder) || Goto :Post
:Post
if exist C:\postgresql (
RD /Q /S C:\postgresql && Echo/Task Completed
) Else Echo/C:\postgresql Absent
Goto :Eof
:Failed
Echo/Task failed to complete in the allocated time
Goto :Eof
I would however suggest a more robust approach on your part to identifying the task
I have a program which has following flow. Problem is the windows batch file doesn't properly checks errorlevel and doesn't set KILLSTS value. Could you please let me know what's wrong with this program and how to fix this?
Ask user to open an exe
if Yes
check exe is running or not
if running, ask user whether to close that exe
if yes close exe
run the exe
else
exit
Here is the sample batch file.
#ECHO OFF
#REM SETLOCAL ENABLEEXTENSIONS
SET /P AREYOUSURE="Open Spring STS [y/n]>"
set AREYOUSURE=%AREYOUSURE:~0,1%
ECHO AREYOUSURE=%AREYOUSURE:~0,1%
IF /I %AREYOUSURE% == N (
SET /A errno^|=%ERROR_OTHERCOMMAND_FAILED%
echo Existing Batch
EXIT /B %errno%
)
SETLOCAL
#REM SET KILLSTS=Y
tasklist /fi "IMAGENAME eq STS.exe" |find ":" > nul
ECHO Error %errorlevel%
IF %errorlevel% neq 0 (
SETLOCAL
SET /P KILLSTS="Spring STS is running. Kill STS Process [y/n]>"
echo KILLSTS %KILLSTS%
set KILLSTS=%KILLSTS:~0,1%
echo KILLSTS AFTER SUBSTR %KILLSTS%
IF /I %KILLSTS% == Y TASKKILL /f /im "STS.exe"
ENDLOCAL
)
START "" "C:\sts-bundle\sts-3.8.3.RELEASE\STS.exe"
I am getting below error
You need to learn how to properly format if statements.
You are formatting them as:
IF /I %KILLSTS% == Y TASKKILL /f /im "STS.exe"
When they should be formatted as:
if /i "%KILLSTS%"=="Y" (TASKKILL /f /im STS.exe)
The formatting doesn't really matter as such in simple batch files, but it's best to use the correct syntax which can handle special characters such as SPACES, AMPERSANDS, QUOTES, PIPE for when more complex variables are involved.
Updated script:
#ECHO OFF
#REM SETLOCAL ENABLEEXTENSIONS
SET /P "AREYOUSURE=Open Spring STS [y/n]>"
set "AREYOUSURE=%AREYOUSURE:~0,1% "
echo "AREYOUSURE=%AREYOUSURE:~0,1%"
IF /I "%AREYOUSURE%"=="N" (
SET /A errno^|=%ERROR_OTHERCOMMAND_FAILED%
echo Existing Batch
EXIT /B %errno%
)
SETLOCAL
#REM SET KILLSTS=Y
tasklist /fi "IMAGENAME eq STS.exe" | find ":" > nul
ECHO Error %errorlevel%
IF "%errorlevel%" neq "0" (
call :escapeexpansion
)
START "" "C:\sts-bundle\sts-3.8.3.RELEASE\STS.exe"
exit /b
:escapeexpansion
SETLOCAL
SET /P "KILLSTS=Spring STS is running. Kill STS Process [y/n]>"
echo KILLSTS %KILLSTS%
set "KILLSTS=%KILLSTS:~0,1%"
echo KILLSTS AFTER SUBSTR %KILLSTS%
IF /I "%KILLSTS%"=="Y" TASKKILL /f /im "STS.exe"
ENDLOCAL
goto :EOF
The entire structure seems wrong to me; as well as pointlessly using SET /P instead of CHOICE.
#ECHO OFF
TASKLIST /FI "IMAGENAME eq STS.exe"|FIND ":">NUL 2>&1&&GOTO ASKIF
CHOICE /M "Spring STS is running. Kill STS Process"
IF ERRORLEVEL 2 GOTO ENDIT
TASKKILL /F /IM "STS.exe"
TIMEOUT 3 /NOBREAK>NUL
:ASKIF
CHOICE /M "Open Spring STS"
IF ERRORLEVEL 2 GOTO ENDIT
START "" "C:\sts-bundle\sts-3.8.3.RELEASE\STS.exe"
:ENDIT
Echo=Exiting Batch
TIMEOUT 3 /NOBREAK>NUL
I need some help here.
I am currently trying to kill any process that isn't in a whitelist (command line) like so, however it is not working.:
#echo off
setlocal
set "whitelist=DcomLaunch RPCSS LocalServiceNetworkRestricted netsvcs LocalService LocalSystemNetworkRestricted NetworkService LocalServiceAndNoImpersonation taskhostex cmd dwm conhost services smss SearchIndexer Isass Explorer csrss conhost cftmon"
for /f "tokens=2 delims=," %%I in (
'wmic process get executablepath^,status /format:csv ^| find "\"'
) do (
set "proc=%%~I"
setlocal enabledelayedexpansion
set /p "=%%~I: "<NUL
wmic path win32_process get CommandLine | findstr /i "%whitelist%" >NUL && (
echo OK
) || (
echo UNACCEPTABLE!
taskkill /im "%%~nxI" /f
)
endlocal
)
wmic path win32_process get CommandLine | findstr /i "%whitelist%"
In above command, findstr would look for a match in entire wmic output so it will find a match always. For instance, at least cmd would match because wmic runs in a cmd window. Next commented code snippet should work however it gives different results if elevated (run as administrator).
set "whitelist=DcomLaunch RPCSS LocalServiceNetworkRestricted netsvcs LocalService LocalSystemNetworkRestricted NetworkService LocalServiceAndNoImpersonation taskhostex cmd dwm conhost services smss SearchIndexer Isass Explorer csrss conhost cftmon"
rem add windows VITAL processes !!! incomplete !!!
set "whitelist=svchost ctfmon lsass winlogon %whitelist%"
for /f "tokens=2,3 delims=," %%I in (
'wmic process get executablepath^,ProcessID^,status^,WindowsVersion /format:csv ^| find "\"'
) do (
set "proc=%%~I"
set "procID=%%~J"
setlocal enabledelayedexpansion
rem debugging: set /p "=%%~I: "<NUL
rem debug try: wmic path win32_process where "ProcessID=%%J" get Name 2>NUL | findstr /i "%whitelist%">NUL 2>&1 && (
rem debug try: wmic path win32_process get executablepath 2>NUL | findstr /i "!proc:/=//!">NUL 2>&1 && (
wmic path win32_process where "ProcessID=%%J" get CommandLine 2>NUL | findstr /i "%whitelist%">NUL 2>&1 && (
rem suppress "No Instance(s) Available" report in above line: 2>NUL
echo OK %%J "%%~I"
) || (
rem UNWANTED: here come inactive processes "cmd", "wmic", "find"
rem and maybe more ones that were active in FOR %%I execution time
rem (but loop continues); let's filter them here:
tasklist /FI "PID eq %%J" /NH | find "%%J" >NUL 2>&1 && (
echo NO %%J "%%~I"
rem taskkill /PID "%%~J" /f
) || (
echo XX %%J "%%~I"
rem inactive at the moment
)
)
endlocal
)
Essential Processes needed to run Windows (next list may be a bit out of date):
… here is a list of the essential processes that Windows needs to run
correctly.
System Idle Process
explorer.exe
taskmgr.exe
spoolsv.exe
lsass.exe
csrss.exe
smss.exe
winlogon.exe
svchost.exe – (There will be a few of these)
services.exe
By shutting down anything other than these processes, stand alone
Windows should operate fine, however if any of these processes are
shutdown, Windows will start to become unstable or unusable.
I'm trying to write a batch that checks how many instances of the process "example.exe" are running, and if there are two or more instances, leave it running. But if there is only one instance running, end the process. Here's what I have:
#echo off
wmic process where name="example.exe" | find "example" /c > %temp%\variable.txt
set /p value=<%temp%\variable.txt
if %value% lss 2 goto endprocess
if %value% gtr 1 goto continue
:endprocess
start taskkill /f /im example.exe
:continue
ECHO continue
#echo off
My issue is this: It always thinks value is lss 2 (it thinks there are less than 2 instances of the process running). However, in my task manager, I can see that there is obviously 2 instances running. I think it's an issue with defining the value maybe? I don't know, I'm quite new to this. Any help? Thanks!
UPDATE
Okay I've now changed it to this (suggested by Magoo)
#echo off
wmic process where name="example.exe" | find "example" /c > "%temp%\variable.txt"
set /p value=<"%temp%\variable.txt"
if %value% equ 1 goto endprocess
if %value% neq 1 goto continue
:endprocess
start taskkill /f /im example.exe
:continue
ECHO continue
#echo off
This still doesn't exactly work, but i changed the number of instances from 1 to 0 and it ended the process. In other words, 1 process was running, but this batch file thought that 0 were running. Any ideas now?
This uses tasklist in XP Pro and higher:
#echo off
tasklist /fi "imagename eq example.exe" /nh |find /i /c "example.exe" > "%temp%\variable.txt"
set /p value=<"%temp%\variable.txt"
if %value% equ 1 taskkill /f /im example.exe
ECHO continue
#echo off
You can do it with one line and no temp file also - this uses another findstr filter to check if the number is a single 1 on a line and then && is a conditional operator that will launch taskkill if it does find 1.
#echo off
tasklist /fi "imagename eq example.exe" /nh |find /i /c "example.exe"|findstr "^1$" >nul && taskkill /f /im example.exe
ECHO continue
#echo off
I'd suggest that you have a fault with your logic.
The code should go to endprocess if the number found is <2 - that is, 0 or 1. If the lss 2 test is failed, then the count must be 3+, so the gtr 1 test will always succeed.
I've no idea why you don't use simply
if %value% neq 1 goto continue
or even
if %value% equ 1 start taskkill /f /im example.exe
But probably you've not told us that you want to be able to detect other instance-counts - as well as concealing the name of the executable for which you are checking.
Now - it may have been really useful to show us the content of the file. Are you sure the file is actually being generated? What happens if you try using "%temp%\variable.txt" instead of %temp%\variable.txt - that is, "quote the filename" ?
Hi I'm trying to create a batch file to filter out servers which has RDP/ICA listener down from a list of servers in a notepad file, I created this script with the below syntax, but for some reasons it won't work as expected, can some one help me fix the situation?
I've a list of servers in computer.txt file and I'm trying to find the one's which are down and if errorlevel is 0, meaning the string down is found, I want the server name to be printed in listenerdown.txt , but for some reasons, if I execute the batch file, all the servers in computer.txt gets written to listenerdown.txt file
below is the batch file
for /f %%i in (computer.txt) do(
qwinsta /server:%%i | findstr/i down >nul 2>&1
if %errorlevel% neq 1
echo %%i >>Listenerdown.txt
)
Move the echo onto the same line as the if statement or else use parentheses to establish scope and %ErrorLevel% will always be 0 because the variable does not get set in a loop without delayed expansion.
setlocal EnableDelayedExpansion
for /f %%i in (computer.txt) do(
qwinsta /server:%%i | findstr /i down >nul 2>&1
if !errorlevel! neq 1 echo %%i>>Listenerdown.txt
)
endlocal
or
setlocal EnableDelayedExpansion
for /f %%i in (computer.txt) do(
qwinsta /server:%%i | findstr /i down >nul 2>&1
if !errorlevel! neq 1 (
echo %%i>>Listenerdown.txt
)
)
endlocal
Cmd.exe parses batch files line by line and unless you tell it that the scope of the command continues onto the next line it will think the command is finished.
You should use setlocal EnableDelayedExpansion, and !errorlevel! instead of %errorlevel%:
#echo off
setlocal EnableDelayedExpansion
for /f %%i in (computer.txt) do (
qwinsta /server:%%i | findstr/i down >nul 2>&1
if !errorlevel! neq 1 echo %%i >>Listenerdown.txt
)
otherwise the value of errorlevel would be expanded only once, before entering the loop, and would not have the correct value. You also have to make sure that echo is in the same row as the if.