From 5a31bc4a46158230203d68bec1e5768bc644bbb1 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 13 Jul 2020 20:59:59 +0000 Subject: [PATCH] sudo for Windows 10 --- sudo/README.md | 33 +++++++++++++++++++++++++++++++++ sudo/install.ps1 | 12 ++++++++++++ sudo/install.sh | 14 ++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 sudo/README.md create mode 100644 sudo/install.ps1 create mode 100644 sudo/install.sh diff --git a/sudo/README.md b/sudo/README.md new file mode 100644 index 0000000..c32661f --- /dev/null +++ b/sudo/README.md @@ -0,0 +1,33 @@ +--- +title: Sudo for Windows +homepage: https://stackoverflow.com/a/40321310/151312 +tagline: | + Sudo for Windows gives you a minimal `sudo` that works in cmd.exe and PowerShell. +--- + +## Cheat Sheet + +> Sudo for Windows isn't real `sudo`, but it's close enough for certain tasks - +> like installing WSL (the Windows Subsystem for Linux), without opening a GUI +> to Alt-Click "Run as Administrator". + +### Example: Enabling WSL + +```bash +sudo.cmd dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart +``` + +### Raw PowerShell + +`sudo.cmd` is simply an alias of a powershell elevation command: + +```pwsh +@echo off +powershell -Command "Start-Process cmd -Verb RunAs -ArgumentList '/c cd /d %CD% && %*'" +@echo on +``` + +Note: replace `/c` with `/k` if you'd like the window to stay open rather than +closing automatically. + +Source: diff --git a/sudo/install.ps1 b/sudo/install.ps1 new file mode 100644 index 0000000..6f0b54d --- /dev/null +++ b/sudo/install.ps1 @@ -0,0 +1,12 @@ +#!/usr/bin/env pwsh + +echo "Installing sudo.cmd..." + +# Couldn't figure out how to get this to work with "here strings", so forgive the ugly, but at least it works +Set-Content -Path .local\bin\sudo.cmd -Value "@echo off`r`npowershell -Command ""Start-Process cmd -Verb RunAs -ArgumentList '/c cd /d %CD% && %*'""`r`n@echo on" + +# Add to path +& "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/bin + +echo "Installed to '$Env:USERPROFILE\.local\bin\sudo.cmd'" +echo "" diff --git a/sudo/install.sh b/sudo/install.sh new file mode 100644 index 0000000..6b37060 --- /dev/null +++ b/sudo/install.sh @@ -0,0 +1,14 @@ +set -e +set -u + +{ + + if [ -z "$(command -v sudo)" ]; then + >&2 echo "Error: on Linux and BSD you should install sudo via the native package manager" + >&2 echo " for example: apt install -y sudo" + exit 1 + else + echo "'sudo' already installed" + fi + +} -- 2.25.1