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

Issue: gsudo: "Error: Invalid option: -noninteractive". The gsudo scriptblocks also fail. #305

Closed
futuremotiondev opened this issue Sep 30, 2023 · 5 comments · Fixed by #306

Comments

@futuremotiondev
Copy link

Issue Description

I have a Powershell 7 script that modifies the registry and sets some commands with netsh. Here's the code:

Clear-Host

$PingResult = & ping 127.0.0.1
$PingResult[2] -match 'TTL=([\d]+)'
$CurrentTTL = $Matches[1]

Invoke-SpectrePrintMarkup -Message "--------------------------------------------------------`n"
Invoke-SpectrePrintMarkup -Message "[grey66]Current TTL Value is[/] [white]$CurrentTTL[/]`n"
Invoke-SpectrePrintMarkup -Message "--------------------------------------------------------`n"

Invoke-SpectrePrintMarkup -Message "[grey66]Windows 10 Default value for DefaultTTL is[/] [white]128[/]"
Invoke-SpectrePrintMarkup -Message "[grey66]Reccommended tweaked values are in the range of [/][white]55-70[/]`n"
[System.String]$TTLSelection = Invoke-SpectreTextPrompt -Prompt "[#00d787]Specify your desired DefaultTTL:[/]"

$TTLSelectionInt = $TTLSelection -as [int]
while(($TTLSelectionInt -lt 55) -or ($TTLSelectionInt -gt 128)){
    Write-Error "TTL Value is outside of the reccommended range. Please enter a different value.`n"
    [System.String]$TTLSelection = Invoke-SpectreTextPrompt -Prompt "[#00d787]Specify your desired DefaultTTL:[/]"
    $TTLSelectionInt = $TTLSelection -as [int]
}

