I have the following script in a bat file. If I run NET STOP "XXX" and NET START "XXX", it will work. But when I execute the bat file, it does not restart the service as I expected. The log file however was renamed. I have tried to run the bat file as administrator but still no good.
#echo off
findstr /m "memory" "C:\Services\ServiceLogs\NZTA_OnTheMove_AlertSender.log"
if %errorlevel%==0 (
NET STOP "Jericho NZTA Alert Sender"
timeout 30
NET START "Jericho NZTA Alert Sender"
set HR=%time:~0,2%
set HR=%Hr: =0%
set HR=%HR: =%
rename "C:\Services\ServiceLogs\NZTA_OnTheMove_AlertSender.log" "NZTA_OnTheMove_AlertSender_%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%.err"
)
After bypassing the logic findstr /m "memory" "C:\Services\ServiceLogs\NZTA_OnTheMove_AlertSender.log"
if %errorlevel%==0
Script works again.
Related
Ive got issues with a batch command that wont start whatever i was to do about it, even using call wouldnt start it.
:choice2
set /P c1="text here [Y/N]? "
if /I "%c1%" EQU "Y" start "" /wait /high /max "Powershell.exe -executionpolicy remotesigned -File %batdir%batch\filename.ps1"
if /I "%c1%" EQU "N" goto :end
goto :choice2
Main requirement is that the %batdir% is in the first if line, because the batch runs from a flash drive.
Tried without %batdir%, wouldnt work either way.
Tried using different commands for the powershell.exe, tried using a direct dir of powershell, wouldnt work, always getting the "Cannot find file, make sure it is typed in properly.".
I would appreciate any help anyone could provide.
As you have not provided a Minimal Complete and Verifiable Example, this assumes that %batdir% was the location of the running batch file, which I've replaced with the appropriate %~dp0 variable. You should also note that I've also replaced your set /p command, because that is not the correct command to use for known single key input:
:choice2
"%SystemRoot%\System32\choice.exe" /M "text here"
If ErrorLevel 2 GoTo end
Start "" /Wait /High /Max "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy RemoteSigned -File "%~dp0batch\filename.ps1"
GoTo choice2
:end
Pause
I do not recommend the use of /High or /Max for this task, (including them only because you did). So because you said you also tried call, it seems to me as if you probably don't need to use start with those options either. If you just need to run the powershell script and wait for it to complete, before returning to the input prompt, try this modification:
:choice2
"%SystemRoot%\System32\choice.exe" /M "text here"
If ErrorLevel 2 GoTo end
"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy RemoteSigned -File "%~dp0batch\filename.ps1"
GoTo choice2
:end
Pause
I am trying to execute remote batch file. I could invoke the batch file using PsExec but unable to complete due to :choice in the batch file.
Here is the snippet from batch file
:choice
set /P c=Are you sure you want to continue [Y/N]?
if /I "%c%" EQU "Y" goto :execute_script
if /I "%c%" EQU "N" goto :END
goto :choice
which populating question
I want to handle this question from the command which is like :
cmd /c start C:\temp\PSEXEC\PsExec.exe \\server -u username -p password cmd /c (^cd C:\BatchExecutors ^& SnapExecutor.bat location^)
Suggestions appreciated. Thanks in Advance.
You can use a strange CMD behaviour when 2 batch files have the same label, to bypass the question. But any code prior to the question would be ignored.
To do this, create another batch file with this inside:
call :execute_script
goto:eof
:execute_script
cd /D C:\BatchExecutors
SnapExecutor.bat %*
So what happens here is that this script will call SnapExecutor.bat, but instead of starting from the begining of the script, it will start from :execute_script
The problem now is how to execute this remotely. You might be able to create this script in a remote writable folder using this command:
cmd /c start C:\temp\PSEXEC\PsExec.exe \\server -u username -p password cmd /c "cd /D c:\[temp folder] &echo call :execute_script>temp.bat &echo goto:eof>>temp.bat &echo :execute_script>>temp.bat &echo cd /D C:\BatchExecutors>>temp.bat &echo SnapExecutor.bat %%*>>temp.bat"
(This will create the batch file, then you use this to call it:
cmd /c start C:\temp\PSEXEC\PsExec.exe \\server -u username -p password cmd /c "C:\[temp folder]\temp.bat"
NOTE:
Change [temp folder] to a writeable folder on the remote PC.
I've written a batch file that checks if Hamachi Service is working and if it is not it starts it. I made this script working in background by writing vbs script
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
and executing it
wscript.exe "invisible.vbs" "HamachiRestart.bat"
HamachiRestart.bat looks like this:
for /F "tokens=3 delims=: " %%H in ('sc query "Hamachi2Svc" ^| findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (
net start "Hamachi2Svc"
echo %COMPUTERNAME% %DATE% %TIME% :: Uruchomiono usługę >> LOG.txt
)
)
echo %COMPUTERNAME% %DATE% %TIME% :: OK >> LOG.txt
exit
and when I'm executing it from console it is working fine (restart service and add line to log) but main problem is that when I try to schedule it in Windows Task Scheduler it is working partially (restart service but log is empty...)
Any ideas how to fix it? I just need this log file working.
I think the problem is the relative path of the log file, you need the absolute path like: C:\temp\LOG.txt .
If you start from console the log file can be created in the same directory, but if you try to schedule it the log file can't be created because the current directory is a system dir.
I am using the sc command to remotely restart a process under runas. This works perfectly with a service that has no spaces, but if it has spaces, forcing me to use ' inside the runas "command" quotes, it fails and cannot find the service.
I have already checked the properties of this particular service to ensure the service name (it is the same as the display name).
#ECHO OFF
REM Prompt for Domain & Username. Password will be prompted when run.
set /P Domainn=Enter Domain Name:
set /P Usern=Enter Username:
set service=workingservice
REM set service='Not a working service'
REM Define Start Stops
set userrunasstop=runas /profile /user:%domainn%\%usern% "sc \\%domainn% stop %service%"
set userrunasstart=runas /profile /user:%domainn%\%usern% "sc \\%domainn% start %service%"
:optionmenu
CLS
ECHO 1 - Stop Service
ECHO 2 - Start Service
ECHO q - Quit
ECHO.
set /P optionnum=Enter command number:
GOTO option%optionnum%
:option1
REM Stop Client
%userrunasstop%
goto optionmenu
:option2
REM Start Client
%userrunasstart%
goto optionmenu
:optionq
EXIT
This script works great as shown, but when I comment out set service=workingservice and un-comment set service='Not a working service it doesn't work. Also, if I use runas to open its open cmd.exe, I can then successfully run the sc command with quotes around the service name.
I have set up a workaround for this issue by opening up a new command prompt under runas. And in the new command prompt calling another bat file that executes the sc stop service command. This removes the multiple quoting that was causing the error.
I am still working on passing variables from the runas batch file to the sc batch file to allow the %domain% variable to be input into the sc command.
File runas.bat
set /P Domainn=Enter Domain Name:
set /P Usern=Enter Username:
runas /profile /user:%domainn%\%usern% "cmd sc.bat"
File sc.bat (minus all the frills in the original script)
set service='Service with spaces'
sc \\domainname stop %service%
I have a .vbs file called test.vbs. And this is running test.bat file silently.
In test.bat file,
CD.>"C:\folder\empty.srt"
dir /b /s "C:\folder" | findstr /m /i "\.srt$" > C:\old.txt
CD.>C:\new.txt
.
.
echo for /f "delims=" %%a in ('findstr /G:%new% /I /L /B /V %old%') do (#echo %%~nxa >> C:\added.txt)
.
.
CD.>C:\added.txt
del "C:\folder\empty.srt"
When I run test.vbs manually .bat file works fine.
But when I run test.vbs from Windows' Scheduled Tasks (command is: C:\test.vbs (tried wscript test.vbs) )
Only creates empty.srt and removes empty.srt. The other commands are not working.
I don't understand why (maybe administrator priviliges (Account is administrator too).
I thought running as administrator would solve the problem. Or is there another way to do this? How can I do that?
edit: Also working when I run this command from CMD --> wscript C:\test.vbs
Also its working fine on Windows 8
I also ran into this issue a while back, as well as have answered a variation of this issue before.
If it's a 64 bit operating system, then you need to open the script via "C:\windows\syswow64\cscript.exe your_vbs_code.vbs" if not, just use "C:\windows\system32\cscript.exe".