Skip to content

Instantly share code, notes, and snippets.

@SyntheticDream
Created May 18, 2023 06:24
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 SyntheticDream/aac1f788dcdeac3f8c8ef64bd15cc40b to your computer and use it in GitHub Desktop.
Save SyntheticDream/aac1f788dcdeac3f8c8ef64bd15cc40b to your computer and use it in GitHub Desktop.
param ($version)
$ErrorActionPreference = "Stop"
$prev_location = $pwd
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Start-Process PowerShell -Verb RunAs "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$prev_location'; & '$PSCommandPath' $version;`"";
exit;
}
# setup done
if ($null -eq $version) {
$minor_version = "3.11.3"
}
$major_version = $minor_version.Split('.')
$major_version = $major_version[0] + $major_version[1]
$url = "https://www.python.org/ftp/python/$minor_version/python-$minor_version-amd64.exe"
$output = "C:/tmp/python-$minor_version-amd64.exe"
$install_path = "C:\Program Files\Python$major_version\python.exe"
# variables done
if (Test-Path $install_path) {
Write-Output "python already installed at $install_path - skipping installation"
Read-Host -Prompt "Press Enter to exit"
return;
}
if (Test-Path $output) {
Write-Output "python already downloaded at $output"
}
else {
Write-Output "downloading python from $url ..."
New-Item -ItemType Directory -Force -Path C:/tmp
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $url -OutFile $output
}
Write-Output "launching installer"
& $output /passive InstallAllUsers=1 PrependPath=1 Include_test=0
Write-Output "installer launched"
# cleaning up
Set-Location $prev_location
Read-Host -Prompt "Press Enter to exit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment