Bat file stopping and i'm not sure why - batch-file

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.

Related

How do I remove the last line on the CMD Window when running a batch file?

So I was making a batch file that allows to make other people think you can hack, and in one part it says in the CMD window
Starting hack in 10...
Starting hack in 9...
etc.
What I want to do is to instead of appearing on different lines I want the lines to change by deleting the last line that's showing on the CMD window. here's how the code for that part of the program looks like right now
echo Starting hack in 10...
timeout /t 1
echo Starting hack in 9...
timeout /t 1
echo Starting hack in 8...
timeout /t 1
echo Starting hack in 7...
timeout /t 1
echo Starting hack in 6...
timeout /t 1
echo Starting hack in 5...
timeout /t 1
echo Starting hack in 4...
timeout /t 1
echo Starting hack in 3...
timeout /t 1
echo Starting hack in 2...
timeout /t 1
echo Starting hack in 1...
timeout /t 1
tree
echo Hacking completed!
pause
I was also searching for a way to do it on stack overflow but I didn't find anything helpful so I created this account JUST TO MAKE THIS QUESTION. Please I need answers.
Edit: This question was answered, here's the link to the program with the problem fixed (if it says that it contains a virus, don't worry it's just a false positive (It's an exe file because I converted the file with BAT2EXE)): https://drive.google.com/file/d/1tg73Aa-Yi3So6OJYvYUPDdL0eAaqG9PL/view?usp=sharing
To overwrite the last line, all you have to do is a carriage return instead of a linefeed (carriage return plus line feed).
I'm using a subroutine here, params are a message string (quoted because of spaces) and a number (counter).
#echo off
setlocal EnableDelayedExpansion
REM create a CariageReturn:
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
echo Welcome to %~n0
echo/
call :spinner "Starting in" 10
echo It has now started.
echo insert payload here
goto :eof
:spinner
set counter=%2
:spinnerloop
<nul set /p ".=%~1 %counter% !CR!"
set /a counter-=1
timeout 1 >nul
if %counter% geq 0 goto :spinnerloop
Make sure, the first echo after the call is long enough to overwrite the complete message plus counter (add enough spaces if necessary) or echo a new line (echo/) if you want to keep the last message on screen.
Use Cls
echo Starting hack in 10...
timeout /t 1
Cls

Run youtube-dl periodically

Say I have some problems with my connection to youtube, and keep getting WinError 10060 when downloading a full channel from time to time - and most importantly got desperate from trying to fix that (The problem is not with a specific video, as when I start over it downloads normally).
My aim is to bypass this by creating a batch file with an infinite loop which will run youtube-dl again with the same parameters after this error occurs.
I've been searching for a solution and my current loop bat contains this:
:LOOP
timeout /T 1 /NOBREAK
call MainDownload.bat
echo "Stop Recorded">> log.txt
if not ErrorLevel 1 goto :LOOP
After trying this I can see a "stop recorded" on my log when it happens, but the MainDownload.bat won't run again.
Can anyone offer a solution which will cause this to run infinitely until I manually close it?
Thanks in advance.
If you run youtube-dl.exe without parameters you'll get an error.
if you check the errorlevel after this you will get 2. So youtube-dl set an errorlevel when an error occur !
#echo off
youtube-dl.exe 2>nul
echo %errorlevel%
Run youtube-dldirectly in the bat to get the good errorlevel.
Something like this :
#echo off
:LOOP
timeout /T 1 /NOBREAK
youtube-dl -v -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" --yes-playlist --download-archive DownloadedVideosArchive -o "%%(uploader)s/%%(playlist)s/%%(playlist_index)s - %%(title)s [%%(upload_date)s].%%(ext)s" --batch-file Playlists.txt
if %errorlevel% GTR 0 (
echo [%time%] "Stop Recorded" >> log.txt
goto :LOOP
)

Batch Script - First "IF" selection does not work, rest do

I am having trouble with my code. If I run the file and follow the prompt, enter a workstation name then select site 1, it continues to open up the cmd window to execute the psexec command, it does not do the "IF NOT EXIST" for site 1, but for other sites it comes back fine. If no file exists it will output to the prompt, if it does it continues to psexec. Any idea what I am doing wrong? Thank you.
:MAN
SET /P S=Please enter a workstation name or IP:
ECHO.
ECHO 1 - Site 1
ECHO 2 - Site 2
ECHO 3 - Site 3
ECHO 4 - Site 4
SET /P D=Select which Site you want to deploy from:
IF %D%==1 SET D="\\site1\Operations\Sofware\Packages\file.msi"
IF %D%==2 SET D="\\site2\Operations\Sofware\Packages\file.msi"
IF %D%==3 SET D="\\site3\Operations\Sofware\Packages\file.msi"
IF %D%==4 SET D="\\site4\Operations\Sofware\Packages\file.msi"
IF NOT EXIST %D% GOTO MSG
START CMD /K PsExec.exe #%S% -s -h cmd /c msiexec.exe /i "%D%" /qn
PAUSE
GOTO EOF
:MSG
CALL :color 1a "ERROR: MSI PACKAGE DOES NOT EXIST"
Note the possibility of doing:
goto label%D%
You can also almost do:
set D=\\10.%D%.2.1\Operations\Sofware\Packages\quest.msi
instead of the 4 IF-statements.
Here is my working code, there was an issue with PsExec as well not taking the %s% variable, I instead outputted the user input to a text file and had Psexec read from that file. I also edited with everyone's suggestions. Thank you all.
:MAN
#ECHO OFF
SET /P S=Please enter a workstation name or IP:
ECHO %S% >> man.txt
ECHO.
ECHO 1 - Site 1
ECHO 2 - Site 2
ECHO 3 - Site 3
ECHO 4 - Site 4
SET /P D=Select which Branch you want to deploy from:
IF %D%==1 SET "R=\\site1\Operations\Sofware\Packages\file.msi"
IF %D%==2 SET "R=\\site2\Operations\Sofware\Packages\file.msi"
IF %D%==3 SET "R=\\site3\Operations\Sofware\Packages\file.msi"
IF %D%==4 SET "R=\\site4\Operations\Sofware\Packages\file.msi"
IF NOT EXIST %R% GOTO MSG
START CMD /k "PsExec.exe #man.txt -s -h cmd /c msiexec.exe /i %R% /qn & DEL man.txt"
PAUSE
GOTO MENU
:MSG
CALL :color 1a "ERROR: MSI PACKAGE DOES NOT EXIST"
== is not advised in a .bat, .cmd file; I'd use EQU

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.

command line parameter while

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.

Resources