Remotely Executing Windows Console or Shell Commands vs Windows Executables in BatchPatch

Today I’d like to discuss a topic that comes up sometimes that can be confusing when you don’t know anything about what is happening “under the hood” in BatchPatch.

BatchPatch Remote Commands

In BatchPatch we provide two primary ways for executing remote commands– ‘Remote command 1/2‘ and ‘Remote command 3/4 (logged output)‘. What some of you might have noticed is that if you try to run the following syntax (see the DEL command below) in ‘Remote command 1/2‘, it doesn’t work. However, you can take the exact same syntax and put it in ‘Remote command 3/4 (logged output)‘ and it works just fine. What’s going on here?

A Typical Windows DEL (delete) Command

DEL /q "C:\temp\New Text Document.txt"

The command above is a standard Windows deletion command that you can run at the command line of any Windows computer. It will simply delete the specified file. The ‘/q‘ is an instruction to *not* prompt the user for confirmation (y/n). We use the ‘/q‘ when we want to run the command without any further interaction, which is necessary when running it remotely since there is no way to interact with the remote command after it has been submitted. If the ‘/q‘ is not used for a remote DEL command, the file will not be deleted, and the remote command will hang indefinitely while it waits for someone to respond to the hidden y/n confirmation prompt. No one can respond to it though because it’s hidden; hence why we have to use ‘/q‘ in the first place.

Console / Shell Commands vs. Windows Executable Files

OK, so here’s the thing with the DEL command… On Windows computers there is no DEL.exe. Many commands like DEL are built-in to the Windows shell, whereas many other commands like IPCONFIG are actually separate executables. That is to say if you look in C:\Windows\System32, for example, you will find IPCONFIG.exe but you will not find DEL.exe because there is no DEL.exe. This fact is the reason why the above DEL command works in BatchPatch’s ‘Remote command 3/4‘ but not in ‘Remote command 1/2‘. This is explained more below.

Comparing BatchPatch Remote Command 1/2 vs BatchPatch Remote Command 3/4 (logged output)

When BatchPatch executes ‘Remote command 1/2‘, it uses the Windows Process class. It parses the command and assumes that you are running an actual executable. So, if you run ‘IPCONFIG’ in ‘Remote command 1/2‘, it works just fine because IPCONFIG.exe exists on all Windows computers, so BatchPatch uses the Windows Process class to execute IPCONFIG.exe, and everything works as expected. However, if you run a DEL command in ‘Remote command 1/2‘, it fails because BatchPatch looks for but cannot find DEL.exe (because it doesn’t exist).

When BatchPatch executes ‘Remote command 3/4 (logged output)‘, on the other hand, instead of parsing the command for the executable, BatchPatch runs the entire command inside of the Windows command console using CMD.exe. So, a DEL command works just fine in ‘Remote command 3/4 (logged output)‘ because under the hood BatchPatch is actually executing it more like this:

cmd.exe /c del /q "C:\temp\New Text Document.txt"

With all that said, if you want to execute

DEL /q "C:\temp\New Text Document.txt"

using BatchPatch, you can either execute it as-is by putting it into ‘Remote command 3/4 (logged output)‘ or you can modify it to run in ‘Remote command 1/2‘ by using this syntax instead:

cmd.exe /c del /q "C:\temp\New Text Document.txt"
This entry was posted in Blog, General, Tutorials and tagged , , . Bookmark the permalink. Both comments and trackbacks are currently closed.