Run Batch Command Without Checking For Confirmation - batch-file

I have a batch file that restarts several computers in my center. The batch file works fine but I would like if it ran faster. The reason it is slow is when it runs into a computer that has been turned off, it continues to try to find that computer for about 15 seconds before it moves to the next one.
Can I lower the time a batch command looks for the computer or just have it send the command and move on to the next?
I have pasted my current batch command below:
shutdown /f /r /m \\VAMAR-3STHWV1 /t 000

With the ping command I believe you can do it. By telling the ping command to only send one echo request and then &&ing the result with the shutdown command, something like:
ping VAMAR-3STHWV1 -n 1 >nul && shutdown /f /r /m \\VAMAR-3STHWV1 /t 000
That way the shutdown command gets executed only when the ping successfully reached out VAMAR-3STHWV1 in one echo request.

Related

Can't get batch to logout when it's done

I have a .bat that I've wrote to automate an install of a piece of software I have to install all the time at work. (I can't use powershell because a lot of the machines are XP still)
Near the end, the script is supposed to clean itself up and and logout, but I can't get it to log out for some reason.
taskkill /f /im explorer.exe>nul 2>&1
set _sd=%~dp0
cd /d c:\
del "%_sd%md5sum.exe"
...
del "%_sd%RUN-AS-ADMIN.bat"
wait 2
shutdown -f -l
There is no wait command in CMD. There is timeout for Win7+ but not for XP. To implement a kind of a timeout you'll have to use for example ping 127.0.0.1 -n 6 > nul. This will make your system "wait" for 5 seconds. You can modify the timeout by replacing 6 with any other value. If you want a timeout of X seconds, replace 6 with X+1.
EDIT: Made a stupid mistake making this, fixed it up.
Try this instead:
taskkill /f /im explorer.exe>nul 2>&1
set _sd=%~dp0
cd /d c:\
del "%_sd%md5sum.exe"
...
del "%_sd%RUN-AS-ADMIN.bat"
pause
shutdown -f -l
All I did was make it so that the wait is replaced with pause
I did some looking around, and I couldn't find any native commands that make the batch file delay and automatically continue, so this is the closest I can find. (Assuming the computer you're using this on is a Windows XP)
If you don't use a Windows XP for the batch file, use this instead:
taskkill /f /im explorer.exe>nul 2>&1
set _sd=%~dp0
cd /d c:\
del "%_sd%md5sum.exe"
...
del "%_sd%RUN-AS-ADMIN.bat"
timeout /t 2 >nul
shutdown -f -l
The timeout acts as a delay, however, it is unfortunately not a native command on Windows XP, but it is on Windows 7+ (Unsure about vista)
Hope this helps :)

How to kill a service in a batch file every 20 seconds?

A task called "FireSvc.exe" (McAffee service) keeps interfering with our app upgrades. I put
> taskkill /f /im "FireSvc.exe"
in a batch file. We continuously run this during installs so the software will successfully upgrade. I'm not positive why this service starts back so often. We have to run from the command line, because in the task manager you get "access denied" when trying to kill this task.
My question is, how would you make this run every 20-30 seconds?
We cannot install any type of non-approved software either. So, theres that...
Thanks for any input.
Here's what we use:
:runagain
taskkill /f /im "FireSvc.exe"
timeout /T 5
goto runagain
You can use vbscript's sleep command in a batch file with cscript like so...
First create a vbscript file (.vbs extension) that contains the following code (don't forget to save it with ANSI encoding otherwise it won't work):
Wscript.sleep 2500
Wscript.exit
Create a batch file in the same directory with this code:
#echo off
:Kill
cscript //NoLogo file.vbs
taskkill /f /im "FireSvc.exe"
GoTo Kill
I currently don't have access to a PC to check if it works so let me know what happens. I still think there might be a cleverer alternative... cheers!
Edit: Btw you can also simulate a sleep command with the ping command like so:
ping localhost -n 1 -w 2500 >nul
And if you are using windows vista or above you can use the timeout command.

Batch Script Logging and it runs double?

