Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
Bump to version 2.0
Browse files Browse the repository at this point in the history
Added:
•  Test that destination is writable first.
•  Compare local version to current version and only upgrade if required.
• Scirpt will now download 32 or 64 bit version of rclone automatically.
• Add ability to download Beta version.
• Improved terminal output readability.
  • Loading branch information
Moodkiller committed Nov 1, 2020
1 parent 936b74e commit 71cb24d
Showing 1 changed file with 91 additions and 74 deletions.
165 changes: 91 additions & 74 deletions rclone-updater.ps1
Original file line number Diff line number Diff line change
@@ -1,102 +1,119 @@
# rclone install/update script by Moodkiller
# Version 1.0
#
# rclone install/update script by Moodkiller
# Version 2.0

# Beta tester?? (NB do not move this block)
param(
$beta='N',
$location='C:\Windows\System32'
)

if ($beta -eq $null) {$beta = Read-Host -Prompt "Download and install Beta version (Y/N)?"}
if ($64 -eq $null) {if ([IntPtr]::Size -eq 8){$64 = $true}}

# Work out if 64 or 32 bit rclone is required
#if ([IntPtr]::Size -eq 8){$64 = $true}

# Set URL's accordinly
if(($64) -and ($beta -ieq "Y")){$url = "https://beta.rclone.org/rclone-beta-latest-windows-amd64.zip";$VerURL = "https://beta.rclone.org/version.txt"}
elseif (($64) -and (!($beta -ieq "Y"))){$url = "https://downloads.rclone.org/rclone-current-windows-amd64.zip";$VerURL = "https://downloads.rclone.org/version.txt"}
elseif ((!($64)) -and (($beta -ieq "Y"))){$url = "https://beta.rclone.org/rclone-beta-latest-windows-386.zip";$VerURL = "https://beta.rclone.org/version.txt"}
elseif ((!($64)) -and (!($beta -ieq "Y"))){$url = "https://downloads.rclone.org/rclone-current-windows-386.zip";$VerURL = "https://downloads.rclone.org/version.txt"}

# USER VARIABLES #
# param($location='C:\Windows\System32')
$PASS = 'Write-Host "[PASS] " -ForegroundColor Green -NoNewline'
$WARN = 'Write-Host "[Warning] " -ForegroundColor Yellow -NoNewline'
$FAIL = 'Write-Host "[FAIL] " -ForegroundColor Red -NoNewline'
$INFO = 'Write-Host "[INFO] " -ForegroundColor Cyan -NoNewline'

