command line parameter while - batch-file

I want to run command line with patameters to make it wait for connection then do "gpupdate /force" and then nofity user the job is done. The thing I am stuck at is making it wait for connection.
cmd /c echo Connect a network cable. & *something* & echo n | gpupdate /force & msg * Done.
If something was following code then it would work:
:top
ping -n 1 site.com >nul
if errorlevel 1 (
goto top
)
but I don't know how would I write it on 1 line as a parameter.
How would I make it work the way I wish? Alternative solutions are welcome as well!

I don't understand your aversion to a batch script. But the following one liner should work.
#echo Connect a network cable.&cmd /q /c "for /l %N in () do ping -n 1 site.com >nul&&exit"&echo echo n^|gpupdate /force&msg * Done.
The key bit is cmd /q /c "for /l %N in () do ping -n 1 site.com >nul&&exit". A new CMD.EXE process is created that enters an infinite FOR loop. It continuously pings the site until it is successful. The EXIT command only executes when PING was successful because of the && operator. Once EXIT executes, control is returned to the parent CMD.EXE process.

Why not make it all a batch file?
#echo off
echo Connect a network cable.
:top
ping -n 1 site.com >nul || goto :top
echo Connected.
gpupdate /force
msg * Done.

Related

how to capture error conditions in windows ftp scripts?

