Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Run profile as admin' ignored when running bat or cmd directly (double-clicking) #17859

Closed
Bostonjunk opened this issue Sep 4, 2024 · 12 comments
Labels
Area-DefApp Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Product-Terminal The new Windows Terminal.

Comments

@Bostonjunk
Copy link

Windows Terminal version

1.21.2361.0

Windows build number

10.0.22631.0

Other Software

No response

Steps to reproduce

Change default profile setting to enable 'Run profile as administrator' then try and open a batch file or cmd file by double-clicking on it.

Caveat - I am logged into Windows as a standard user but need to run scripts under a separate admin account. Terminal is installed for both the standard and admin user.

Extra context - I upgraded from a Windows 10 to a Windows 11 laptop - everything else works fine, this is the only thing I can't get functioning as it did before.

Expected Behavior

To double-click on the bat or cmd file and be prompted for elevation and the bat or cmd to launch in an elevated Terminal window.
This is how it functioned on my old laptop.

Actual Behavior

I am not prompted for elevation and the bat or cmd file opens in a standard session.

If Terminal is run directly, or the + is pressed for a new tab, only then am I prompted for elevation.

@Bostonjunk Bostonjunk added Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Sep 4, 2024
@zadjii-msft
Copy link
Member

That's totally by design. When you double-click on a batch script, the OS launches an instance of CMD.exe to process that script, then launches a terminal application to handle that script. By the time the Terminal is launched, the script is already running unelevated. The terminal settings can't affect the elevation of that process anymore. That hasn't changed between Windows 10 & 11

To double-click on the bat or cmd file and be prompted for elevation and the bat or cmd to launch in an elevated Terminal window.
This is how it functioned on my old laptop.

Are you actually clicking on a shortcut to a batch file, one that's got this box checked?
image

because otherwise I don't know how a batch script could request to be run as admin... Maybe it's something specific to that script/?

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Sep 4, 2024
@Bostonjunk
Copy link
Author

Bostonjunk commented Sep 4, 2024

Nope, if I use a shortcut as shown in your image, it will open in a conhost window.

On my previous Windows 10 laptop I was using 2 days ago, if I double-clicked directly on a bat or cmd file, it would ask me for elevation and run in an elevated Terminal window - I was just wanting the same behaviour on my new laptop.

The only setting I changed on my old laptop to make this happen was 'Run profile as administrator' - then it functioned as described - I've been using it that way for over 2 years.

EDIT: On my new laptop, if I right-click and run as admin, it will run in a conhost window. If I double-click, it will run in a standard Terminal session.
The only way to currently have it running in Terminal AND as admin, is to run Terminal as admin, then navigate to the folder via the command line and launch it that way, which is much less convenient than just double-clicking on the file.

The bat files themselves serve only as launchers for PowerShell scripts, so the files themselves are literally just:
@ECHO OFF
CLS
powershell -Executionpolicy bypass -file [path to script]
PAUSE

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs-Attention The core contributors need to come back around and look at this ASAP. and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Sep 4, 2024
@Bostonjunk
Copy link
Author

Bostonjunk commented Sep 4, 2024

20240904.143352-new.mp4

@zadjii-msft
Copy link
Member

zadjii-msft commented Sep 4, 2024

That's... definitely not supposed to happen 😨

When you get the UAC prompt for the Terminal, what's the commandline it shows? (under "Show more details")

@Bostonjunk
Copy link
Author

20240904_145833

@zadjii-msft
Copy link
Member

huh. That's wacky. It

  • boots the unelevated terminal
  • decides that ....\Enable AD Account.bat matches one of your profiles (with a guid of {3ad42e7b-...)
  • evaluates that the profile it matched should be elevated
  • then hands off the commandline instead to an elevated window

So what ends up running is a second copy of that batch script, with the original one presumably terminating early when the original terminal process exits.

That's super weird.

That is on 1.20, but I don't think we changed that in 1.21.
https://github.com/microsoft/terminal/blame/7b50f12a780ec0f8a55e7f04ad12d65b451f7667/src/cascadia/TerminalApp/TerminalPage.cpp#L305-L314

Most recent changes there are:

Unless we're just not matching this commandline anymore, but I'd doubt that.

Could you manually install a 1.20 Terminal on your win11 machine, and see if that works the same as before? Or inversely, a 1.21 Terminal on win10? I cannot imagine that the handoff logic that's in the OS is different between those two OS versions.

@Bostonjunk
Copy link
Author

I've had this behaviour going back quite a long way - it survived many updates.
I don't know when I first installed Terminal, but I'm pretty sure in the early days it wasn't a Store app, but installed via an old-skool MSI package, maybe? I can't remember my exact update path over the years, but that's the only 'non-standard' aspect to things here. Possibly something to do with my upgrade path from very early versions? (Clutching at straws here a bit)

@zadjii-msft
Copy link
Member

Terminal's been an MSIX "store package" since our earliest release - 0.2, so I doubt it's that. I just want to try and narrow down where the delta happened that regressed this scenario for you.

What's the profile in your settings.json with the 3ad42e7b... guid? The commandline for that might be enlightening

@carlos-zamora carlos-zamora added Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something and removed Needs-Attention The core contributors need to come back around and look at this ASAP. labels Sep 4, 2024
@Bostonjunk
Copy link
Author

Apologies - did you want the settings.json file itself or something else as well?

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs-Attention The core contributors need to come back around and look at this ASAP. and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Sep 5, 2024
@zadjii-msft
Copy link
Member

Yea the settings.json file would be good enough ☺️

@zadjii-msft zadjii-msft added Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something Product-Terminal The new Windows Terminal. Area-DefApp and removed Needs-Attention The core contributors need to come back around and look at this ASAP. labels Sep 5, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. label Sep 9, 2024
@1EDExg0ffyXfTEqdIUAYNZGnCeajIxMWd2vaQeP

still relevant AFAIK

@microsoft-github-policy-service microsoft-github-policy-service bot removed the No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. label Sep 23, 2024
@zadjii-msft
Copy link
Member

@1EDExg0ffyXfTEqdIUAYNZGnCeajIxMWd2vaQeP if you're hitting this too, then the same question applies to you ☺️ That's the next thing I'd investigate, and can't do much else without it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-DefApp Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Product-Terminal The new Windows Terminal.
Projects
None yet
Development

No branches or pull requests

4 participants