I intend to use the code below to shutdown or restart a computer. Every time I run this, it goes back to set /p var=Type computer name here: Can you please tell me, what is wrong with my script and how to fix it?
#ECHO off
set /p var=Type computer name here:
cls
TITLE %var% power control!!!!
ECHO Copyright © Joshua Reynolds 2012
ECHO USE THIS PROGRAM AT YOUR OWN RISK
pause
:start
cls
ECHO Choose an option below:
ECHO 1. Shutdown
ECHO 2. Restart
ECHO 3. Abort previous choice
ECHO 4. Exit
set /p choice=Type the options number:
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto shutdown
if '%choice%'=='2' goto restart
if '%choice%'=='3' goto abort
if '%choice%'=='4' exit
cls
ECHO "%choice%" is not a valid option please try again
ECHO.
pause
goto start
:shutdown
COLOR 4f
cls
ECHO To shutdown %var%
pause
shutdown /s /m \\%var%
ECHO %var% should now shutdown.
goto start
:restart
COLOR A0
cls
ECHO To restart %var%
pause
shutdown /r /m \\%var%
ECHO %var% should now restart.
goto end
:abort
COLOR 5f
cls
shutdown /a /m \\%var%
goto end
:end
pause
COLOR 0f
goto start
Perhaps the name of your batch script is shutdown.bat and it is in your current directory. So when your script tries to run the SHUTDOWN command, it re-runs your batch script instead.
Either change the name of your script, or else explicitly use shutdown.exe within your script whenever you want to invoke the command.
Related
I would like to run VSCode from Windows Command Script with options of extensions-dir and user-data-dir.
It is ok on Windows Command Prompt.
But it is failed on Windows Command Scripts.
I tried many ways but all failed.
These is codes I'm trying:
#echo off
setlocal
set VSCODE_DEV=
set ELECTRON_RUN_AS_NODE=1
set configpath=%~dp0..\extensions
set extensionpath=%~dp0..\settings
set exepath=%~dp0..\Code.exe
::"%~dp0..\Code.exe" "--extensions-dir=\"D:\Windows\Programs\VSCode\extensions\""
::"%~dp0..\Code.exe" "--user-data-dir=D:\Windows\Programs\VSCode\settings" %*
::"%~dp0..\Code.exe --extensions-dir=D:\Windows\Programs\VSCode\extensions --user-data-dir=D:\Windows\Programs\VSCode\settings" %*
start "" "" "%~dp0..\Code.exe" "--help" "--version" "%~dp0..\resources\app\out\cli.js" %*
::start /d %exepath% "--list-extensions"
::start /WAIT /B "D:\Windows\Programs\VSCode\Code.exe"
endlocal
Could someone tell me how to fix it?
Thank you all with kind regards!
I am calling a .bat file from Powershell in a Microsoft Windows environment, and would like to know if the exit code was success or failure.
A problem seems to be occurring in this unusual, (although not very unusual), edge-case.
Example #1:
.\test1.bat
ECHO ON
setlocal enabledelayedexpansion
False
if !errorlevel! neq 0 exit /b !errorlevel!
echo "More text"
This works fine. False causes an error; the next line checks the status, sees a 1, and exits with code 1. The calling powershell has the correct result. echo $LASTEXITCODE is 1.
Example #2:
.\test2.bat
ECHO ON
setlocal enabledelayedexpansion
if "testing" == "testing" (
False
if !errorlevel! neq 0 exit /b !errorlevel!
echo "More text"
echo "More code"
True
if !errorlevel! neq 0 exit /b !errorlevel!
echo "More code"
)
Here is the difficulty. False causes an error; the next line checks the status, sees a 1, and exits with code 1. Then, the calling powershell has an unexpected result, echo $LASTEXITCODE is 0.
Could only the batch file be modified to fix the problem?
You're seeing problematic cmd.exe behavior when batch files are called from outside cmd.exe, such as from PowerShell, their exit codes aren't reliably relayed as cmd.exe's process exit code.
For background information, see this answer.
Workarounds:
Call your batch file as follows:
cmd /c '.\test2.bat & exit'
Alternatively, if installing a third-party module (authored by me) is an option, use the ie function from the Native module (Install-Module Native), which additionally compensates for PowerShell's broken argument-passing to external programs (see this answer):
# Assuming `Install-Module Native` was run:
ie .\test2.bat
I have a batch file that calls a powershell script that calls the same batch file again which calls a different powershell script
runPowerShell.bat
scriptWrapper.ps1
Process.ps1
the flow is: runPowerShell.bat > scriptWrapper.ps1 > runPowerShell.bat > Process.ps1
the batch file contains an echo of the Process.ps1 exit code.
if %errorlevel% NEQ 0 GOTO :error
GOTO :end
:error
echo Exit Code: %ERRORLEVEL%
echo Failed!
EXIT /B %ErrorLevel%
:end
echo Exit Code: %ERRORLEVEL%
echo Success!
The thing is, since scriptWrapper runs successfully as well, it also seems to pass back a success even though i have no exit codes in the scriptWrapper.ps1 file. so essentially i end up with two success messages instead of just the one from the Process.ps1 that i want only.
Exit Code: 0
Success!
Exit Code: 0
Success!
so is there a way to suppress the echo in runPowerShell.bat to ONLY echo from the Process.ps1?
I could create another batch file to handle this scenario, but i would like to see if there is an option that doesnt have to involve another batch file here first.
For some reason trying to install a msi executable trough commandline takes too long or never completes. The program is unsigned themes for windows that allows you to run unsupported themes on windows. Its available from here: Download
Im trying to install the 64bit version with:
start /wait "UxStyle Core x64.msi"
The whole batch file looks like this:
#echo off
net stop uxsms
IF "%PROCESSOR_ARCHITECTURE%" == "AMD64" call :install64
IF "%PROCESSOR_ARCHITECTURE%" == "x86" call :install32
IF ERRORLEVEL 1 goto :UxStyleErr
takeown /f "%WINDIR%\Resources\Themes\Aero\aero.msstyles"
icacls "%WINDIR%\Resources\Themes\Aero\aero.msstyles" /grant %USERNAME%:F"
ren "%WINDIR%\Resources\Themes\Aero\aero.msstyles" aero.msstyles.original
copy /y aero.msstyles "%WINDIR%\Resources\Themes\Aero\"
net start uxsms
echo Installation completed. Press any key to reboot or close this dialog if you want to restart later.
pause
shutdown /r /t 0
goto :eof
:install64
start /wait "UxStyle Core x64.msi"
goto :eof
:install32
start /wait "UxStyle Core x86.msi"
goto :eof
:UxStyleErr
echo An error occured while installing UxStyle Core. Installation will now quit.
pause
goto :eof
What am i doing wrong?
Instead of using start /wait to launch the .msi file, I'd recommend calling msiexec.exe directly. You'll also be able to generate a log file which will help you diagnose what is going wrong. So I'd modify your start /wait commands to look like:
msiexec /i "UxStyle Core x64.msi" /l*v x64_installlog.txt
You can add /passive or /quiet to the command to show just a progress bar or run with absolutely no UI respectively.
Please read:
Msiexec (command-line options)
It looks like you are lacking the /QB or /QB switches to run without interaction. Also consider adding REBOOT=R to prevent them MSI from executing any unexpected reboots.
I'm trying to do a batch program that needs to check if the service is installed before checking if it is running/stopped.
I would just like to ask if there is any way that I can check the ERRORLEVEL of an uninstalled service when the OS is Windows XP.
In my code snippet:
ver | find /I "XP"
if %errorlevel%==0 goto ver_xp
goto ver_nonXP
:ver_xp
echo Windows XP
sc query myService > nul
echo %errorlevel%
if errorlevel ___I goto ServiceOk
if errorlevel ___ goto ServiceError
goto ServiceError
:ver_nonXP
echo Windows is not XP
sc query myService > nul
echo error1_percent %errorlevel%
if %errorlevel%==0 goto ServiceOk
if %errorlevel% NEQ '0' goto ServiceError
goto end
:ServiceError
echo Service is not installed
net helpmsg %errorlevel%
goto end
:ServiceError
rem do some operations here....
I tried to use
if errorlevel 1060 goto ServiceError
It seems that if the service is not installed, the condition above will always be false.
I made the errorlevel ____ because I don't know that the correct condition should be.
sc query myService |find "myService" >nul will do the trick
According to this answer this is possible in batch using method you described How does one find out if a Windows service is installed using (preferably) only batch?
Alternatively you could query with powershell:
$serv_status = get-service "myService"
if($serv_status -ne $null)
{
// do some operations here
}