I wrote a batch script to make a backup of my Thunderbird client on Windows 7. It works perfectly, but I am having issues with the logging part.
Essentially, I would like it to see what is going on in the command window when it runs, as well as to log all output to a .log file.
The problem: It logs to a file, but runs without anything in the command window. Then after it completes, it runs AGAIN, but this time displays what's happening in the command window.
Included: The script. The log file.
Script:
#ECHO ON
rem
call :Logit>>%Desktop%\"%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4%-ThunderbirdBackup".log
:Logit
echo Backup Start at = %date% %time%
echo Beginning Automatic Backup and Encryption for Thunderbird
echo This will take approximately 45 seconds to complete
echo Killing Thunderbird.exe
taskkill /F /IM thunderbird.exe
echo Give the computer a moment to complete task
timeout /T 3
echo Zipping to Desktop
"C:\Program Files\7-Zip\7z.exe" a -t7z %Desktop%\"%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4%-ThunderbirdBackup".7z %AppData%\Thunderbird\Profiles\ -m0=lzma2 -mx3 -mmt=8 -mhe=on
echo Backup Complete at = %date% %time%
Thank you for any assistance you cold lend.
The log:
ThunderbirdScript-Log-Pastebin
You are invoking the :logit "function" twice; first by call and second by running through.
Just add a goto :eof after the call and you're done.
Then , to both logging to a file and displaying in stdout, you will need to tee http://en.wikipedia.org/wiki/Tee_(command) the output of the call.
call :logit | tee ThunderbirdBackup.log
goto :eof
:logit
...

Timeout prompt won't stop showing

I'm attempting to use Batch for the first time, and I'm running into some trouble with the timeout command. I'm making a simple backup program to backup certain files to my flash drive, and this is the beginning.I'm trying to make it so that the prompt does not show how much of the countdown is left. This is what I have:
ECHO Deleting current backup location...
RD /s /q F:\CurrentBackup
#TIMEOUT /t 10
ECHO Setting up new backup...
MKDIR F:\CurrentBackup
MKDIR F:\CurrentBackup\Documents
MKDIR F:\CurrentBackup\Pictures
MKDIR F:\CurrentBackup\Desktop
MKDIR F:\CurrentBackup\Music
rem xcopy C:\Eric D:\
Can anyone help me with this seemingly simple problem?
you can tell a command, where to write it's output. If you don't, it writes it to screen
TIMEOUT /t 10 >nul
will write the output to a "Null-Device" (also known as "Nirwana")
by the way: # does not suppress the output of a command, but suppress the repetition of the commandline. It's a kind of "one-line-echo off"
Normally, you put
#echo off
as the first line of a script.
echo off will turn command repetition off, and the # does the same thing for this very line (as the echo off is not yet active for this line)

How to break an infinite condition by setting a timeout in batch file?

I'm executing programs in a batch file. I want to break if the batch script runs more than some time(say 10 sec). Because it might become an infinite loop.
cls
::set timeout here
program.exe
::after timeout continue from here
Is there any suggestion to do this in a windows batch file? Plus, is it possible to define the line of endless loop in a batch script?
When you run a program from a batch file, you either run it and wait for it to terminate before executing the next command in the batch file, or you launch the program and executing immediately continues in the batch file. There is no way to measure how long a program runs. However, you can use certain techniques to see if a program is running and terminate it. You can also insert delays. So you can do what you are asking (more or less):
cls
start program.exe
ping -n 10 -w 1000 127.0.0.1 > nul 2>&1
tasklist | find /i "program.exe"
if %errorlevel%==0 taskkill /im program.exe
This starts the program and immediately starts to execute the next line of code, the ping. The ping causes a 10 second delay. The tasklist command pipes the list to find, which looks for the program. If it is found in the list, then it is still running and errorlevel is set to 0, which means the next line terminates the program.
This isn't fool proof however, because if program.exe is a common program name, then tasklist and taskkill will terminate all processes with the same program name (not just the instance you started).
How about that?
start calc.exe
ping -n 10 localhost >nul
taskkill /F /IM calc.exe

Resources