I want to derive a simple reliable method to auto elevate a running batch without using extra VBS files or elevated shortcuts, proposed in other threads. Calling the UAC dialog from the batch via javascript ensures the short simple code.
The script below auto elevates a user to Admin rights correctly, when Yes is chosen in the dialog, but the error dialog popups (outside of Cmd window) "Windows cannot find 'test.bat'". Can it be since the path-to-file includes spaces? How to fix the code or suppress this error popup?
@echo off
:: test admin rights
>nul 2>&1 net file
echo '%errorlevel%'
MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute("%~nx0", '', '', 'runas', 1);close();"
:: test admin rights
>nul 2>&1 net file
echo '%errorlevel%'
if !errorlevel! equ 0 echo Hello >%temp%\tst.txt
exit /b