Skip to content

Instantly share code, notes, and snippets.

@R0rt1z2
Last active November 19, 2022 10:08
Show Gist options
  • Save R0rt1z2/61ad508286bdc0e2f5103fa0287ae552 to your computer and use it in GitHub Desktop.
Save R0rt1z2/61ad508286bdc0e2f5103fa0287ae552 to your computer and use it in GitHub Desktop.
#
# PowerShell displays error messages regardless of whether you redirect it whatever you're executing to null.
# To prevent this from happening, disable all the output(s) by using PowerShell's default variables.
#
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'SilentlyContinue'
#
# There is no built-in function to update the (global) Windows env %PATH%. Instead, use this function to update
# the registry directly (https://stackoverflow.com/a/69239861).
#
function Add-Path {
param(
[Parameter(Mandatory, Position=0)]
[string] $LiteralPath,
[ValidateSet('User', 'CurrentUser', 'Machine', 'LocalMachine')]
[string] $Scope
)
Set-StrictMode -Version 1; $ErrorActionPreference = 'Stop'
$isMachineLevel = $Scope -in 'Machine', 'LocalMachine'
if ($isMachineLevel -and -not $($ErrorActionPreference = 'Continue'; net session 2>$null)) { throw "You must run AS ADMIN to update the machine-level Path environment variable." }
$regPath = 'registry::' + ('HKEY_CURRENT_USER\Environment', 'HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment')[$isMachineLevel]
$currDirs = (Get-Item -LiteralPath $regPath).GetValue('Path', '', 'DoNotExpandEnvironmentNames') -split ';' -ne ''
if ($LiteralPath -in $currDirs) {
return
}
$newValue = ($currDirs + $LiteralPath) -join ';'
Set-ItemProperty -Type ExpandString -LiteralPath $regPath Path $newValue
$dummyName = [guid]::NewGuid().ToString()
[Environment]::SetEnvironmentVariable($dummyName, 'foo', 'User')
[Environment]::SetEnvironmentVariable($dummyName, [NullString]::value, 'User')
$env:Path = ($env:Path -replace ';$') + ';' + $LiteralPath
}
#
# Small logger. Includes color-like and symbol-like verbosity.
# @param $verbosity: Logging verbosity (info, fail, success)
# @param $logmessage: Message to print to the console.
#
function LogToConsole {
Param
(
[Parameter(Mandatory = $true)] [int] $verbosity,
[Parameter(Mandatory = $true)] [string] $logmessage
)
Switch ($verbosity) {
1 {
Write-Host "[?] $logmessage" -ForegroundColor White
}
2 {
Write-Host "[-] $logmessage" -ForegroundColor Red
}
3 {
Write-Host "[+] $logmessage" -ForegroundColor Green
}
4 {
Write-Host "[!] $logmessage" -ForegroundColor Yellow
}
default {
Write-Host "[~] $logmessage" -ForegroundColor Gray
}
}
}
#
# We need special privilegies to install the script globally, if we don't have them, bail out.
#
$per = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-Not $per.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
LogToConsole 2 "You need privilegies to execute this script!"
} else {
#
# For some reason, Windows 10 (and 11) include different aliases that are used with priority when one
# of the defined keywords is detected. These include python and python3. By default, these aliases cause
# the Windows Store to open regardless of whether Python is installed or not. Since we can't really get
# rid of the aliasses, delete the python(3) Store shortcuts so it uses the real python executables.
#
Remove-Item $env:LOCALAPPDATA\Microsoft\WindowsApps\python.exe
Remove-Item $env:LOCALAPPDATA\Microsoft\WindowsApps\python3.exe
if ([string]::IsNullOrEmpty((Get-Command python.exe).Path)) {
LogToConsole 4 "Python is not installed!"
LogToConsole 1 "Downloading python..."
Invoke-WebRequest -Uri https://www.python.org/ftp/python/3.11.0/python-3.11.0-amd64.exe -OutFile PythonSetup.exe
if (-Not (Test-Path -Path PythonSetup.exe -PathType Leaf)) {
LogToConsole 2 "Unable to download python!"
exit 1
}
LogToConsole 1 "Installing python..."
Start-Process -Wait -FilePath "PythonSetup.exe" -ArgumentList "InstallAllUsers=1 PrependPath=1 /quiet /log log.txt"
if (-Not ($env:Path.Contains("Python"))) {
if (-Not [string]::IsNullOrEmpty((Resolve-Path 'C:\Program Files\Python*').Path)) {
LogToConsole 4 "Adding python to the PATH..."
Add-Path $(Resolve-Path 'C:\Program Files\Python*').Path
}
}
if ([string]::IsNullOrEmpty((Get-Command python.exe).Path)) {
LogToConsole 2 "Unable to install python (check log.txt for more details), please install manually!"
exit 1
}
LogToConsole 3 "Successfully installed python!"
} else {
LogToConsole 3 "Found $(python.exe --version) installed"
}
if ([string]::IsNullOrEmpty((Get-Command pip.exe).Path)) {
LogToConsole 4 "Pip is not installed!"
LogToConsole 1 "Downloading pip..."
Invoke-WebRequest -Uri https://bootstrap.pypa.io/get-pip.py. -OutFile get-pip.py
if (!Test-Path -Path get-pip.py -PathType Leaf) {
LogToConsole 2 "Unable to download pip!"
exit 1
}
LogToConsole 1 "Installing pip..."
Start-Process -Wait -FilePath "python.exe" -ArgumentList "get-pip.py"
if (-Not ($env:Path.Contains("Scripts"))) {
if (-Not [string]::IsNullOrEmpty((Resolve-Path 'C:\Program Files\Python*\Scripts').Path)) {
LogToConsole 4 "Adding scripts to the PATH..."
Add-Path $(Resolve-Path 'C:\Program Files\Python*\Scripts').Path
}
}
if ([string]::IsNullOrEmpty((Get-Command pip.exe).Path)) {
LogToConsole 2 "Unable to install pip, please install manually!"
exit 1
}
LogToConsole 3 "Successfully installed pip!"
} else {
LogToConsole 3 "Found $(pip.exe --version)installed"
}
#
# I'd love to rely on the built-in setup.py method, but it doesn't
# seem to work at all under Windows. Instead, copy the files manually.
#
cd 'C:\Program Files\Python*\Scripts\'
LogToConsole 4 "Downloading realme-ota..."
Invoke-WebRequest -Uri https://github.com/R0rt1z2/realme-ota/archive/refs/heads/master.zip -OutFile dist.zip
LogToConsole 4 "Extracting realme-ota..."
Expand-Archive dist.zip -DestinationPath realme-ota -Force; rm -Force dist.zip
LogToConsole4 "Copying files..."
mv -Force realme-ota\realme-ota-master\* realme-ota\; rm -Force realme-ota\realme-ota-master
#
# .bat wrapper for Windows-based systems, since we PowerShell doesn't know
# we're actually playing with python executables.
#
LogToConsole 4 "Downloading wrapper..."
Invoke-WebRequest -Uri https://gist.githubusercontent.com/R0rt1z2/9b4600b5bb11d0750bdcebea04374c63/raw/b4df9d6ff725904b2115bb8353bd1fcb6de24b5b/realme-ota.bat -Outfile realme-ota.bat
if ([string]::IsNullOrEmpty((Get-Command realme-ota.bat).Path)) {
LogToConsole 2 "Unable to install realme-ota, please install manually!"
} else {
LogToConsole 3 "Successfully installed realme-ota!"
}
}
pause; exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment