Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: gerardog/gsudo
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.5.1
Choose a base ref
...
head repository: gerardog/gsudo
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
24 changes: 11 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -28,15 +28,14 @@ jobs:
permissions:
id-token: write
contents: read
checks: write
checks: write
steps:
- uses: actions/setup-dotnet@v2
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '7.0.x'
include-prerelease: true
dotnet-version: '9.0.x'
- uses: actions/checkout@v2
with:
fetch-depth: 0
with:
fetch-depth: 0
- name: Run Tests
id: tests
run: ./build/02-test.ps1
@@ -45,7 +44,7 @@ jobs:
if: success() || failure()
with:
name: TestsResults (dotnet)
path: "**/TestResults*.trx"
path: "**/TestResults*.trx"
reporter: dotnet-trx
fail-on-error: true
- name: Test Report PowerShell v5
@@ -68,12 +67,11 @@ jobs:
name: Build
runs-on: windows-latest
steps:
- uses: actions/setup-dotnet@v2
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'
- uses: actions/checkout@v2
with:
dotnet-version: '7.0.x'
include-prerelease: true
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install dependencies
run: choco install GitVersion.Portable --version 5.6.11 --confirm --no-progress; choco install il-repack --confirm --no-progress
@@ -93,7 +91,7 @@ jobs:
- name: Build
run: ./build/01-build.ps1
- name: Upload build artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Binaries
path: ./artifacts
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ jobs:
- name: Setup Pages
uses: actions/configure-pages@v2
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
uses: actions/upload-pages-artifact@v3
with:
# Upload entire repository
path: './docs/build'
23 changes: 15 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -30,12 +30,16 @@ jobs:
permissions:
contents: write
steps:
- uses: actions/setup-dotnet@v2
with:
dotnet-version: '7.0.x'
include-prerelease: true
- name: Install dependencies
run: choco install GitVersion.Portable --version 5.6.11 --confirm --no-progress; choco install NuGet.CommandLine GitVersion.Portable wixtoolset --confirm --no-progress
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: Binaries
path: ./artifacts
@@ -48,19 +52,21 @@ jobs:
- name: Code Sign
run: ./build/03-sign.ps1
- name: Upload build artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Binaries
path: ./artifacts
path: ./artifacts
overwrite: true
- name: Build Installer
run: ./build/04-build-installer.ps1
- name: Remove the pfx
run: Remove-Item -path $env:cert_path
- name: Upload installer artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Installer
path: ./artifacts/*.msi
overwrite: true
- name: Create Release
uses: ncipollo/release-action@v1.12.0
with:
@@ -124,7 +130,7 @@ jobs:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: Binaries
path: ./artifacts
@@ -133,12 +139,13 @@ jobs:
- name: Build Package for Chocolatey & Upload
run: ./build/05-release-Chocolatey.ps1
- name: Upload build artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: Binaries
path: ./artifacts
overwrite: true
- name: WinGet Releaser
uses: vedantmgoyal2009/winget-releaser@v2
uses: vedantmgoyal9/winget-releaser@main
if: ${{ env.is_prerelease == 0 }}
with:
token: ${{ secrets.WINGET_RELEASER_TOKEN }}
@@ -161,7 +168,7 @@ jobs:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4
with:
name: Binaries
path: ./artifacts
4 changes: 2 additions & 2 deletions .github/workflows/winget-solo.yml
Original file line number Diff line number Diff line change
@@ -14,8 +14,8 @@ jobs:
name: release-chocolatey
steps:
- name: WinGet Releaser
uses: vedantmgoyal2009/winget-releaser@v2
uses: vedantmgoyal9/winget-releaser@main
with:
token: ${{ secrets.WINGET_RELEASER_TOKEN }}
identifier: gerardog.gsudo
installers-regex: '\.msi$' # Only .msi files
installers-regex: '\.msi$' # Only .msi files
13 changes: 0 additions & 13 deletions GitVersion.yml

This file was deleted.

55 changes: 30 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -7,12 +7,12 @@

**gsudo** is a `sudo` equivalent for Windows, with a similar user-experience as the original Unix/Linux sudo.
Allows you to run a command (or re-launch your current shell) with elevated permissions, in the current console window or a new one.

Just [prepend `gsudo`](#usage) (or the `sudo` alias) to your command and it will run elevated. For PowerShell use this [syntax](#usage-from-powershell--powershell-core): `gsudo { ScriptBlock }`

One UAC popup will appear each time. You can see less popups if you enable [gsudo cache](#credentials-cache).

It detects your current shell and elevates your command as native shell commands. (Supports `Cmd`, `PowerShell`, `WSL`, `git-bash`, `MinGW`, `Cygwin`, `Yori`, `Take Command`, `BusyBox` & `NuShell`.)
It detects your current shell and elevates your command as native shell commands. (Supports `Cmd`, `PowerShell`, `WSL`, `git-bash`, `MinGW`, `Cygwin`, `Yori`, `Take Command`, `BusyBox` & `NuShell`.)

## Table of contents

@@ -52,23 +52,24 @@ It detects your current shell and elevates your command as native shell commands
- Supports being used on scripts:
- Outputs StdOut/StdErr can be piped or captured (e.g. `gsudo dir | findstr /c:"bytes free" > FreeSpace.txt`) and exit codes too (`%errorlevel%`). If `gsudo` fails to elevate, the exit code will be 999.
- If `gsudo` is invoked from an already elevated console, it will just run the command (it won't fail). So, you don't have to worry if you run `gsudo` or a script that uses `gsudo` from an already elevated console. (The UAC popup will not appear, as no elevation is required)

- `gsudo !!` elevates the last executed command. Works on CMD, Git-Bash, MinGW, Cygwin (and PowerShell with [gsudo module](#gsudomodule) only)

## Installation

- Using [Scoop](https://scoop.sh): `scoop install gsudo`
- Using [WinGet](https://github.com/microsoft/winget-cli/releases): `winget install gerardog.gsudo`
- Using [Chocolatey](https://chocolatey.org/install): `choco install gsudo`
- Manual Setup: download and run the proper `MSI` installer for you system from the [latest release](https://github.com/gerardog/gsudo/releases/latest).
- Running this line: [(script)](installgsudo.ps1):
``` PowerShell
PowerShell -Command "Set-ExecutionPolicy RemoteSigned -scope Process; [Net.ServicePointManager]::SecurityProtocol = 'Tls12'; iwr -useb https://raw.githubusercontent.com/gerardog/gsudo/master/installgsudo.ps1 | iex"
```
- Portable:
1. Download `gsudo.portable.zip` from the [latest release](https://github.com/gerardog/gsudo/releases/latest)
1. Download `gsudo.portable.zip` from the [latest release](https://github.com/gerardog/gsudo/releases/latest).
2. Extract the folder corresponding to your processor architecture (x64, x86, or arm64) from the zip file to a suitable directory on your computer.
3. Optionally, add that directory to your system's PATH environment variable if it's not already accessible.

**Please restart all your console windows** after installing to ensure that the `PATH` environment variable is refreshed.

Note: `gsudo.exe` is portable. No windows service is required or system change is done, except adding it to the Path.
@@ -149,10 +150,10 @@ To elevate a commands or script block: **Wrap it in {curly braces}**.
# Pass arguments with -args
$MyString = "Hello World"
gsudo { Write-Output $args[0] } -args $MyString
gsudo { Write-Output $args[0] } -args $MyString
# Output is serialized as PSObjects with properties.
$services = gsudo { Get-Service 'WSearch', 'Winmgmt'}
$services = gsudo { Get-Service 'WSearch', 'Winmgmt'}
Write-Output $services.DisplayName
# Inputs too: Example elevated iteration of a list.
@@ -167,18 +168,18 @@ Alternative syntaxes:
# Pass values (not variables by reference) by prefixing `$using:`. I.E.
$MyString = "Hello World"
Invoke-Gsudo { Write-Output $using:MyString }
Invoke-Gsudo { Write-Output $using:MyString }
# Syntax:
Invoke-Gsudo [-ScriptBlock] <ScriptBlock>
[[-ArgumentList] <Object[]>]
[-InputObject <PSObject>]
[-LoadProfile | -NoProfile]
Invoke-Gsudo [-ScriptBlock] <ScriptBlock>
[[-ArgumentList] <Object[]>]
[-InputObject <PSObject>]
[-LoadProfile | -NoProfile]
[-Credential <PSCredential>]
```

- PowerShell function.
- Performs auto serialization of inputs & outputs.
- Performs auto serialization of inputs & outputs.
- You can prefix variables with the `$using:` scope modifier (like `$using:variableName`) and their serialized value is applied.
- Use `-LoadProfile` or `-NoProfile` to override profile loading or not.
- Use `-Credential` option for Run As User (same as `-u` but for `Get-Credentials`).
@@ -187,16 +188,16 @@ Alternative syntaxes:

<details>
<summary>3. Manual string interpolation. (not recommended) </summary>
I don't recommend this approach as it is really hard to do proper escape all special characters.
I don't recommend this approach as it is really hard to do proper escape all special characters.

``` PowerShell
Usage: gsudo 'string literal'
# Variable substitutions example:
$file='C:\My Secret.txt';
$file='C:\My Secret.txt';
$algorithm='md5';
$hash = gsudo "(Get-FileHash '$file' -Algorithm $algorithm).Hash"
# or
# or
$hash = gsudo "(Get-FileHash ""$file"" -Algorithm $algorithm).Hash"
```

@@ -209,13 +210,13 @@ Alternative syntaxes:
- <a name="gsudomodule"></a> Optional: Import module `gsudoModule.psd1` into your PowerShell Profile:
- Adds syntax auto-complete to gsudo in PowerShell. Plus, it suggests the 3 previous commands, making your workflow smoother than ever!
- Enables `gsudo !!` on Powershell, to elevate the last executed command.
- Adds Functions:
- Adds Functions:
- `Test-IsGsudoCacheAvailable` Returns true if a gsudo cache is active (meaning elevation with no UAC is possible).
- `Test-IsProcessElevated`: Returns true if the current process is elevated.
- `Test-IsAdminMember`: Returns true if the current user is member of the `Local Administrators` groups, which means it can elevate with.

``` Powershell
# Add the following line to your $PROFILE
# Add the following line to your $PROFILE
Import-Module "gsudoModule"
# Or run:
@@ -224,12 +225,12 @@ Alternative syntaxes:

- If you haven't already customized your PowerShell prompt (for example by installing Oh-My-Posh), you can easily add a red `#` indicating that the current process is elevated:
![gsudo prompt](docs/static/img/gsudo-powershell-prompt.gif)

To do so add this line to your profile (after importing `gsudoModule`):

``` powershell
Set-Alias Prompt gsudoPrompt
```
```
---

### Usage from WSL (Windows Subsystem for Linux)
@@ -240,7 +241,7 @@ On WSL bash, prepend `gsudo` to elevate **WSL commands** or `gsudo -d` for **CMD

``` bash
# elevate default shell
PC:~$ gsudo
PC:~$ gsudo

# run elevated WSL command
PC:~$ gsudo mkdir /mnt/c/Windows/MyFolder
@@ -304,9 +305,13 @@ In any case, you can stop all cache sessions with `gsudo -k`.

When I created `gsudo`, there were other `sudo` packages on most Windows popular package managers such as `Chocolatey` and `Scoop`, so I had no other choice to pick another name. `gsudo` installers create an alias for `sudo`, so feel free to use `sudo` on your command line to invoke `gsudo`.

- Why did you migrated from `.Net Framework 4.6` to `.Net Core 7.0`?
- Which dotnet version is built with?

Versions prior to v1.3.0 used .NET 4.6 because it was included in every Windows 10/11 installation.

From v1.4.0 up to v2.5.1, gsudo has been built using .net7.0 NativeAOT. This approach offers faster load times, reduced memory usage, and allows the tool to run on machines without any .NET runtime installed.

Starting from v1.4.0, it is built using `.Net 7.0` NativeAOT. It loads faster and uses less memory, and runs on machines without any .Net runtime installed. Prior versions `<v1.3.0` used .Net 4.6, because it was included in every Windows 10/11 installation.
Starting with v2.6.0, we've migrated to net9.0 due to the end-of-life of net7.0.

- Is `gsudo` a port of `*nix sudo`?

4 changes: 2 additions & 2 deletions build/00-prerequisites.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
choco install il-repack
choco install il-repack
choco install GitVersion.Portable
choco install wixtoolset
choco install hub
choco install dotnet-7.0-sdk
choco install dotnet-9.0-sdk
choco install NuGet.CommandLine
28 changes: 14 additions & 14 deletions build/01-build.ps1
Original file line number Diff line number Diff line change
@@ -12,24 +12,24 @@ if ($env:version) {
"- Using version number v$version / v$version_MajorMinorPatch"

"-- Cleaning bin & obj folders"
Get-Item ".\src\gsudo\bin\", ".\src\gsudo\obj\" -ErrorAction Ignore | Remove-Item -Recurse -Force
"-- Building net7.0 win-arm64"
dotnet publish .\src\gsudo\gsudo.csproj -c Release -o .\artifacts\arm64 -f net7.0 -r win-arm64 --sc -p:PublishAot=true -p:IlcOptimizationPreference=Size -v minimal -p:WarningLevel=0 || $(exit $LASTEXITCODE)
"-- Building net7.0 win-x64"
dotnet publish .\src\gsudo\gsudo.csproj -c Release -o .\artifacts\x64 -f net7.0 -r win-x64 --sc -p:PublishAot=true -p:IlcOptimizationPreference=Size -v minimal -p:WarningLevel=0 || $(exit $LASTEXITCODE)
"-- Building net7.0 win-x86"
dotnet publish .\src\gsudo\gsudo.csproj -c Release -o .\artifacts\x86 -f net7.0 -r win-x86 --sc -p:PublishReadyToRun=true -p:PublishSingleFile=true -v minimal -p:WarningLevel=0 || $(exit $LASTEXITCODE)
Get-Item ".\src\gsudo\bin\", ".\src\gsudo\obj\" -ErrorAction Ignore | Remove-Item -Recurse -Force
"-- Building net9.0 win-arm64"
dotnet publish .\src\gsudo\gsudo.csproj -c Release -o .\artifacts\arm64 -f net9.0 -r win-arm64 || $(exit $LASTEXITCODE)
"-- Building net9.0 win-x64"
dotnet publish .\src\gsudo\gsudo.csproj -c Release -o .\artifacts\x64 -f net9.0 -r win-x64 || $(exit $LASTEXITCODE)
"-- Building net9.0 win-x86"
dotnet publish .\src\gsudo\gsudo.csproj -c Release -o .\artifacts\x86 -f net9.0 -r win-x86 || $(exit $LASTEXITCODE)
"-- Building net4.6 AnyCpu"
dotnet publish .\src\gsudo\gsudo.csproj -c Release -o .\artifacts\net46-AnyCpu\unmerged -f net46 -p:Platform=AnyCpu -v minimal -p:WarningLevel=0 || $(exit $LASTEXITCODE)
dotnet publish .\src\gsudo\gsudo.csproj -c Release -o .\artifacts\net46-AnyCpu\unmerged -f net46 -p:Platform=AnyCpu -p:WarningLevel=0 || $(exit $LASTEXITCODE)

"-- Repacking net4.6 AnyCpu into a single EXE"

ilrepack .\artifacts\net46-AnyCpu\unmerged\gsudo.exe .\artifacts\net46-AnyCpu\unmerged\*.dll /out:.\artifacts\net46-AnyCpu\gsudo.exe /target:exe /targetplatform:v4 /ndebug /wildcards || $(exit $LASTEXITCODE)

if ($?) {
rm artifacts\net46-AnyCpu\unmerged -Recurse
echo "artifacts\net46-AnyCpu\unmerged -> ilmerge -> artifacts\net46-AnyCpu\"
}
ilrepack .\artifacts\net46-AnyCpu\unmerged\gsudo.exe .\artifacts\net46-AnyCpu\unmerged\*.dll /out:.\artifacts\net46-AnyCpu\gsudo.exe /target:exe /targetplatform:v4 /ndebug /wildcards || $(exit $LASTEXITCODE)

if ($?) {
rm artifacts\net46-AnyCpu\unmerged -Recurse
echo "artifacts\net46-AnyCpu\unmerged -> ilmerge -> artifacts\net46-AnyCpu\"
}

cp .\src\gsudo.Wrappers\* .\artifacts\x86
cp .\src\gsudo.Wrappers\* .\artifacts\x64
Loading