gerardog / gsudo

Sudo for Windows
https://gerardog.github.io/gsudo
MIT License
5.07k stars 136 forks source link

Commands with Bool ($True/$False) Paramiters do not run within a gsudo session #349

Closed hossimo closed 2 months ago

hossimo commented 2 months ago

Issue Description

I get different results from an elevated Powershell vs a Normal one, this seems to happen when I use a Command that requires a $True or $False parameter.

Steps to Reproduce

  1. From within a non-elevated powershell I run the command gsudo Disable-NetAdapter -Name "Ethernet" -Confirm:$False
  2. This gives the the following error after UAC:

    Disable-NetAdapter : Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by parameter 'Confirm'. At line:1 char:44 Disable-NetAdapter -Name Ethernet -Confirm:False -Verbose

    
        CategoryInfo          : InvalidArgument: (:) [Disable-NetAdapter], ParameterBindingException
        FullyQualifiedErrorId : CannotConvertArgument,Disable-NetAdapter

However, if I run the same command from an elevated (obviously without (g)sudo) PowerShell it works correctly:

It almost looks like the gsudo is converting the $False to a string "False"

Screenshots

Working from Elevated: image

Not working from Normal PS: image

Context:

gerardog commented 2 months ago

Please enclose your command to elevate in single quotes, or even better: in {} brackets

You will find this information usefull: https://gerardog.github.io/gsudo/docs/usage/powershell#using-gsudo-scriptblock-syntax

hossimo commented 2 months ago

Thanks so much for answering, Typical RTFM for me :)

Thanks again this has been a real help! now I just need to make an alias for !!

gerardog commented 2 months ago

Lol, happy to help :)

And btw, don't make an alias for !!, write gsudo, press space, and then press tab, for example:

PS C:\> echo something
something
PS C:\> sudo [press tab key]
PS C:\> sudo { echo something }      # magic autocomplete

(this requires Import-Module gsudoModule in your profile )

hossimo commented 2 months ago

Amazing! Thanks so much again! I'm so glad this will be in Windows by default. I bounce between Windows, Mac and Debian constantly and this is a huge help for my mental gymnastics.

gerardog commented 2 months ago

Microsoft's sudo is like 2019's gsudo. You may still want to run winget install gsudo ;)