if($TTLSelectionInt -eq 128){
    $Prompt = "Are you sure you want to reset [WHITE]DefaultTTL[/] to default? ($TTLSelectionInt)"
    $Success = "`nSetting [white]DefaultTTL[/] to [white]$TTLSelectionInt[/] now.`n"
    $Result = Invoke-SpectreTextPromptConfirm -Prompt $Prompt -ConfirmSuccess $Success
    if($Result -ne "Y"){
        exit
    }

    gsudo { netsh int ipv4 set glob defaultcurhoplimit=128 >NUL }
    gsudo { netsh int ipv6 set glob defaultcurhoplimit=128 >NUL }
    gsudo { reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters" /v DisabledComponents /f }

} else {
    
    Write-Host "`$TTLSelectionInt:" $TTLSelectionInt -ForegroundColor Green
    $Prompt = "Are you sure you want to set [WHITE]DefaultTTL[/] to $TTLSelectionInt`?"
    $Success = "`nSetting [WHITE]DefaultTTL[/] to [white]$TTLSelectionInt[/] now.`n"
    $Result = Invoke-SpectreTextPromptConfirm -Prompt $Prompt -ConfirmSuccess $Success
    if($Result -ne "Y"){
        exit
    }

    gsudo { netsh int ipv4 set glob defaultcurhoplimit=$args[0] >NUL } -args $TTLSelectionInt
	gsudo { netsh int ipv6 set glob defaultcurhoplimit=$args[0] >NUL } -args $TTLSelectionInt
    gsudo { reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters" /v DisabledComponents /t REG_DWORD /d 32 /f }

}

Invoke-SpectrePrintMarkup -Message "[white]DefaultTTL[/] has been set to [white]$TTLSelectionInt[/]`n"
read-host "You may close this window now or press any key to exit."

When I run the code, all works fine until the gsudo lines:

gsudo { netsh int ipv4 set glob defaultcurhoplimit=$args[0] >NUL } -args $TTLSelectionInt
gsudo { netsh int ipv6 set glob defaultcurhoplimit=$args[0] >NUL } -args $TTLSelectionInt
gsudo { reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters" /v DisabledComponents /t REG_DWORD /d 32 /f }

Once these lines are processed, the console spits out three identical lines:

Code_m7HKJW4f6P

Context:

  • Windows version: Windows 10 Pro x64 Version 22H2 (Build 19045.3448)
  • gsudo version: gsudo v1.3.0 (Branch.master.Sha.24fb735f547e1e5dd7aa22fdd77777fa8c923a1c)

One thought I had is that gsudo.exe and gsudo are in a separate folder (in my PATH) than the actual module files. Could this cause trouble?

Any help at all would be greatly appreciated!

@gerardog
Copy link
Owner

gerardog commented Oct 1, 2023

Hi @fmotion1!

One thought I had is that gsudo.exe and gsudo are in a separate folder (in my PATH) than the actual module files. Could this cause trouble?

Rarely, I guess.. You are calling gsudo, so please run Get-Command gsudo and verify it is returning either gsudo.exe or gsudo function from gsudoModule. If the first or only results is the C:\Program Files\gsudo\Current\gsudo (no extension) bash script, then we may have a problem there.

If that were the problem, calling gsudo.exe instead of gsudo might fix the error, or doing a regular install. Anyway, If this was the case, I wouldn't expect this error message.

  • I am not able to trigger that -NonInteractive argument, so help me identify why your code is running non-interactively. How are you invoking the.ps1 file? What pwsh.exe or pwsh console host are you using? Specific Pwsh verision? Does Pwsh any experimental feature enabled?
    For example, if this script is invoked from, let's say, scoop, that may help me reproduce the problem.

Could you please do gsudo --debug { command } and paste the output? let's see where the -NonInteractive is placed.

Unrelated: the $args[0] needs a fix. You can pass an argument with $args[0], but if you prepend it with 'defaultcurhoplimit=' then pwsh will treat it as a double quoted string and treat [0] as a string literal

image

@futuremotiondev
Copy link
Author

gsudo binaries are in C:\BIN\gsudo which is in my PATH.

I'm invoking the script from within VSCode as well as in the Pwsh7 console. Errors remain the same.

PowerShell Version: 7.3.7
 PSEdition: Core

I am not able to trigger that -NonInteractive argument, so help me identify why your code is running non-interactively.

It shouldn't (isn't) running non-interactively at all. The script prompts the user several times for input and depends on that input to function. I don't have a -noninteractive flag set anywhere.

This is interesting though:

Code_JdfLFTVsZP

Even without any code in the scriptblock it's still erroring out:

Debug: Command Line: -noninteractive --debug -encodedCommand IAA= -inputFormat xml -outputFormat xml
Error: Invalid option: o in -noninteractive

If I run my script, I get the following:

Code_VkgJL63CXJ

Debug: Command Line: -noninteractive --debug -encodedCommand IABuAGUAdABzAGgAIABpAG4AdAAgAGkAcAB2ADQAIABzAGUAdAAgAGcAbABvAGIAIABkAGUAZgBhAHUAbAB0AGMAdQByAGgAbwBwAGwAaQBtAGkAdAA9ACQAKAAkAGEAcgBnAHMAWwAwAF0AKQAgAD4ATgBVAEwAIAA= -inputFormat xml -outputFormat xml
Error: Invalid option: o in -noninteractive
Debug: Command Line: -noninteractive --debug -encodedCommand IABuAGUAdABzAGgAIABpAG4AdAAgAGkAcAB2ADYAIABzAGUAdAAgAGcAbABvAGIAIABkAGUAZgBhAHUAbAB0AGMAdQByAGgAbwBwAGwAaQBtAGkAdAA9ACQAKAAkAGEAcgBnAHMAWwAwAF0AKQAgAD4ATgBVAEwAIAA= -inputFormat xml -outputFormat xml
Error: Invalid option: o in -noninteractive
Debug: Command Line: -noninteractive --debug -encodedCommand IAByAGUAZwAgAGEAZABkACAAIgBIAEsARQBZAF8ATABPAEMAQQBMAF8ATQBBAEMASABJAE4ARQBcAFMAWQBTAFQARQBNAFwAQwB1AHIAcgBlAG4AdABDAG8AbgB0AHIAbwBsAFMAZQB0AFwAUwBlAHIAdgBpAGMAZQBzAFwAVABjAHAAaQBwADYAXABQAGEAcgBhAG0AZQB0AGUAcgBzACIAIAAvAHYAIABEAGkAcwBhAGIAbABlAGQAQwBvAG0AcABvAG4AZQBuAHQAcwAgAC8AdAAgAFIARQBHAF8ARABXAE8AUgBEACAALwBkACAAMwAyACAALwBmACAA -inputFormat xml -outputFormat xml
Error: Invalid option: o in -noninteractive

If this helps, here's the code:

Clear-Host

$PingResult = & ping 127.0.0.1
$PingResult[2] -match 'TTL=([\d]+)'
$CurrentTTL = $Matches[1]

Invoke-SpectrePrintMarkup -Message "--------------------------------------------------------`n"
Invoke-SpectrePrintMarkup -Message "[grey66]Current TTL Value is[/] [white]$CurrentTTL[/]`n"
Invoke-SpectrePrintMarkup -Message "--------------------------------------------------------`n"

Invoke-SpectrePrintMarkup -Message "[grey66]Windows 10 Default value for DefaultTTL is[/] [white]128[/]"
Invoke-SpectrePrintMarkup -Message "[grey66]Reccommended tweaked values are in the range of [/][white]55-70[/]`n"
[System.String]$TTLSelection = Invoke-SpectreTextPrompt -Prompt "[#00d787]Specify your desired DefaultTTL:[/]"

$TTLSelectionInt = $TTLSelection -as [int]
while(($TTLSelectionInt -lt 55) -or ($TTLSelectionInt -gt 128)){
    Write-Error "TTL Value is outside of the reccommended range. Please enter a different value.`n"
    [System.String]$TTLSelection = Invoke-SpectreTextPrompt -Prompt "[#00d787]Specify your desired DefaultTTL:[/]"
    $TTLSelectionInt = $TTLSelection -as [int]
}

if($TTLSelectionInt -eq 128){
    $Prompt = "Are you sure you want to reset [WHITE]DefaultTTL[/] to default? ($TTLSelectionInt)"
    $Success = "`nSetting [white]DefaultTTL[/] to [white]$TTLSelectionInt[/] now.`n"
    $Result = Invoke-SpectreTextPromptConfirm -Prompt $Prompt -ConfirmSuccess $Success
    if($Result -ne "Y"){
        exit
    }


    gsudo --debug { netsh int ipv4 set glob defaultcurhoplimit=128 >NUL }
    gsudo --debug { netsh int ipv6 set glob defaultcurhoplimit=128 >NUL }
    gsudo --debug { reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters" /v DisabledComponents /f }

} else {

    $Prompt = "Are you sure you want to set [WHITE]DefaultTTL[/] to $TTLSelectionInt`?"
    $Success = "`nSetting [WHITE]DefaultTTL[/] to [white]$TTLSelectionInt[/] now.`n"
    $Result = Invoke-SpectreTextPromptConfirm -Prompt $Prompt -ConfirmSuccess $Success
    if($Result -ne "Y"){
        exit
    }

    gsudo --debug { netsh int ipv4 set glob defaultcurhoplimit=$($args[0]) >NUL } -args $TTLSelectionInt
    gsudo --debug { netsh int ipv6 set glob defaultcurhoplimit=$($args[0]) >NUL } -args $TTLSelectionInt
    gsudo --debug { reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters" /v DisabledComponents /t REG_DWORD /d 32 /f }

}

Invoke-SpectrePrintMarkup -Message "`n[white]DefaultTTL[/] has been set to [white]$TTLSelectionInt[/]`n"
read-host "You may close this window now or press any key to exit."

Even if I just do this: gsudo --debug { Get-Process }, I still get:

Debug: Command Line: -noninteractive --debug -encodedCommand IABHAGUAdAAtAFAAcgBvAGMAZQBzAHMAIAAgAA== -inputFormat xml -outputFormat xml
Error: Invalid option: o in -noninteractive

I have no idea what's going on. In the meantime, I've switched to another elevation technique. Hopefully this gets sorted because I really love the idea of gsudo!

Appreciate any help. Let me know if there is any more information I can give you.

@futuremotiondev
Copy link
Author

OK! I've made some progress!

I've successfully got it to work outside VSCode if I run the script directly. Furthermore, I also replaced all gsudo references with gsudo.exe since Get-Command gsudo was pointing to the non-exe binary.

But running within VSCode still produces the error. I know positively that VSCode does Automatic script injection. So I was thinking the -noninteractive argument was originating from that injected code.

If I run code --locate-shell-integration-path pwsh it prints the injection script path:

c:\Users\futur\AppData\Local\Programs\Microsoft VS Code\resources\app\out\vs\workbench\contrib\terminal\browser\media\shellIntegration.ps1

But I checked that script and there is no -noninteractive flag set anywhere in the file. So that's frustrating. But at least I've narrowed the problem down to VSCode.

Is there any way you can test/troubleshoot this on your end?

I've attached the shell integration script to this post.

shellIntegration.zip

@futuremotiondev
Copy link
Author

Just saw the commit... Awesome! I hope it makes it into release soon. Just some further info - I can actually get the script to work in VSCode if I run the script "In a new terminal" (It's a feature in the PowerShell Pro Tools VSCode extension).

Image for reference:

UduqnqBFfA

This is definitely leading me to believe that the integrated terminal is spawning with the -noninteractive flag set. I just don't know where it's coming from.

Either way, I hope the latest commit works!

@gerardog
Copy link
Owner

gerardog commented Oct 1, 2023

The parameter binding on native calls from PowerShell have some special rules, like: if a scriptblock appears, use a special powershell to powershell parameter binding, which encodes the scriptblock... and also the -noninteractive is added by powershell itself, when it thinks it's invoked from a non-interactive host.
I can understand the 'integrated terminal' would trigger this issue, since clearly is a non-interactive host, but I still don't understand why do you get that effect in the regular terminal as you shown 👇
image

Do you still get the error in the interactive terminal ? can you try pwsh -noprofile then gsudo to see if that makes a difference?

Here is a potential fix for the issue: https://github.com/gerardog/gsudo/releases/tag/v2.4.1-fixnoninteractive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants