dos batch script using psexec shows 'help' text every time it loops - batch-file

Currently working on a script to ping every host on a /24 subnet, and then executes another script which runs psexec on those machines which are online. The ping sweep script is called ping.bat and the other script which actually runs psexec on the machines is called deploy_mir.bat. I can simply run deploy_mir.bat on a remote host and it will run no problem.
The problem im having is that every time mir.bat runs, which itself contains a loop, it will display the help info for psexec in the cmd window. As far as i can tell everything is working fine, aside from the annoying fact that everytime the loop inside of mir.bat runs my cmd window gets filled with the help info for psexec. I dont have #echo enabled, not that it would cause this anyway.
hoping for a quick fix, but if my code is needed to get an answer ill post it.
Posting the code anyway...
#echo on
setlocal EnableDelayedExpansion
set /p ipAddress="enter ip address: "
for /l %%i in (1,1,255) do (
ping -n 1 %ipAddress%.%%i | find "TTL" > nul
if !errorlevel! == 0 (
call deploy_mir.bat %ipAddress%.%%i
)
)
endlocal
deploy_mir.bat code
#ECHO OFF
echo "Mir Agent deployment to: %1"
rem net use T: \\%1\C$ /user:administrator "password"
net use T: \\%1\C$ /user:administrator "username"
copy /y conf.xml T:\WINDOWS\
copy /y setup_mir.bat T:\WINDOWS\
net use t: /delete
rem psexec \\%1 -i -u administrator -p "password" c:\windows\setup_mir.bat
psexec \\%1 -i -u administrator -p "username" c:\windows\setup_mir.bat
Desired cmd line result of running deploy_mir.bat
C:\DOCUME~1\socuser2\MIR>deploy_mir.bat 10.180.145.66
"Mir Agent deployment to: 10.180.145.66"
The command completed successfully.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
t: was deleted successfully.
PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\setup_mir.bat exited on 10.180.145.66 with error code 0.
C:\DOCUME~1\socuser2\MIR>

Just a suggestion. Not sure if it will solve your problem, but may provide some guidance:
My first step would be a small test by explicitly calling psexec on some test batch file in place of the line call deploy_mir.bat %ipAddress%.%%i. If no help message appears, since deploy_mir.bat works find on its own, try explicitly placing it's content in place of the same line call deploy_mir.bat %ipAddress%.%%i. If that works, then there is some issue in the said line we've been replacing. I believe dos / batch will open a sub shell from this line of code and run it's code in that scope. That may be causing the problem. Just guessing with the information provided.
Code Specific Notes:
#echo is enabled, but you say it is not in your question.
!errorlevel! == 0 should be !errorlevel! EQU 0
Some General Notes:
In general, I used to pass parameters to batch scripts in quotes, then strip the quotes with %~1 once inside the batch script. Similarly for if conditions, as !someVar! == a will throw and error if someVar is not set / empty, while "!someVar!" == "a" will gracefully not meet the criteria of the if condition.

I don't know why it works when called from outside the loop. But the psexec line in deploy_mir.bat should have cmd /c.
psexec \\%1 -i -u administrator -p "username" cmd /c c:\windows\setup_mir.bat

Related

How to get the return code of a batch started with the PSEXEC command?

I use the PSEXEC command to start a batch file on a remote computer:
psexec \\remotemachine -s -d cmd.exe /c c:\test_dir\build_dummy.bat
The build_dummy.bat script:
#echo off
SETLOCAL EnableDelayedExpansion
>output_build_bummy.bat.log (
rem just print something into an output file
echo.
echo This is a dummy batch script
rem close the file output
)
EXIT /B -12345
I want that psexec returns the code -12345
However, I get just the process ID of the started cmd.exe.
How can I get the error code?
Error code of any command is stored in %errorlevel% variable. Simply type echo %errorlevel% and you will get it.
I discovered that if I omit the option -d in the psexec call then psexec returns exactly what I need - the exit code of my batch script
:)

Unable to launch multiple programs via a batch file

Am trying to create a batch file, that would launch multiple programs. But unfortunately, things don't seem to work.
Kindly, find below my requirement:
Open InfluxDB server
Launch Grafana application.
Commands used in the batch:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
Start.cmd
timeout 5
cd "C:\Users\C51539A\Downloads\grafana-5.1.3\bin"
grafana-server.exe
The above script, launches InfluxDB. But doesn't moves further.
Could you please suggest me, on how to proceed?
You need to use the call keyword to have control returned to the caller after invoking another batch script:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
call start.cmd
...
Should start.cmd run InfluxDB synchronously (i.e. not in the background) you need to launch it in a separate window:
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
start "InfluxDB" cmd /c start.cmd
...
#echo off
cd "C:\Users\C51539A\Downloads\influxdb-1.5.2-1"
start InfluxDB
ping -n 6 127.0.0.1 > nul
cd "C:\Users\C51539A\Downloads\grafana-5.1.3\bin"
start grafana-server
Edit the "start InfluxDB" and "start grafana-server" to be the correct exe names, without .exe

