Skip to content

Instantly share code, notes, and snippets.

@mmun
Last active May 15, 2022 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmun/688d979e8b1543b490fda236abfc0df9 to your computer and use it in GitHub Desktop.
Save mmun/688d979e8b1543b490fda236abfc0df9 to your computer and use it in GitHub Desktop.
import base64
import subprocess
def encode_powershell_command(command: str) -> str:
return base64.b64encode(command.encode('utf-16-le')).decode('utf-8')
def run_powershell_command(command: str, as_admin: bool = False):
encoded_command = encode_powershell_command(command)
if as_admin:
encoded_wrapper_command = encode_powershell_command(f"Start-Process -Verb RunAs powershell -ArgumentList '-EncodedCommand {encoded_command}'")
subprocess.run(f"powershell -EncodedCommand {encoded_wrapper_command}")
else:
subprocess.run(f"powershell -EncodedCommand {encoded_command}")
# Example Usage
run_powershell_command('Get-NetIPInterface -InterfaceIndex 1')
run_powershell_command('Set-NetIPInterface -InterfaceIndex 1', as_admin=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment