0

I have tried Google, but perhaps Google mojo is bad, since I have not found and suitable explanation

I have a batch file that stops and starts some services. This batch file is then scheduled in task scheduler using a service account.

If I change the extension from .bat to .cmd then the script fails, since the user does not appear to have the rights to stop and start a service.

But why does this change behavior just by changing the extension of the script?

  • It shouldn't. Does anything else change? – LPChip Apr 5 '17 at 8:07
  • 1
    This has been discussed on stackoverflow with a good answer. – Alesito Apr 5 '17 at 8:11
  • Also, what OS are you using? – LPChip Apr 5 '17 at 8:24
  • Sorry I did not explicit mention that I am in a Windows 2008 R2 Server. I the time since, I have come to the conclusion, that my errors might not relate to bat vs cmd, but the TaskScheduler itself, and if you Google that, you’ll find a whole new bag of issues. Also thanks to @Alesito for the link to StackOverflow – Verakso Apr 6 '17 at 9:25
4

What is the difference from using bat and cmd?

BAT was created to interact with COMMAND.COM, the command interpreter of DOS. Microsoft adopted most of the DOS commands into their new interpreter named CMD. EXE. CMD was created to interface with CMD.EXE and it breaks compatibility with COMMAND.COM. Another key difference is in how they handle the errorlevel variable. When using BAT, this variable is only changed once an actual error occurs and no change in state occurs when the each command executes successfully. This is not true for CMD as the errorlevel variable would still change state even if no errors occur. Programmers should take note of this when creating elaborate scripts as it may cause a little bit of confusion.

Aside from those minor differences, CMD and BAT are identical to each other. Most users who create simple scripts to clear or transfer files around should not encounter any problem. For users of the more recent versions of Windows, BAT and CMD are pretty much interchangeable as CMD.EXE would interpret and execute the commands in both files. Although most users are aware of this fact, a lot of the older people who had a chance to work with DOS and its batch files still use the BAT extension; simply out of habit and familiarity.

Summary:

1. The BAT extension is used by DOS and Windows while the CMD extension is for Windows NT Command Scripts

2. The BAT extension can be interpreted by COMMAND.COM and CMD.EXE while the CMD extension can only be interpreted by CMD.EXE

3. The errorlevel always changes state in CMD but only on errors in BAT

source


If I change the extension from .bat to .cmd then the script fails

In order to fully give you an accurate answer on this portion of your question, the logic you are using needs to be disclosed in order to understand what it's doing to help troubleshoot.

Otherwise, I would assume the failure you are seeing has to do with how the .CMD scripts handle errorlevel differences or perhaps some Group Policy or other restrictions that affects the interpretation by CMD.exe and not when run and interpreted by COMMAND.COM.


Further Resources

  • Thanks for the elaborate answer As mentioned above, I have come to the conclusion that strange behavoiur is more relared to the configuration in TaskScheduler that the differences between bat and cmd (unless there is some differences to the elevated rights between cmd and command). I ended up adding some eventlogging to my script, that can found here. That way it was easier for me to spot if the script did actually run. – Verakso Apr 6 '17 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.