Executing a batch file containing an exe path remotely via wmic - wmi

I have a batch file on Computer-A that contains several commands and a path to an exe file.
When I execute the batch file on Computer-B via wmic, I can see all the commands being executed in the batch file except that it does not run the exe. What are the things I need to check for to see why the .exe is not being executed on Computer-A?
wmic execution:
wmic /node:"<server_ip>" /user:domain\user /PASSWORD:"password" /OUTPUT:STDOUT process call create "cmd.exe /c C:\Folder\batch.bat"

File paths in wmic need to be c style paths. So C:\\somefolder\\somefile.ext. \\ for\ in paths.

Related

how to use batch in c++

I have a batch program that I would like to add to one of my c++ programs. The batch program tests if a file exists and exits the script if it doesn't. I do not want to add any more libraries to my code. The problem that I have is that I am not sure how I am able to use batch in c++. I am able to figure everything else out on my own I just need to know this one thing.
You can use the system(" ") command to use batch.
Assuming you are on Windows,
you have two options available to run batch files on Windows from C/C++.
You can use system (or _wsystem for wide characters).
"The system function passes command to the command interpreter,
which executes the string as an operating-system command. system
refers to the COMSPEC and PATH environment variables that locate the
command-interpreter file (the file named CMD.EXE in Windows 2000 and
later)."
Or
You can use CreateProcess directly.
Note that for batch files:
"To run a batch file, you must start the command interpreter; set
lpApplicationName to cmd.exe and set lpCommandLine to the following
arguments: /c plus the name of the batch file."

ofstream writing to my documents when launched remotely with shell from vba from access

I have a vba scrip that launches a .exe this .exe is a program that writes to a file. These files are both in the same directory, and they're both using current directory as inputs and outputs for the read/write/launch.
The .exe writes to the current directory fine, but when I launch it through shell in vba it writes to my documents.
I genuinely have no idea why it's doing it, all I know is it shouldn't be.
Assuming that the MSAccess database that contains the VBA code is in the same directory that contains the EXE file, you can change the current directory to be that by adding the following line to the VBA code:
ChDir Left(CurrentDB.Name, InstrRev(CurrentDB.Name, "\") - 1)
That should be done somewhere prior to the Shell statement.
If the database is not in the same directory as the EXE, you might have to hardcode the path. If, for instance, your Shell command is currently something like
Shell "C:\abc\def\xyz.exe"
then you could use
ChDir "C:\abc\def"
Shell "xyz.exe" ' No *need* to include path information anymore
' because it is now in "current" directory
The best solution though, if you have access to the code for the EXE, is to change the EXE to read/write from files which are located in the same directory as the EXE itself rather than from the "current" directory.

Open CMD in the directory where my program is

I have a piece of software written in c++, that has to call command line and execute 2 simple commands. The problem is, they need to be executed in the main directory of my program (folder where exe file is). How can I make sure, that they will execute in this directory, if it can be different on PCs (for example "Program Files" or "Program Files(x86)").
On windows, you can use the GetModuleFileName WinAPI, which will return you the path of the exe that you can cd to and execute your commands.

Where are files placed when a path does not include a path or drive letter?

When I indicate a file to create and write to via ofstream without a path or drive letter, i.e. "testfile.txt" where is it placed when NOT run in an IDE (when run in VS, the file is placed in the project working directory) and run from a shortcut (I needed to indicate command line arguments)? It does not place it in the same location as the executable when run from a shortcut.
Your shortcut has a "Start in" property, which is the directory where your files will be placed by default (i.e. if you don't specify a path). The main exception is that in Vista, if the directory is in \Program Files\ the actual writes will be redirected to your profile directory.
If you start the program from the command prompt, the default directory is the working directory (i.e. your CMD.EXE prompt when you started your program). This isn't necessarily where your program is located. If your program is on the %PATH% or if you specified a full path to your executable, CMD can run your executable even if is stored outside your current working directory.

How to execute Windows System() command from C:\Windows\System32 folder?

I have a batch file "install.bat" stored in location "
c:\Users\abc\xyz
".
I want to execute this batch file in administrative mode from
C:\Windows\System32
folder using a System() API. Can anyone kindly lemme know how do I achieve this VC++ programatically.
My code snippet::
int ret = System("c:\Users\abc\xyz\install.bat");
If I give this command, batch file is of course executing from "c:\Users\abc\xyz" folder. But I want to run this batch file from System32 folder?
Thanks in advance.
One option is to put a cd command as the first line of your .bat file. You could change the working directory of the calling process, but that's using a hammer to crack a nut.
If you move away from the system function you can call CreateProcess. That allows you to specify all the gory details you need when creating a new process. You need to run the command interpreter (find that by reading the COMSPEC environment variable). You can specify the working directory for the new process as one of the parameters to CreateProcess.
CreateProcess is rather hard to call though. And it won't help you with requesting elevation to admin rights. Instead you can use ShellExecute. Call that passing "runas" for the verb, which will result in elevation.
ShellExecute(0, "runas", "c:\\Users\\abc\\xyz\\install.bat", NULL,
"C:\\Windows\\System32", SW_SHOW);
You need to use ShellExecute.
Something like the following
ShellExecute(hwnd, "runas", "c:\\Users\\abc\\xyz\\install.bat", NULL, "c:\\windows\\system", SW_SHOWNORMAL );
Without using System(), you can just cd to the location containing the .bat file and just invoke the batch file