The problem is with the script that if the process is running as admin the same as the batch script it wont work. It just doesn't find the process. Any help?
http://pastebin.com/qUrhxYrT
#echo off
title Unturned Server Restarter
echo Server Batch by :: Mage ::
:loop
echo (%time%) Loot Resetting...
start Unturned.exe -nographics -pei -normal -nosync -pvp -port:25444 -players:12 -sv -batchmode
for /f "tokens=2" %%x in ('tasklist ^| findstr Unturned.exe') do set PIDTOKILL=%%x
echo (%time%) Loot Reset.
echo (%time%) Server is online.
echo (%time%) Server Loot Resetting in 2 Hours.
timeout /t 10 >null
echo (%time%) Server Loot now resetting . . .
taskkill /F /PID %PIDTOKILL%
goto loop
When you launch a batch file with elevated permissions then the working directory is set to the system32 folder.
Presumably your script cannot find the Unturned.exe file, so using a cd command to set the working directory before the loop is one way to resolve an issue like that.
See your code below with full paths to all executables.
#echo off
title Unturned Server Restarter
echo Server Batch by :: Mage ::
:loop
echo (%time%) Loot Resetting...
start "Unturned" "%ProgramFiles%\team\steamapps\common\Unturned\Unturned.exe" -nographics -pei -normal -nosync -pvp -port:25444 -players:12 -sv -batchmode
for /f "tokens=2" %%x in ('%SystemRoot%\System32\tasklist.exe ^| %SystemRoot%\System32\findstr.exe Unturned.exe') do set PIDTOKILL=%%x
echo (%time%) Loot Reset.
echo (%time%) Server is online.
echo (%time%) Server Loot Resetting in 2 Hours.
timeout /t 10 >null
echo (%time%) Server Loot now resetting . . .
%SystemRoot%\System32\taskkill.exe /F /PID %PIDTOKILL%
goto loop
As I don't know where Unturned.exe is installed on your computer, you may need to adapt the string "%ProgramFiles%\team\steamapps\common\Unturned\Unturned.exe".
%ProgramFiles% is a reference to string value of environment variable ProgramFiles which contains usually C:\Program Files.
On 64-bit Windows there is also the environment variable ProgramFiles(x86) which needs to be used with %ProgramFiles(x86)% in batch code if Unturned.exe is installed somewhere below C:\Program Files (x86).
Related
the below code checks the python version and calls the other python script to excute. I want to take all the logs of the below bat file, including the output of the python script into another log file. doing some test, I am able to put the logs into the file but the output still prints command print window. i thought it will redirect the output.
setlocal
SET "parent=%~dp0"
set LOGFILE=%parent%\run.log
call :LOG > %LOGFILE% 2>&1
:LOG
set "$py=0"
set "$ms=0"
call:construct
for /f "delims=" %%a in ('python dele.py ^| findstr "2"') do set "$py=2"
for /f "delims=" %%a in ('python dele.py ^| findstr "3"') do set "$py=3"
del %parent%\dele.py
if %%py%% == 0 echo "python is not installed" & goto :EOF
echo running with python %py%
for /f "delims=" %%V in ('pip -V') do #set ver=%%V
echo %ver%
pip install --user -r %parent%\requirements.txt
[output on command prompt][1]
echo connectivity script is running...
python %parent%\ConnChk.py
echo connectivity check is completed
goto :EOF
:construct
echo import sys; print('{0[0]}.{0[1]}'.format(sys.version_info^)^)>dele.py
I dont want to print anything on cmd. is there a way? any help would be great!!
[output on cmd][1]
[1]: https://i.stack.imgur.com/qC4pZ.png
I got it worked.
using this link : https://superuser.com/questions/620865/dump-batch-script-output-into-a-text-file-without-specifing-batchfile-location-b
#echo off
REM setlocal enabledelayedexpansion
(
content...
) > "%~dpn0.txt"
enclosing the code with open brackets got it worked!
I have created a BAT file to uninstall any version of skype. It works fine. I am wanting to set it up to also check the event viewer to see that it uninstalled successfully. I was wondering if there was anyone that has done something to this nature. So far, this is what I have:
#echo off
echo Closing skype...
taskkill /F /IM Skype.exe
echo Removing previous versions...
wmic product where "Name like 'Skype%%'" call uninstall
New Code:
#ECHO off
IF EXSIST "C:\Program Files (x86)\Skype" GOTO INSTALLED
ELSE IF EXSIST "C:\Program Files\Skype" GOTO INSTALLED
ELSE GOTO NOTINSTALLED
:Installed
taskkill /F /IM skype.exe
wmic product where "name like 'Skype%%'" call uninstall
shutdown /r /f
:NOTINSTALLED
EXIT
You can check the %errorlevel% variable to get the last error code.
If it's 0, then it worked!
if %errorlevel%==0 echo Uninstall successful
EDIT:
An update for your new code, (you can't do an else if in batch, only use them individually)
#ECHO off
IF EXIST "C:\Program Files (x86)\Skype" GOTO INSTALLED
IF EXIST "C:\Program Files\Skype" GOTO INSTALLED
EXIT
:INSTALLED
taskkill /F /IM skype.exe
wmic product where "name like 'Skype%%'" call uninstall
shutdown /r /f
You can try:
(
echo.Exit code was %errorlevel%
echo.
echo.Uninstall application terminated on %date% - %time%
) > ".\uninstall_log.txt"
If you want to output to console then use this:
echo.
echo.
echo.Exit code was %errorlevel%
echo.
echo.Uninstall application terminated on %date% - %time%
echo.
echo.
echo.
echo.Press any key to exit Skype uninstaller.
pause>nul
exit
Recently I got a script to query the reg set a value and then delete folders under the folder I puled from the query. Looks like this:
pause
FOR /F "TOKENS=2*" %%I IN ('REG QUERY "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\14.0\Outlook\Security" /V OutlookSecureTempFolder') Do SET "ValueData=%%J"
pause
echo Delete Outlook Temp. Files???
echo Enter to continue or Ctrl+C to cancel.
pause
del /q /f /s "%valuedata%\*.*"
del /q /f /s "%systemdrive%\Jacob'sTemp"
pause
echo --------------------------------------------------------------------------------
echo Complete! Goodbye!
echo --------------------------------------------------------------------------------
timeout /t 3
Works great when used locally. So I set up a robo copy to copy a folder with this script in it then use psexec to execute it remotely and it looks like this:
set /p cpu=
robocopy "\\nmcfs01\software\scripts\Jacob's Awesome Outlook Scripts" \\%cpu%\c$\Jacob'sTemp
pause
psexec \\%cpu% -u administrator "%systemdrive%\Jacob'sTemp\outlooktempdelete.bat"
pause
Now it works and it will run but here is the kicker when it goes back to the reg query batch to do the reg query it will run but it skips the 1st pause following the query and it always says it can not find the registry key but I can follow the path and it is there. The worst part is once I end the script there it wipes the computer of everything the user has access to. Not folders but all files/subfiles everywhere. Any insight is greatly appricated!
Here's a quick tidy up of your script:
#Echo Off
SetLocal EnableExtensions
(Set OV=14.0)
Choice /C YN /M "Delete Outlook Temp Files?"
If ErrorLevel 2 Exit/B
Set "BK=HKCU\SOFTWARE\Microsoft\Office\"
Set "EK=\Outlook\Security"
Set "VN=OutlookSecureTempFolder"
For /F "Tokens=2*" %%I In ('Reg Query "%BK%%OV%%EK%" /V %VN%') Do Set "VD=%%J"
PushD "%VD%" && (RD/S/Q "%VD%" 2>Nul) && PopD
REM The below commands will empty Jacob'sTemp:
If Exist "%SystemDrive%\Jacob'sTemp" (PushD "%SystemDrive%\Jacob'sTemp" && (
(RD/S/Q "%SystemDrive%\Jacob'sTemp" 2>Nul) && PopD
REM The below commands without the first two characters will remove Jacob'sTemp
::If Exist "%SystemDrive%\Jacob'sTemp" (RD/S/Q "%SystemDrive%\Jacob'sTemp"
Pause
Echo(------------------------------------------------------------------------------
Echo( Complete! Goodbye!
Echo(------------------------------------------------------------------------------
Timeout 5 >Nul
Give that a try exactly as it is just to see if it makes any difference.
I left the lonely but editable 'set' at the top because this is the only bit that need's changing to cater for earlier or later editions of outlook.
Ive been looking for answer to this question but so far unsucessfully. Can i somehow make it that after batch file is started it wouldnt close if you would press X button on top right (the exit button)? If yes, how to do that?
code for "fake virus"
#echo off
color 47
net stop themes >nul
title DEEP VIRAL INFECTION!
echo VIRAL INFECTION!!!
echo VIRAL INFECTION!!!
echo VIRAL INFECTION!!!
echo ERROR!!!
echo -
echo virus - TROJAN_DEMOLISHER code #45643676
echo -
echo FIREWALL - FAILED
echo -
echo ANTI-VIRUS - FAILED
echo -
echo IP ADDRESS BREACHED!
echo -
echo VIRUS ATTAINING: ****-****-****-8894
echo -
pause
cls
echo -
echo SCANNING INFECTED AREAS...
echo -
pause
title SCANNING INFECTED AREAS...
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
:end
cls
title DEEP VIRAL INFECTION!
echo -
echo 86.5 PERCENT OF MEMORY INFECTED
echo -
echo INFECTION FATAL!
echo -
echo DELETION OF ENTIRE CONTENTS OF LOCAL DISK C: REQUIRED
echo -
pause
cls
echo -
title DELETING HARD-DRIVE C:
echo -
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
cls
title DEEP VIRAL INFECTION!
echo -
echo CONTENTS OF HARD-DRIVE C: ERASED
echo -
pause
cls
echo -
echo SCANNING...
echo -
title SCANNING...
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
dir /s "C:\program files\"
:end1
cls
title DEEP VIRAL INFECTION!
echo -
echo 0.00 PERCENT OF HARD-DRIVE INFECTED
echo -
pause
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
echo ERROR
pause
cls
title SYSTEM FAILURE
color 17
echo ERROR!
echo -
echo VISUAL MEMORY LOST!
echo -
echo RAM LOST!
echo -
echo CORE PROCESSOR FAILING...
echo -
echo TOTAL SYSTEM CRASH IMMINENT!
echo -
echo -
pause
cls
echo -
echo -
echo -
echo SHUTDOWN COMPUTER NOW TO AVOID RISK OF FIRE!
echo -
echo -
echo -
pause
cls
echo -
echo -
echo -
echo SEEK PROFESSIONAL HELP IMMEDIATLY TO PREVENT FURTHER DAMAGE!
echo -
echo -
echo -
pause
start shutdown /s /t 60 /c "SYSTEM FAILURE<SHUTTING DOWN TO AVOID FURTHER DAMAGE!"
multiple dir lines are to give the effect of smth actually happening
There is no way to avoid that the cmd.exe session be closed when the user press the X button, but your Batch file can start a second cmd.exe session via start command and restart it again as soon as it terminates. If the window size of both the initial and second cmd.exe sessions are adjusted properly, you will get the desired effect.
#echo off
if "%1" equ "Restarted" goto %1
:again
echo N|start "" /WAIT cmd.exe /C "%~F0" Restarted > NUL
goto :again
:Restarted
echo I am a virus!
:loop
echo You can't close me!
timeout /T 1 > NUL
goto loop
Also, you were worried about losing the virus effect. Heres a way to make that not happen
Make another Notepad document and put,
start C:\Users\%username%\Documents\The_Name_Of_Virus
start C:\Users\%username%\Documents\The_Name_Of_Virus
start C:\Users\%username%\Documents\The_Name_Of_Virus
start C:\Users\%username%\Documents\The_Name_Of_Virus
start C:\Users\%username%\Documents\The_Name_Of_Virus
pause
Replace The_Name_Of_Virus with the file name of the batch file.
Doing that tells the computer to open the file 5 times. Then if someone closes one of them and it restarts the code, there are still 4 left that are still going. Making it run 5 batch files at the same time will slow your system down a bit. The %username% command uses the username so you do not need to replace %username% with your PC username.
Someone please correct me if I got anything wrong (I don't think I did).
Also, I think you should only put 1 dir command in there because on computers with a lot of filled memory (like mine), it will take a long time to go through every file. I have about 400GB filled on my computer and it took 15mins just to finish the first block of dir commands.
Well instead of deleting task mgr you could just kill taskmgr and then rename it to say nottaskmgr.exe and after the prank thing is done just kill taskmgr and rename it back to taskmgr.exe
Instead of putting "pause" at the end of each speech part you can put
ping -n 2 localhost >nul
That gives it a small delay before moving on the next part.
It basically just tells the system to ping itself and depending on your computer that will take a different amount of time. The 2 is telling the system to ping itself 2 times.
You could copy your .Bat file source and re-create it in a command prompt applications, this will require Visual Studio Community or better.
You can remove this windows Top-Bar utilities through the application frames Properties.
(Use the property tab for this)
You could also make a simple prank virus using a regular windows form application by using the following code with a looped (copy 'n paste) prompt.
MessageBox.Show("message", title);
I hope this helped you.
Yours sincerely,
OTA
I have an issue and can't seem to see where I am going wrong.
I have this code, that is meant to loop through a text file, and look at the last word of the line to see if the user at the start of the line should receive elevated access to their assigned laptop.
However the code just runs through once, and then exits.
#echo off
cls
echo.
echo The following script will process the entire list of students, giving each
echo student administration rights to the laptop and set the laptop description
echo where necessary.
echo.
pause
cls
FOR /F "tokens=1-6 delims=," %%a IN (accesselevations.txt) DO (
set School1=%%a
set LaptopID=%%b
set Model1=%%c
set Serial1=%%d
set User1=%%e
set Access1=%%f
If /i "%%f" EQU "Admin" goto Elevate2
If /i "%%f" EQU "NoAdmin" goto NoElevate2
:Elevate2
set Desc1=!School1! - !LaptopID! - !Model1! - !Serial1! - !User1!
echo.
echo Now creating local admin account for !user! on !LaptopID!
echo.
echo Description: !Desc1!
echo.
pause
psexec \\!LaptopID! -u GBN\!ocusr! -p !ocpw! -n 10 -e net localgroup Administrators "GBN\!User1!" /add
psexec \\!LaptopID! -u GBN\!ocusr! -p !ocpw! -n 10 -e net config server /srvcomment:"!Desc1!"
:NoElevate2
cls
Echo.
Echo User !user1! is not allowed Local Administrator rights on !LaptopID!.
pause
)
pause
:End
The file AccessElevations has confidential data in it so I am not able to post it unfortunately, however it contains something like this
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
...and so-on.
I hope I have given enough information, I'm fairly new here.
Thanks in advance for any light you can spread on the issue.
Toby
Use setlocal enabledelayedexpansion or you won't able to use exclamation point on variables.
Close your FOR loop before Elevate2
Change this:
If /i "%%f" EQU "Admin" goto Elevate2
If /i "%%f" EQU "NoAdmin" goto NoElevate2
To:
If /i "%%f" EQU "Admin" (CALL:Elevate2) else (CALL:NoElevate2)
Using CALL command, your script will return to the end of FOR loop after elevation operation is done, GOTO command connot do this.
Put exit/b before :NoElevate2, otherwise your script won'r return to CALL command point.
Your code should be like this:
#echo off
setlocal enabledelayedexpansion
cls
echo.
echo The following script will process the entire list of students, giving each
echo student administration rights to the laptop and set the laptop description
echo where necessary.
echo.
pause
cls
FOR /F "tokens=1-6 delims=," %%a IN (accesselevations.txt) DO (
set School1=%%a
set LaptopID=%%b
set Model1=%%c
set Serial1=%%d
set User1=%%e
set Access1=%%f
If /i "%%f" EQU "Admin" (CALL:Elevate2) else (CALL:NoElevate2))
:Elevate2
set Desc1=!School1! - !LaptopID! - !Model1! - !Serial1! - !User1!
echo.
echo Now creating local admin account for !user! on !LaptopID!
echo.
echo Description: !Desc1!
echo.
pause
psexec \\!LaptopID! -u GBN\!ocusr! -p !ocpw! -n 10 -e net localgroup Administrators "GBN\!User1!" /add
psexec \\!LaptopID! -u GBN\!ocusr! -p !ocpw! -n 10 -e net config server /srvcomment:"!Desc1!"
exit/b
:NoElevate2
cls
Echo.
Echo User !user1! is not allowed Local Administrator rights on !LaptopID!.
pause
exit/b
:End
#echo off
setlocal
cls
echo.
echo The following script will process the entire list of students, giving each
echo student administration rights to the laptop and set the laptop description
echo where necessary.
echo.
pause
cls
FOR /F "tokens=1-6 delims=," %%a IN (accesselevations.txt) DO (
set School1=%%a
set LaptopID=%%b
set Model1=%%c
set Serial1=%%d
set User1=%%e
set Access1=%%f
If /i "%%f" EQU "Admin" CALL :Elevate2
If /i "%%f" EQU "NoAdmin" CALL :NoElevate2
)
GOTO :EOF
:Elevate2
set Desc1=%School1% - %LaptopID% - %Model1% - %Serial1% - %User1%
echo.
echo Now creating local admin account for %user1% on %LaptopID%
echo.
echo Description: %Desc1%
echo.
ECHO psexec \\%LaptopID% -u GBN\%ocusr% -p %ocpw% -n 10 -e net localgroup Administrators "GBN\%User1%" /add
ECHO psexec \\%LaptopID% -u GBN\%ocusr% -p %ocpw% -n 10 -e net config server /srvcomment:"%Desc1%"
GOTO :eof
:NoElevate2
Echo.
Echo User %user1% is not allowed Local Administrator rights on %LaptopID%.
GOTO :EOF
Notes:
setlocal added to ensure environment does not get polluted by a run
Doesn't appear to be much point in setting Access1 since it isn't used.
CALL :routine rather than goto - Not a ood idea to goto within a block as some cmd versions appear to work differently. Colon is required for running internal subroutine
Within internal subroutines, can use %var% since these run in their own context.
!var! syntax is only effective when invoked by setlocal enabledelayedexpansion (not executed in original batch.)
psexec commands simply ECHOed. Need to remove the preceding echo to execute the psexec
Shouldn't a 'remove privileges' routine be applied for 'Nodamin' ?
!user! changed to %user1% in :elevate2 since user1 is established by the loop, not user.
Suitable datafile used for testing was:
School1,Workstation1ID,Laptop1Model,Laptop1Serial,Student1Username,Admin
School2,Workstation2ID,Laptop2Model,Laptop2Serial,Student2Username,NoAdmin
School3,Workstation3ID,Laptop3Model,Laptop3Serial,Student3Username,Admin
School4,Workstation4ID,Laptop4Model,Laptop4Serial,Student4Username,NoAdmin
not that hard to construct - could easily be done with a prettier version by substituting for names and other sensitive information.