chocolatey / choco

Chocolatey - the package manager for Windows
https://chocolatey.org
Other
10.35k stars 903 forks source link

Detecting elevated command shell works for PowerShell 7, but not for PowerShell 5 and CMD #3363

Closed agabrys closed 11 months ago

agabrys commented 11 months ago

Checklist

What You Are Seeing?

I encountered a very strange issue. When I run PowerShell 5 (default version on Windows) or Command Prompt as administrator, chocolatey says I did not run them as admin. When I do the same in PowerShell 7, everything works as it should.

PowerShell 5.X PowerShell 5.X

Command Prompt Command Prompt

PowerShell 7.X

PowerShell 7.X

What is Expected?

Every shell run as administrator should be detected as an elevated command shell by chocolatey.

How Did You Get This To Happen?

Prerequsites:

Steps:

  1. Open PowerShell 5.X as administrator and execute command to upgrade all packages
    choco upgrade all -y -i

    it fails with a message

    Chocolatey v2.2.2 Chocolatey detected you are not running from an elevated command shell (cmd/powershell).

    You may experience errors - many functions/packages require admin rights. Only advanced users should run choco w/out an elevated shell. When you open the command shell, you should ensure that you do so with "Run as Administrator" selected. If you are attempting to use Chocolatey in a non-administrator setting, you must select a different location other than the default install location. See https://docs.chocolatey.org/en-us/choco/setup#non-administrative-install for details.

    For the question below, you have 20 seconds to make a selection.

  2. Open Command Prompt as administrator and execute command to upgrade all packages
    choco upgrade all -y -i

    it fails with a message

    Chocolatey v2.2.2 Chocolatey detected you are not running from an elevated command shell (cmd/powershell).

    You may experience errors - many functions/packages require admin rights. Only advanced users should run choco w/out an elevated shell. When you open the command shell, you should ensure that you do so with "Run as Administrator" selected. If you are attempting to use Chocolatey in a non-administrator setting, you must select a different location other than the default install location. See https://docs.chocolatey.org/en-us/choco/setup#non-administrative-install for details.

    For the question below, you have 20 seconds to make a selection.

  3. Open PowerShell 7.X as administrator and execute command to upgrade all packages
    choco upgrade all -y -i

    it works

    Chocolatey v2.2.2 Upgrading the following packages: all By upgrading, you accept licenses for the packages. [...]

System Details

Installed Packages

7zip.install 23.1.0
base64 1.0.0
chocolatey 2.2.2
chocolatey-core.extension 1.4.0
cloudfoundry-cli 8.7.5
dive 0.11.0
gardenlogin 0.4.0
gawk 5.1.0
git.install 2.43.0
golang 1.21.4
grep 3.7.0
jq 1.7.0
keepass.portable 2.55.0
kubectx 0.9.5
kubens 0.9.5
kubernetes-cli 1.28.2
kubernetes-helm 3.13.1
licecap 1.28.0
make 4.4.1
msys2 20231026.0.0
powershell-core 7.4.0
tortoisegit 2.15.0
yq 4.33.3

Output Log

PowerShell 5.X: https://gist.github.com/agabrys/f01bfd8b3a3d3ba0c10ef4fc87b014e3

PowerShell 7.X: https://gist.github.com/agabrys/38ebfa3065a275fa9d7adfc9438e67e6

Additional Context

I reinstalled chocolatey and it did not help. I have a feeling that PowerShell 7.X blocked some files.

agabrys commented 11 months ago

It is currently working as expected, though I am not entirely sure why. It was functioning correctly, then I encountered this strange issue, and now it is working again. I shut down my computer every day, so perhaps that action resolved the problem.

corbob commented 11 months ago

Thank you for the update @agabrys. I have briefly looked through the logs, and I too am at a loss for what could have caused it (other than maybe you thought you were elevated but weren't actually, but that feels really odd for seeing it in multiple shells).

Should it happen again, one troubleshooting step might be to check the tokens to determine if you're administrator. You can do that with this snippet from Sean Wheeler's profile:

$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal] $identity
$principal.IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')