Batch not launching exe when in Administrator mode - windows

I've got a simple exe application that writes in a file the first argument with which it gets called, so from command line I can do
MySimpleApp.exe "FOO"
and in the SimpleFile.cfg I get "FOO".
If I try to run this batch (it's in the same folder of the app)
set mypath=%~dp0
%mypath%MySimpleApp.exe "FOO1"
%mypath%MySimpleApp.exe "FOO2"
%mypath%MySimpleApp.exe "FOO3"
every time MySimpleApp gets called Windows ask the administrator permissions to execute the app. I thought that I could just run the batch as administrator, but even if I get no UAC prompt the application doesn't execute.
Is there some option or command that I must use to call an exe file when the batch is launched in administrator mode?

Filenames with spaces MUST be enclosed in quotes.
Always tack a pause at the end of a batch to see how it is interpreting your commands. And likewise if using Echo Off turn it on. Hiding information from yourself about YOUR error is not wise.
So
C:\Users\FirstName LastName\Desktop>C:\Users\FirstName LastName\Desktop\MySimpleApp.exe "FOO3"
'C:\Users\FirstName' is not recognized as an internal or external command,
operable program or batch file.

Related

Why does "start firefox" command works in Windows command prompt?

I'm curious why commands like "start iexplore" and "start firefox" work in Windows cmd.
They're not standalone commands. If you try to type in just "firefox", you'll get:
"'firefox' is not recognized as an internal or external command,
operable program or batch file."
This leads to the conclusion that this is a special behaviour of the "start" command.
My first guess was that it works in a similar way to how the %path% variable is used, having known directories to search in.
I ruled it out easily by trying to run "start [executable]" for another executable located in the same directory as firefox.
My conclusion is that somewhere on my computer there's a list of designated file paths, that can be started by simply typing their file name after the "start" command, instead of the entire path.
Imagine the potential of being able to add things to this list..
Anyone knows where I can find it?
It is in the registry
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\
Under this key there are defined applications that can be invoked without need to change the path environment variable.
Usual invocation (typing the name in the command line) will not search inside this list, but call to start command, windows Run dialog or call from anything that uses the ShellExecute or ShellExecuteEx API calls, will check for applications defined in this list.

Using RUNAS to use the local Administrator account to run a second batch file with elevated privileges

I am trying to develop a simple script that can run a second script as the machine's local administrator. As the first script I am currently trying:
set name=%computername%
runas /noprofile /user:%name%\Administrator "cmd.exe /c %cd%bg2.2.bat"
When I run the script it never seems to run the second script, which is located in the same directory. The second script is only for copying files.
del "C:\Windows\obh_logo.bmp"
copy "%cd%obh_logo.bmp C:\Windows\obh_logo.bmp"
When the first script runs I am prompted for the admin password as expected and once I enter it, a second command prompt seems to flicker on the screen then vanishes. It never appears to run the second script. What seems to be the reason for this not working?
%cd%will probably not point where you think it does, (just tested it, of course the variable gets replaced before the command gets called and does actually point to the first scripts dir, but the problem still persists in the second script)
the runaschanges the working directory for the cmd.exe instance you are starting, probably to C:\Windows\system32.
replace it with %~dp0, which yields the drive and path of the current batch file.
But! Your script will not run the second file elevated, just under the Administrator account, which is not the same. Manual confirmation is always necessary, see here for possible ways to request elevation from within your script.
I now assume the missing rights is the actual problem, add a pause statement in your second script to watch it, or call it with cmd /k so the console doesn't close.

prevent Windows .bat file from quiting

I have a very simple Windows .BAT file:
set PATH=c:\xxx;%PATH%
call foo.pl
set VAR=true
I thought "call" will start a new batch process, without affecting the current one. However, the batch file exited immediately after the foo.pl finished executing. The set VAR=true has never been called.
Is there a way to fix it?
foo.pl is not a batch file, it is a Perl script.
So you need to use
path c:\xxx;%PATH%
"Path to\Folder With\perl.exe" "foo.pl"
rem Additional batch code executed after Perl script execution finished.
In other words you have to run the console application perl.exe best with full path, or with just perl.exe if program files folder of Perl is not the same on all computers on which this batch file is used and hopefully PATH contains also the directory containing perl.exe.
If you specify on a command line or in a batch file just foo.pl, Windows looks in Windows registry which application is associated with .pl for action Open. If there is such a file association, Windows runs this application in a separate process like when using command start.
So using call foo.pl is like using start foo.pl.
PATH is not only an environment variable, but also an interal command written for changing the value of environment variable PATH at any time within a batch file. This is the reason why I removed set from first line. It is better to use internal command path for modifying environment variable PATH.

Run an input file using an exe file with cmd

I am using Windows 7
How can i run an input file (text file of commands) in an exe progam in CMD please.
Using other questions on the site, i have tried:
CMD /c ""C:/Program Files/Mplus/Mpluswin.exe" "C:/Users/jj/Desktop/mplus/test_mplus.inp""
which opens the input file in the program but does not run it
and this, which opens the program, but not the script
CMD /c "C:/Program Files/Mplus/Mpluswin.exe" < "C:/Users/jj/Desktop/mplus/test_mplus.inp"
Does this depend on the exe program?
Edit:
At present, the first command above launches the exe program and opens the text file within it (this is a file of program specific commands that will read in data, run calculations and output automatically). I can then run the commands in the exe program that has been opened (by selecting run in a menu) . But, I would like to pass the file to the exe program and it to be run automatically, ideally in the background. I am not sure of the correct terminology to use, so sorry if my description is unclear.
I've just noticed that you enclosed the entire term in an extra set of double quotes, and used linux forward slashes - try this batch file and also see if there is any error message on the console.
#echo off
cd /d "%userprofile%\Desktop\mplus"
"C:\Program Files\Mplus\Mpluswin.exe" "test_mplus.inp"
echo mplus was launched
pause

Any known limitations to special characters in shortcut pathname and "Run as Administrator"?

I'm working on a project that creates Windows shortcuts to batch files, and have been testing special characters in the pathname to the batch script. One problem that has me stumped is that a pathname will work when the shortcut is invoked normally, but if "Run as Administrator" is used, it does not work.
For example, if the pathname to the shortcut is "C:\Program Files\myapp )]}##$_-=+'.{[(\bin\hello.bat", I can run it fine when I click on the shortcut. When I select "Run as Administrator" it does not work. The shortcut Target value does have double quotes around the pathname.
I can invoke the above pathname with no problems from the Command Prompt, either running as Administrator or as a regular user.
I put in a prompt at the beginning of the batch script to determine if the batch script itself fails, but it does not. When run-as is used, it appears the script is never launched.
My guess there is something in how Windows (7) performs the run-as operation that is getting tripped up by one (or more) of the special characters in the pathname. Is there any known/documented pathname characters limitations of using run-as (with batch scripts[?])?
UPDATE:
After some testing, I discovered that it is the '#' character that screws things up. When I run a test with that character not present, the run-as operation succeeds. Not sure what is special about that character wrt run-as.
On my Windows 10, if a .cmd file's name contains # or ^ or & , the file cannot be run as an administrator. If a .cmd file's parent folder's name contains # or ^ or & , the file cannot be run as an administrator even if the file's name does not contain # or ^ or &.
Ironically, if a .cmd file's name is:
πŸ’žπŸ’”γŠ£γ€‡β—β™₯β˜…βœΊγŠ₯❀️🏩
the file can be run as an administrator.

Resources