You are not logged in.

#1 16 Feb 2016 21:33

jetelina
Member
Registered: 30 Aug 2013
Posts: 5

cmd.exe with RUNASADMIN causes troubles in simple FOR command

Hi,
can I get please your help?

My environment description:
1. I have set cmd.exe to always elevate, because I always want Admin
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
C:\Windows\System32\cmd.exe = RUNASADMIN

2. Because Admin cmd.exe open current dir in C:\Windows\System32 I use Autorun:
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
Autorun = cd /d "C:\Users\User\Desktop"

3. And for the security reasons I have set UAC to highest level

Here is my troubled code:

@echo off
cd /d "%~dp0"
whoami
echo(
FOR /F "tokens=*" %%h IN ('whoami') DO echo %%h
pause

When I execute it via right click "Run as administrator" it returns correct result

PC\user

PC\user
Press any key to continue . . .

But when I execute it just by double click, it returns

PC\user

'whoami' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .

I'm not sure why this happens.
- Can I somehow escape the inner FOR command?
- I have also tried usebackq, but it didn't worked
- When I pipe it via file - simple double click it works

whoami > file.txt
FOR /F "tokens=*" %%h IN (file.txt) DO echo %%h

- I have also found solution like on the beginning of script to remove cmd.exe from elevation and on the end add it back (this is quite fragile)

reg delete "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v C:\Windows\System32\cmd.exe /f > nul
... code here ...
reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v C:\Windows\System32\cmd.exe /t REG_SZ /d RUNASADMIN > nul

Can you please advise and possibly explain why this happens?

Offline

#2 16 Feb 2016 22:38

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: cmd.exe with RUNASADMIN causes troubles in simple FOR command

jetelina wrote:

But when I execute it just by double click, it returns

PC\user

'whoami' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .

That message appears when the command can't be located in the current folder or on the path.
Something in your process has changed the path, it would seem.

Offline

#3 17 Feb 2016 09:53

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: cmd.exe with RUNASADMIN causes troubles in simple FOR command

Yeah, run

where /r C:\Windows whoami*.*

and make sure that at least one of the paths the command returns is listed when if you

echo %PATH%

I had copies in C:\Windows\System32 and C:\Windows\SysWOW64

Offline

Board footer

Powered by