I am using windows batch scripts to run ftp scripts automatically. Where the set of ftp commands I want run is saved in a file.
example:
#echo off
ftp -n -i -s:c:\temp\myftpscriptfile.ftp
I have used the %ERRORLEVEL% syntax to successfully capture error conditions in the batch commands. My challenge is the ftp script command is always returning an ERRORLEVEL of 0 even when the commands inside the script fail.
I am having difficulty figuring out how to have the ftp script actually return or trap when errors occur inside it. It will simply run through the commands blindly and even though i can see the errors echoed on screen I can't capture them as an ERRORLEVEL..
Sample screen shot of trying script which fails to login and then echoing the ERRORLEVEL which shows a zero..
ftp> open fubar.test.com
Unknown host fubar.test.com
ftp> user test test
Not connected.
ftp> ascii
Not connected.
ftp> cd /home/test/dirname
Not connected.
ftp> mput C:\Test\test*.txt
Not connected.
ftp> close
Not connected.
ftp> quit
.0
Use find:
ftp -n -i -s:c:\temp\myftpscriptfile.ftp 2>&1|find "Unknown host">nul
if %errorlevel%==0 (
echo Error!
)
Parse ftp communication using for /F command. Possible approach in next script. Caveat: there supposedly exist some ftp error message(s) not included in given test sequence. Of course, you can test positive ftp messages rather...
#ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
set "errsftp=0"
ftp -n -i -s:c:\temp\myftpscriptfile.ftp >c:\temp\31442020.err 2>&1
for /F "tokens=1*" %%G in (c:\temp\31442020.err) do (
rem echo %%G [%%H]
if "%%G"=="ftp>" (
set "line=%%H"
set "errs=0"
) else (
set "reply=%%G %%H"
Call :testreply Unknown host
Call :testreply Connection timed out
Call :testreply Not connected
rem insert next tests here
if !errs! EQU 0 echo !line! =looks OK= !reply!
)
)
echo(
echo :errors total: %errsftp%
ENDLOCAL
goto :eof
:testreply
set "ylper=!reply:%*=!"
if not "!ylper!"=="!reply!" (
echo !line! =ERROR= !reply!
set /A "errs+=1"
set /A "errsftp+=1"
)
goto :eof

My Batch file wont continue while windows is locked

I have a batch file which calls other programs to open and run.
I want this batch file to run overnight while my computer is locked. I use Windows Task Scheduler to run this program overnight. The batch file runs fine except when it gets to start Check1.msl. Then windows can't seem to find that file. When I unlock my computer and manually execute the batch file, it has no problem finding and running those files.
Here is my code:
#echo off
Start Check1.msl
PING 1.1.1.1 -w 1000 -n 1
start passwordinjector.vbs
IF EXIST C:\Users\Username\Desktop\New folder\check.rpt (
taskkill /IM MAINRDW.exe >nul
PING 1.1.1.1 -w 500 -n 1
Start Trial.msl
PING 1.1.1.1 -w 1000 -n 1
start passwordinjector.vbs
Del C:\Users\Username\Desktop\New folder\check.rpt
) ELSE (
PING 1.1.1.1 -w 500 -n 1
taskkill /IM MAINRDW.exe >nul
)
EDIT:
I've been trying it out for awhile now. To test it, I simply use Windows Task Scheduler to run it a minute out then lock my computer and wait for the Scheduler to kick in.
Here is what I have so far:
#echo off
cd /d "%~dp0"
PING 1.1.1.1 -w 2000 -n 1
echo starting the check1 msl file
Start Check1.msl
PING 1.1.1.1 -w 2000 -n 1
echo starting the password injector file
WScript //B passwordinjector.vbs
IF EXIST C:\Users\USERNAME\Desktop\New folder\check.rpt (
echo check if the check exists
taskkill /IM MAINRDW.exe /f >nul 2>&1
PING 1.1.1.1 -w 2000 -n 1
echo It does exist, so run the next Trial msl
Start Trial.msl
PING 1.1.1.1 -w 3000 -n 1
WScript //B passwordinjector.vbs
Del C:\Users\USERNAME\Desktop\New folder\check.rpt
) ELSE (
PING 1.1.1.1 -w 1000 -n 1
it doesnt exits
taskkill /IM MAINRDW.exe /f >nul 2>&1
)
exit
It is very hard to troubleshoot batch files. If anyone has any suggestions, I would appreciate it. Through trial and error, I figured out (still not sure) that it wasn't executing the .vbs script. So I looked it up online from Microsoft's developer network and got the syntax WScript //B.
EDIT2:
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
I've tried cscript passwordinjector.vbs to no avail.
The .bat script can make the current working directory to be the one in which it is located by:
PUSHD "%~dp0"
Remember to POPD before exiting.
#ECHO OFF
PUSHD "%~dp0"
ECHO CD is now %CD%
DIR Check1.msl
START Check1.msl
...
It seems like the issue lies in the VBScript. I use .sendkey to inject the password. This is retricted while the computer is locked; so I'm out of luck.

Bat file stopping and i'm not sure why

So, I have a bat file I've been building to do a simple profile backup/restore. I'm running Windows 7 64bit.
I get through 99% of the script until it gets to this point and then dies. To be clear, in the environment I'm working in, I HAVE to turn off UAC for certain things. Someone much higher up than me made that call.
However, this needs to do this but it dies after the "[ COMPLETE ]" is echoed. It doesn't close the window, it just goes back to the C prompt.
The only other thing it's going (trying) to do after that is log the user off after a 15 second timer.
Any help would be much appreciated.
echo [ Turning off UAC... ]
C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
ping -n 2 -w 1000 127.0.0.1 > nul
echo [ COMPLETE ]
ping -n 2 -w 1000 127.0.0.1 > nul
echo.
ECHO ==========================================================
ECHO = Restore Complete =
ECHO = Computer will logoff in 15 seconds to apply changes. =
ECHO ==========================================================
TIMEOUT 16
shutdown /l
#pause
:EOF
I made a test batch file that does what you're trying:
#echo off
echo before
cmd /k echo I'm doing it!
echo after
And here was the result:
C:\files\j>test
before
I'm doing it!
So I changed it to call:
#echo off
echo before
call echo I'm doing it!
echo after
And got this:
C:\files\j>test
before
I'm doing it!
after
So clearly, /k isn't for you. But CALL will put it in a new shell... So my recommendation is to just change that line to be :
%windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
No cmd, no \k, no anything - just run the REG.exe.
If you have to, use the CALL since you're adding a registry key and it should stay there.

Batch script to ping other PC in my lan and shutdown together last online PC

I have a LAN with 4 PCs and one Synology server.
I wish that every time a computer is turned off, it runs a batch (from windows pc) and see if other PCs are on, - if any is on do nothing, - otherwise runs a command that shuts down the server.
Here is what I have at the moment:
#echo off
PING 192.168.1.10
IF %ERRORLEVEL% EQU 1 plink root#192.168.1.10 -pw MYPASSWORD poweroff
But I would like to do something like:
http://i.imgur.com/BLAVCBt.png
THANK YOU!!
No need to do it from client-side.
I would do it on server-side: check if at least one client is up, if not then shutdown (basically a one-liner):
(ping -4 Client1 & ping -4 Client2 & ping -4 Client3 & ping -4 Client4) |find "TTL=" >nul || shutdown -s -t 60 -f -c "Shutdown because all clients are down"
Create a scheduled task on the server that runs this script - let's say all 5 minutes. The timeout gives you enough time to abort the shutdown (shutdown -a) in case you are working on the server.
first you should schedule a shutdown batch.. follow these steps to run the batch when you shut down..
https://technet.microsoft.com/en-us/magazine/dd630947.aspx
The batch looks fine.. but if you want to do nothing in the first case and shutdown in the second case use it like this:
#echo off
PING 192.168.1.10
IF %ERRORLEVEL% EQU 1 plink root#192.168.1.10 -pw MYPASSWORD shutdown -s -t 00
So the statement "do nothing" is deleted and the poweroff command is shutdown -s -t 00
for %%a in (pc-1 pc-2 pc-3 pc-4) do if /i "%computername%" neq "%%a" ping %%a&if not errorlevel 1 goto :eof
echo shut-down-the-server-code
That way, you ping until you find a PC that's on, skipping your own (which is obviously on). If any are found on, just terminate the batch by jumping to end-of-file. If all are off (other than yours, obviously) then shut down the server and shut down your PC.
http://ss64*org/viewtopic.php?pid=8188
http://stackoverflow*com/questions/28304099/batch-script-to-ping-other-pc-in-my-lan-and-shutdown-together-last-online-pc
http://wwwmsfnorg/board/topic/173438-shutdown-last-running-pc-on-the-lan-with-server/
http://forum_synology*com/enu/viewtopic.php?f=145&t=96609
http://www_dostips*com/forum/viewtopic.php?f=3&t=6245
I think last one will be more easy to use thank you for the answer!
#echo off
set "flag="
ping 192.168.1.1 -n 2 |find /i "TTL=" >nul && set flag=1
ping 192.168.1.2 -n 2 |find /i "TTL=" >nul && set flag=1
ping 192.168.1.3 -n 2 |find /i "TTL=" >nul && set flag=1
ping 192.168.1.4 -n 2 |find /i "TTL=" >nul && set flag=1
if not defined flag echo shutdown server

Why is this line printing in CMD when I have told it to cls?

So I want it to not show that it is pinging, just whether or not it is running.
My code that is not doing what I want it to is:
#echo off
:start
ping 188.138.32.53 -n 1
if %errorlevel% == 1 (
goto :fail
) else (
cls
echo The MTG SAMP Server is up and running!
pause
goto :troubleshooting
)
:fail
cls
echo The MTG SAMP Server is currently down, please be patient...
goto :start
It clears the screen and only says when it is running, but when it is not running, it displays the ping status. Why is this?
As you are in a loop.
The ping needs some time to detect that the destination isn't reachable.
So you see this on the screen.
After the first test, the cls is executed and your text is displayed.
But as you startet the test again, you get also the ping output.
Simply redirect the output to nul
ping 188.138.32.53 -n 1 > nul 2>nul

Resources