function Test-Admin {
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

# Running as Admin?
if (Test-Admin) {
Invoke-Expression $PASS;Write-Host "Running script with admin privileges"
} else {
Invoke-Expression $WARN;Write-Host "Running script without admin privileges"
}

Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
function Unzip {
param([string]$zipfile, [string]$outpath)

[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}


function Check-PowershellVersion {
$version = $PSVersionTable.PSVersion.Major
Write-Host "Checking Windows PowerShell version -- $version" -ForegroundColor Green
Invoke-Expression $PASS;Write-Host "Checking Windows PowerShell version: $version"
if ($version -le 2)
{
Write-Host "Using Windows PowerShell $version is unsupported. Upgrade your Windows PowerShell." -ForegroundColor Red
Invoke-Expression $FAIL;Write-Host "Using Windows PowerShell $version is unsupported. Please upgrade your Windows PowerShell."
Invoke-Expression $INFO;Write-Host "Go to (https://github.com/PowerShell/PowerShell/releases/latest) to get the latest release."
throw
Exit 0
}
}


function Download-rclone ($filename) {
$filename = "rclone-current-windows-amd64.zip"
Write-Host "Downloading" $filename -ForegroundColor Green
$link = "https://downloads.rclone.org/rclone-current-windows-amd64.zip"
Invoke-WebRequest -Uri $link -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox -OutFile $filename
}


function Extract-rclone () {
Write-Host "Extracting rclone" -ForegroundColor Green
Expand-Archive -Force -LiteralPath ".\rclone-current-windows-amd64.zip" -DestinationPath ".\rclone"
# Get-ChildItem "C:\Program Files\rclone\rclone-*\*.exe" | Move-Item -Destination "C:\Program Files\rclone\" -Force
Write-Host "Moving rclone.exe to C:\Windows" -ForegroundColor Green
Get-ChildItem ".\rclone\rclone-*\*.exe" | Move-Item -Destination "C:\Windows\" -Force

function Test-Write {
$random = -join ((48..57) + (65..90) + (97..122) | Get-Random -Count 25 | % {[char]$_})
if (!(test-path $location)) {Invoke-Expression $INFO;Write-Host "$location doesn't exist, creating...";New-Item -ItemType Directory -Force -Path $location | Out-Null}
try{"TESTWRITE" | Out-File -FilePath $location\$random -ErrorAction SilentlyContinue}catch{Invoke-Expression $FAIL;Write-Host $_.Exception.Message}
$wrote = (Test-Path $location\$random -ErrorAction SilentlyContinue)
if ($wrote){
Remove-Item $location\$random -ErrorAction SilentlyContinue
Invoke-Expression $PASS;Write-host "Installation directory ($location) is writtable."
}else{
if (!(Test-Admin)){Invoke-Expression $FAIL;Write-Host "($location) is unwrittable, script is not running with Admin privileges"}
#throw
exit 0
}
}


function Test-Admin
{
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
function Install-rclone {
if (!(Get-ChildItem "$location\rclone.exe" -ErrorAction SilentlyContinue)) {
Invoke-Expression $INFO;Write-Host "rclone doesn't appear to be installed in a system"'$PATH.'""
Download-rclone
} else {
Update-rclone
}
}

function Update-rclone {
Invoke-Expression $INFO;Write-Host "Checking local version vs current version..."
$version = & rclone.exe --version | Select -First 1
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $VerURL -OutFile "$env:TEMP\version.txt"
$current_version = Get-Content "$env:TEMP\version.txt" -First 1
if ($version -ieq $current_version) {
Invoke-Expression $INFO;Write-Host "You currently have the latest verstion ($version) of rclone installed. No update requried."
Exit 0
} else { Invoke-Expression $INFO;Write-Host "You have $version, updating to $current_version";Download-rclone }
}


function Read-KeyOrTimeout ($prompt, $key){
$seconds = 9
$startTime = Get-Date
$timeOut = New-TimeSpan -Seconds $seconds

Write-Host "$prompt " -ForegroundColor Green

# Basic progress bar
[Console]::CursorLeft = 0
[Console]::Write("[")
[Console]::CursorLeft = $seconds + 2
[Console]::Write("]")
[Console]::CursorLeft = 1

while (-not [System.Console]::KeyAvailable) {
$currentTime = Get-Date
Start-Sleep -s 1
Write-Host "#" -ForegroundColor Green -NoNewline
if ($currentTime -gt $startTime + $timeOut) {
Break
}
}
if ([System.Console]::KeyAvailable) {
$response = [System.Console]::ReadKey($true).Key
}
else {
$response = $key
function Download-rclone {
$filename = $URL.Substring($URL.LastIndexOf("/") + 1)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-Expression $INFO;Write-Host "Downloading ($filename)...";Invoke-WebRequest -Uri $url -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox -outfile "$env:TEMP\$filename"
# Extract-rclone
Expand-Archive -Force -LiteralPath "$env:TEMP\$filename" -DestinationPath "$env:TEMP\rclone";Invoke-Expression $INFO;Write-Host "Extracting" $filename
Get-ChildItem "$env:TEMP\rclone\rclone-*\*.exe" | Copy-Item -Destination "$location" -Force;Invoke-Expression $INFO;Write-Host "Copying" (Get-ChildItem "$env:TEMP\rclone\rclone-*\*.exe" -Name) "to $location\rclone.exe"
Remove-Item $env:TEMP\rclone -Recurse;Invoke-Expression $INFO;Write-Host "Deleting temp downloaded files..."
Remove-Item $env:TEMP\rclone*.zip
}
return $response.ToString()
}

#
# Main script entry point
#
if (Test-Admin) {
Write-Host "Running script with administrator privileges" -ForegroundColor Yellow
}
else {
Write-Host "Running script without administrator privileges" -ForegroundColor Red
}

try {
Check-PowershellVersion
# Sourceforge only support TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Download-rclone
Extract-rclone
Write-Host "Operation completed" -ForegroundColor Magenta
Test-Write
Install-rclone
Invoke-Expression $PASS;Write-Host "Operation completed!"
& rclone.exe --version -q
exit 0
}
catch [System.Exception] {
Write-Host $_.Exception.Message -ForegroundColor Red
Invoke-Expression $FAIL;Write-Host $_.Exception.Message
exit 1
}

0 comments on commit 71cb24d

Please sign in to comment.