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

sudo !! (bang bang) for PowerShell #44

Closed
Christoph-Wagner opened this issue Jun 12, 2020 · 15 comments
Closed

sudo !! (bang bang) for PowerShell #44

Christoph-Wagner opened this issue Jun 12, 2020 · 15 comments

Comments

@Christoph-Wagner
Copy link
Contributor

Christoph-Wagner commented Jun 12, 2020

The just released 0.7.1 doesn’t support !! for PS. At face value, this seems like it should be pretty straight forward using Get-History, but I don’t have much experience with PS and even less with using PS from C#. I would like to look into it, but was wondering if you tried and encountered any showstoppers' or problems that might make this moot or extremely complicated.

@gerardog
Copy link
Owner

gerardog commented Jun 12, 2020

It's not a trivial task.
Currently gsudo is just the gsudo.exe C# app. gsudo !! on CMD uses a Windows Console API's to get its parent CMD history. On PS, there is no global WinApi call, and the c# App can't access the invoked PS session. Calling Get-History on another PS process ('pwsh -c Get-History') will return a empty or different list.
Maybe connecting to the invoker PS session with runspaces... but it doesn't feel good to me and probably has a lot of quirks. Even if we succed to do so, the history item does not have the same meaning on another context: eg. the history would say Get-FileHash $filename but we don't know the content of $filename.
So, we should first go for another item in the list: a gsudo-wrapper function (#39). The rationale for that is described on that issue, but the idea is to make elevation syntax feel more PS native. Once that one is coded, the !! can be implemented on that cmdlet as it can Get-History easily because the cmdlet runs in the same session as the invoker.

@oising
Copy link

oising commented Jun 17, 2020

the psreadline module maintains its own history list, and this module is default on every modern version of windows. get-history is ubiquitous, but has its own issues.

@majkinetor
Copy link

majkinetor commented Oct 3, 2020

Using PSReadline or Get-History is easy to do via function.

 function gsudo!! { 
   $c = Get-Content (Get-PSReadLineOption).HistorySavePath | Select-Object -last 1 -Skip 1
   gsudo $c 
}

Add this to $PROFILE and problem solved. You can also easily make it support full syntax if you are into that.

@gerardog
Copy link
Owner

gerardog commented Aug 9, 2021

On gsudo v1.0 you may experiment editing gsudo.ps1 to:

if ($MyInvocation.Line -match "!!$")
{ 
	$c = Get-Content (Get-PSReadLineOption).HistorySavePath | Select-Object -last 1 -Skip 1
	gsudo $c
}
elseif($myinvocation.expectingInput) {
	$input | & gsudo.exe @args 
} 
else { 
	& gsudo.exe @args 
}

@AlessandroDiGioacchino
Copy link

On gsudo v1.0 you may experiment editing gsudo.ps1 to:

if ($MyInvocation.Line -match "!!$")
{ 
	$c = Get-Content (Get-PSReadLineOption).HistorySavePath | Select-Object -last 1 -Skip 1
	gsudo $c
}
elseif($myinvocation.expectingInput) {
	$input | & gsudo.exe @args 
} 
else { 
	& gsudo.exe @args 
}

What would be the path of such a file?

@mattcargile
Copy link

mattcargile commented Jan 24, 2022

On gsudo v1.0 you may experiment editing gsudo.ps1 to:

if ($MyInvocation.Line -match "!!$")
{ 
	$c = Get-Content (Get-PSReadLineOption).HistorySavePath | Select-Object -last 1 -Skip 1
	gsudo $c
}
elseif($myinvocation.expectingInput) {
	$input | & gsudo.exe @args 
} 
else { 
	& gsudo.exe @args 
}

What would be the path of such a file?

@DigiOhhh It is located here. You'd have to download or clone it manually and pull it into your $PROFILE or the like. Additionally, if you are interested, please provide insight on Issue #39 to help future development in this space of the repo. There is a script Invoke-gsudo.ps1 that is being worked on and this !! function could be incorporated in it as a module or the like.

@gerardog
Copy link
Owner

gerardog commented Apr 7, 2022

For reference, for this feature to work on PowerShell, you need to follow the instructions given in the readme: Which is: import-module gsudoModule.psd1 in your $profile:

Please run the following, and it will do it for you:

Get-Command gsudoModule.psd1 | % { Write-Output "`nImport-Module `"$($_.Source)`"" | Add-Content $PROFILE }

@gerardog gerardog changed the title !! for PowerShell? sudo !! (bang bang) for PowerShell Apr 7, 2022
@AlessandroDiGioacchino
Copy link

Doesn't work for me:
Error: Failed to find last invoked command (GetConsoleCommandHistoryLength==0)

@gerardog
Copy link
Owner

@DigiOhhh: have you imported gsudoModule as my last comment said? (plus reloading your profile)

@AlessandroDiGioacchino
Copy link

Yes, this is the content of my profile:

# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"

if (Test-Path($ChocolateyProfile)) {
  Import-Module "$ChocolateyProfile"
}


Import-Module "C:\ProgramData\chocolatey\lib\gsudo\bin\gsudoModule.psd1"

@gerardog
Copy link
Owner

Please: check you gsudo version, if not 1.3.0 do choco upgrade gsudo.
Change your $profile for the new path Import-Module 'C:\tools\gsudo\Current\gsudoModule.psd1'
Then restart your consoles, then paste the results of:

Get-Module gsudoModule | fl
Get-Command gsudo | fl

@gerardog
Copy link
Owner

Should look like:
image

@AlessandroDiGioacchino
Copy link

image

Anyways I think I got it: gsudo !! works, but sudo !! doesn't

@gerardog
Copy link
Owner

gerardog commented Apr 16, 2022

Right, I´ve updated the readme recently:

You can create a custom alias for gsudo or Invoke-gsudo, as you prefer: (add one of these lines to your $PROFILE)

  • Set-Alias 'sudo' 'gsudo' or
  • Set-Alias 'sudo' 'Invoke-gsudo'

The first one (Set-Alias 'sudo' 'gsudo') is the one you should append to your profile, at the bottom.

@AlessandroDiGioacchino
Copy link

Works good! Thank you

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

No branches or pull requests

6 participants