Get a VBScript to run properly while my computer is locked

I put a pause into the code to break it down and make sure it is doing what it is supposed to do. Its not.
The Check1.msl will spit out a file named check.rpt. When I run it manually and the batch file pauses - check.rpt is there. I schedule the task and lock my computer. When I log back in, the msl program is open and the cmd prompt is sitting at pause but there is NO check.rpt file.
Here is what I have at the beginning:
#ECHO OFF
PUSHD "%~dp0"
ECHO CD is now %CD%
PING 1.1.1.1 -w 2000 -n 1
echo starting the check1 msl file
DIR Check1.msl
Start Check1.msl
PING 1.1.1.1 -w 2000 -n 1
echo starting the password injector file
WScript //B passwordinjector.vbs
pause
POPD
exit
Here is the VBScript called passwordinjector.vbs
set wshshell = wscript.CreateObject("wscript.shell")
wshshell.AppActivate "User Login"
WshShell.SendKeys "Username"
WshShell.SendKeys "{tab}Password"
WshShell.SendKeys "{tab}{enter}"
I've tried cscript passwordinjector.vbs to no avail.
EDIT:
Researching (and common sense) I figured out that you can't .SendKeys while the computer is locked. I need an alternative way to pass the log in information to the program. How do I pass a username and password to a prompt from a program?
How you can pass login information to a program depends on how that particular program expects login information. Unless you're able to present credentials in a commandline or something similar (like a HTTP POST request for instance) this can't be done with VBScript I'm afraid.
You may want to consider switching to something that is more suitable for GUI automation, like AutoIt.

batch script if user press Ctrl+C do a command before exiting

I wrote a script which is doing net use at the beginning and net use /DELETE at the end.
But if user decides to press Ctrl + C and exits the script, I need to do a net use /DELETE.
Is that possible? I can't find anything on google.
Sure, simply execute most of your script in a new CMD session:
#echo off
if "%~1" neq "_start_" (
net use ...
cmd /c "%~f0" _start_ %*
net use /delete ...
exit /b
)
shift /1
REM rest of script goes here
As long as your console window remains open, the net use /delete command will always fire after the inner cmd session closes. It could close because of normal run, user presses Ctrl-C, or fatal error - the final net use \delete will still fire.
My idea is similar to dbenham's. Took me forever to figure out how to minimize the current console window though. I banged my head against the wall trying to get the cmd window not to ignore an Alt+Space keypress using Wscript.Shell's .SendKeys method. Finally I turned to PowerShell to handle minimizing and restoring the working window.
The advantage to this over dbenham's is that you'll inevitably have some rectal-cranially inverted user who gets bored with the running of your script and terminates it with the red X. dbenham's won't catch that, but mine should.
#echo off
setlocal
if "%~1" neq "wrapped" (
rem :: Map network drive
net use y: \\computername\c$ >NUL
rem :: minimize this console
powershell -windowstyle minimized -command ""
rem :: relaunch self with "wrapped" argument and wait for completion
start /wait "" cmd /c %~f0 wrapped
rem :: After script completes or user interrupts, remove drive mapping and restore window
net use y: /delete >NUL
powershell -windowstyle normal -command ""
goto :EOF
)
:: Main script goes here.
:loop
cls
echo Simulating script execution...
ping -n 2 0.0.0.0 >NUL
goto loop
There is a very simple solution. Just write:
ping -l www."site".com -t 65500>nul
before your net use /delete command. The only way to break that command is to press ctrl+c.
By example:
net use "arguments"
ping -l www.google.com -t 65500>nul
net use /delete
This is a good way to detect ctrl+c, but beware of the site address you write, because it risks to make that site crash. You should, by consequence, write the address of an unusual site, or of a site of your own(Attention: the site must be existing), like a blog or something like that.
I don't think this is possible. At the beginning of your script, you can use:
net use /delete 2>nul
net use g: \\server\sharename /persistent:no
Or you could try pushd instead of net use...
If Command Extensions are enabled the PUSHD command accepts
network paths in addition to the normal drive letter and path.
If a network path is specified, PUSHD will create a temporary
drive letter that points to that specified network resource and
then change the current drive and directory, using the newly
defined drive letter. Temporary drive letters are allocated from
Z: on down, using the first unused drive letter found.
This way, you will always have mapped drive correctly set.

How to ping a server only once from within a batch file?

I want to learn how to write batch scripts and tried to create a script which automatically runs this command in the command line once:
ping www.google.de -t
and displays the ping, so it would look like this:
Reply from XXX.XXX.X.XX: time=30ms
Reply from XXX.XXX.X.XX: time=31ms
Reply from XXX.XXX.X.XX: time=29ms
My problem is, that this will result in this when I execute this command as script:
My problem is that it will not execute the ping command at all, but just insert the command unlimited times in the console window as its shown in the screenshot.
I just created a new file, wrote ping www.google.de -t in it, saved it as ping.bat file and executed it with double clicking on it.
So how to write the batch file to start this command only once and display the ping result?
I am sure you must have named the resultant bat file as "ping.bat". If you rename your file to something else say pingXXX.bat. It will definitely work. Try it out.
my batch file contains below code only
ping 172.31.29.1 -t
with file name as ping.bat
with file name abc.bat
Enter in a command prompt window ping /? and read the short help output after pressing RETURN. Or take a look on:
ping - latest Microsoft documentation for this Windows command
ping - Windows XP documentation for this Windows command
Explanation for option -t given by Microsoft:
Specifies ping continue sending echo Request messages to the destination until interrupted. To interrupt and display statistics, press CTRL+ENTER. To interrupt and quit this command, press CTRL+C.
You may want to use:
#%SystemRoot%\system32\ping.exe -n 1 www.google.de
Or to check first if a server is available:
#echo off
set MyServer=Server.MyDomain.de
%SystemRoot%\system32\ping.exe -n 1 %MyServer% >nul
if errorlevel 1 goto NoServer
echo %MyServer% is available.
rem Insert commands here, for example one or more net use to connect network drives.
goto :EOF
:NoServer
echo %MyServer% is not available yet.
pause
goto :EOF
For bash (OSX) ping google.com -c 1 (incase search brought you here)
if you want to use the name "ping.bat", a small trick is to use this code:
#echo off
cd\
ping google.com -t
Just add that "cd\" and you are fine... ;)
Not sure exactly what you are trying but your posted code should work just fine. in case you don't want the command to be displayed, add #echo off at starting of your script. If i have the below code in a file named as test.bat and run it command prompt as test.bat it will work just fine.
#echo off
ping www.google.de -t
To address your EDIT: where the main concern is ping command was not recognizable. ping command generally will be located under C:\Windows\System32\ where C:\ being the root directory. In case, the root directory is different you can get the root directory using %SystemRoot% environment variable and can say like
%SystemRoot%\Windows\System32\PING.EXE www.google.de -t
Another way to see if the command you are trying to run is recognizable or not is using WHERE command like below
where ping
If the command is recognizable; it will output the path like
C:\Windows\System32\PING.EXE
Else will result in error
I know why, you are using the file name "ping" and you are using the code "ping", it just keeps trying to run itself because its selected directory in where that file is, if you want it to actually ping, put this before the ping command: "cd C:\Windows\system32", the actual file that pings the server is in there!
From Batch file, ping a ip only once using the following command:
Ping 192.168.199.10 -n 1
i used Mofi sample, and change some parameters, no you can do -t
#%SystemRoot%\system32\ping.exe -n -1 4.2.2.4
The only thing you need to think about in this case is, in which directory you are on your computer.
Your command line window shows C:\users\rei0d\desktop\ as your current directory.
So the only thing you really need to do is:
Remove the desktop by "going up" with the command cd ...
So the complete command would be:
cd ..
ping XXX.XXX.XXX.XXX -t
Having 2 scripts called test.bat and ping.bat in same folder:
Script test.bat contains one line:
ping google.com
Script ping.bat contains below lines:
#echo off
echo Hello!
pause
Executing "test.bat" the result on CMD will be:
Hello!
Press any key to continue . . .
Why? Because "test.bat" is calling the "ping.bat" ("ping google.com" is interpreted as calling the "ping.bat" script).
Same is happening if script "ping.bat" contains "ping google.com". The script will execute himself in a loop.
Easy ways to avoid this:
Do not name your script "ping.bat".
You can name the script as "ping.bat" but inside the script use "ping.exe google.com" instead of "ping google.com".
Create a text file with text "#%SystemRoot%\system32\ping.exe -t www.google.com" and save it with extension ".bat".
Just click and run it and you will get the result.
So basically what happens is that we run ping.exe application with parameters '-t' and 'www.google.com' (web-address).
The answer to your question is this
Ping -n 1 0.0.0.0
But if you want it to be faster than this, this will be your answer
Ping -n 1 -l 1 0.0.0.0
Note: Replace 0.0.0.0 with your desired IP address
Just
write the command "ping your server IP" without the double quote. save file name as filename.bat and then run the batch file as administrator

Resources