Basic Windows Command don't work in .cmd file - batch-file

Here is my dummyScript.cmd file contents-
#echo off
set logfile=LogFile.log
echo Deleting backed up databases and folder > %logfile%
RMDIR /S /Q dummyFolder
if %ERRORLEVEL% NEQ 0 (
echo Unable to delete the back-ups. Please check the log file '%logfile%'
call start notepad %logfile%
exit %ERRORLEVEL%
)
echo Copying another backed up folder from a remote server... >> %logfile%
xcopy /Y "\\dummyRemoteServer\c$\dummy\folder" "C:\Dummy\Folder" >> %logfile%
if %ERRORLEVEL% NEQ 0 (
echo Unable to the backed up folder from remote server. Please check the log file '%logfile%'.
call start notepad %logfile%
exit %ERRORLEVEL%
)
When I run that as Administrator, nothing happens. When I run the script from command prompt, it gives me this error message-
The syntax of the command is incorrect.
OS: Windows 10
Many people have posted this, but the solutions didn't work for me. Any suggestions?

Related

How to create a service to run an application in admin mode or how can we configure an application to run in admin mode always using batch script?

I faced this problem, my task to delete some folder and some service and to change system environment variable automatically after some days of installation. So I created a .exe file which will do all above requirements. But I faced this problem for deleting service, changing environment variable we need administrator privilege.
My .exe file contains below codes
sc stop LaptopAudit
sc delete LaptopAudit
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V MYPROJECT
start cmd /k "#RD /S /Q %projectFolder% & exit"
my Service creation coding is here
nssm install LaptopAuditDestruct "%FolderPath%\selfdestr.exe"
I need to execute this selfdestr.exe file after some days automatically also in administrator mode. Is it Possible? Please Help me. Thank You. Also tried to create task scheduler there also same problem administrator mode required.
To run programs in admin using a batch-file you must have admin permissions on your script first, if it's the same as C++. Here's something I've been using that I found somewhere on this website. Put it at the begining of your script and tell me if it worked!
rem Checking permissions
if "%PROCESSOR_ARCHITECTURE%" == "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) else (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)
rem If error then no admin permissions
if %errorlevel% NEQ 0 (
echo Catching admin permissions
timeout /t 2 /nobreak >nul
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
) else (
pushd "%CD%"
CD /D "%~dp0"
)

Log a successful uninstall with a BAT file

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

Creating and configuring remote server shares in BATCH

I took this portion out of a larger script that I'm modifying in order to create and set the permissions to shares on remote file servers. I know this can done with Powershell but there are legacy servers preventing me from using it in this case unfortunately.
My concerns are that I'm not sure you can stack the psexec commands the way that I'm attempting to, that it is processing the parenthesis, and that it may not be passing on the variables to psexec properly. Trying to use call in this case may also add a lot of complexity but may be an option.
#echo off
setlocal EnableDelayedExpansion
set Fs1FQDN=fs1.city.local
set Fs2FQDN=fs2.city.local
set Fs3FQDN=fs3.city.local
set BSTFQDN=bst-fs1.city.local
:: this is set to whatever server is being tested
set UserFileServer=fs1.city.local
set UserName=footest
:: please note that for icacls I am setting up a user I know exists to insure that it can appropriately set permissions
:EMPTY_FUNCTION
echo.
echo EXECUTING CREATEUSERFILESHARE
echo.
CREATEUSERFILESHARE
:CREATEUSERFILESHARE
if UserFileServer==%Fs1FQDN% (
echo.
echo Generating user folder on file server %UserFileServer%
echo.
psexec \\%Fs1FQDN% -e cmd /c mkdir E:\UDrives\%UserName% ^& attrib +h E:\UDrives\%UserName% /s /d ^& icacls "E:\UDrives\%UserName%" /grant:r "CITY\%UserName%":(OI)(CI)M)
echo. User folder successfully created
echo.
goto CLEANUP
) else if UserFileServer==%Fs2FQDN% (
echo.
echo Generating user folder on file server %UserFileServer%
echo.
psexec \\%Fs2FQDN% -e cmd /c (mkdir E:\UDrives\%UserName% ^& attrib +h E:\UDrives\%UserName% /s /d ^& icacls "E:\UDrives\%UserName%" /grant:r "CITY\%UserName%":(OI)(CI)M)
echo. User folder successfully created
echo.
goto CLEANUP
) else if UserFileServer==%Fs3FQDN% (
echo Generating user folder on file server %UserFileServer%
echo.
psexec \\%Fs3FQDN% -e cmd /c (mkdir E:\UDrives\%UserName% ^& attrib +h E:\UDrives\%UserName% /s /d ^& icacls "E:\UDrives\%UserName%" /grant:r "CITY\%UserName%":(OI)(CI)M)
echo. User folder successfully created
echo.
goto CLEANUP
) else if UserFileServer==%BSTFQDN% (
echo.
echo Generating user folder on file server %UserFileServer%
echo.
psexec \\%BSTFQDN% -e cmd /c (mkdir D:\UserHomeDrive\UDrives\%UserName% ^& attrib +h D:\UserHomeDrive\UDrives\%UserName% /s /d ^& icacls "D:\UserHomeDrive\UDrives\%UserName%" /grant:r "CITY\%UserName%":(OI)(CI)M)
echo. User folder successfully created
echo.
goto CLEANUP
) else (
echo.
echo Invalid selection or no file server specified. Skipping user folder creation...
echo
goto CLEANUP
:CLEANUP
echo.
echo CLEANUP INITIATED
exit
I did get this resolved. So it turns out you don't need to stack remote commands to the terminal in psexec. Since each successful remote command returns an error code it will proceed to the next set of commands. Please note that you will not get any sort of error code back unless you log it on the machine you are performing remote executions on.
A working chunk of the originally posted code would be.
:CREATEUSERFILESHARE
if UserFileServer==%Fs1FQDN% (
echo.
echo Generating user folder on file server %UserFileServer%
echo.
psexec \\%Fs1FQDN% -e cmd /c mkdir E:\UDrives\%UserName%
psexec \\%Fs1FQDN% -e cmd /c icacls "E:\UDrives\%UserName%" /grant:r "CITY\%UserName%:(OI)(CI)M"
echo. User folder successfully created
echo.
goto CLEANUP

Devenv command line switches error

I have a simple solution which contains next to class projects also setup projects (extension installed in VS: vdproj).
Facing strange behaviour doing the following:
Open solution in VS2013 -> kick off rebuild -> no errors.
Create a small batch file:
CALL "%VS120COMNTOOLS%vsvars32.bat"
DEVENV "D:\Source\Solution.sln" /Rebuild "Debug|Any CPU" 1>NUL 2>&1
IF ERRORLEVEL 1 GOTO Error
IF ERRORLEVEL 0 GOTO Yeah
:Error
COLOR c
Echo.
Echo end %date% - %time%
Echo Failed!
PAUSE
:Yeah
Echo.
PAUSE
Run this batch file and the errorlevel is not zero
Changed now inside the batch file the devenv action to the following:
DEVENV "D:\Source\Solution.sln" /Clean "Debug|Any CPU" 1>NUL 2>&1
DEVENV "D:\Source\Solution.sln" /Build "Debug|Any CPU" 1>NUL 2>&1
Run this batch file and the errorlevel is zero
The strange thing is that without output redirection I do also not get any errors while running the batch with the rebuild action active.
Any suggestions why I do get a different behaviour?
In the first case you are doing a Rebuild. In the second case you are doing a Clean and Build. Temporarily remove the 1>NUL 2>&1 and you should be able to see what the error is all about.
You might simplify things a bit and save the results like this. Then you can view the log file before you 'Press any key to continue' if you want.
CALL "%VS120COMNTOOLS%vsvars32.bat"
SET "LogFile=%Temp%\BuildLog.txt"
IF EXIST "%LogFile%" DEL /F /Q "%LogFile%"
DEVENV "D:\Source\Solution.sln" /Rebuild "Debug|Any CPU" 1>"%LogFile%" 2>&1
IF ERRORLEVEL 1 (
COLOR c
Echo.
Echo end %date% - %time%
Echo Failed! View log file %LogFile% if you prefer. Then
)
PAUSE
IF EXIST "%LogFile%" DEL /F /Q "%LogFile%"

Is my batch file compatible with all computers?

Will this work on Windows 8 and always work? I've tested it on Windows 7 but I'm not sure... I had a friend test it and he said that it coudn't find the correct path but I checked and the syntax of the commands is the same and I don't know why there'd be a problem. This is assuming %USERPROFILE%\Documents\My Games\Terraria\Players and %USERPROFILE%\Google Drive exists already
#echo off
echo Administrative permissions required. Detecting permissions...
net session >nul 2>&1
if %errorLevel% == 0 (
echo Success: Administrative permissions confirmed.
) else (
echo Failure: Current permissions inadequate. Please run as administatior.
pause >nul
exit
)
echo ---------------------------------------------
echo Below enter "S" for simple install type or "A" for avanced install type.
echo (Simple is recommended, only use advanced if you know what your doing!)
echo ---------------------------------------------
set /p option=Enter:
if /i "%option%"=="S" goto simple
if /i "%option%"=="A" goto advanced
echo Your entry did not match available options. Try again.
pause >nul
exit
:simple
mklink /d "%USERPROFILE%\Google Drive\Terraria" "%USERPROFILE%\Documents\My Games\Terraria\Players"
cd %USERPROFILE%\Google Drive\Terraria\
copy /y NUL marker >nul
cd %USERPROFILE%\Documents\My Games\Terraria\Players
if exist marker (
echo Validation of installation complete. Symbolic link functional.
del marker
) else (
echo SOMETHING WENT WRONG!!!!!!!!
)
echo ==============
echo You Selected Simple. & echo.If there are no errors above, your installation should be complete.
echo ==============
pause >nul
exit
:advanced
mkdir "%USERPROFILE%\Google Drive\Terraria"
mklink /d "%USERPROFILE%\Google Drive\Terraria\Players" "%USERPROFILE%\Documents\My Games\Terraria\Players"
cd %USERPROFILE%\Google Drive\Terraria\Players
copy /y NUL marker >nul
cd %USERPROFILE%\Documents\My Games\Terraria\Players
if exist marker (
echo Validation of installation complete. Symbolic link functional.
del marker >nul
) else (
echo SOMETHING WENT WRONG!!!!!!!!
)
echo ==============
echo You Selected Advanced. & echo.If there are no errors above, your installation should be complete.
echo ==============
pause >nul
exit
If delivered that all paths exist....
mklink command was introduced in Vista so the script won't work on XP/2003.

Resources