-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstallrar.cmd
198 lines (175 loc) · 9.31 KB
/
installrar.cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<# :# DO NOT REMOVE THIS LINE
:: installrar.cmd
:: oneclickwinrar, version 0.8.0.701
:: Copyright (c) 2023, neuralpain
:: Install WinRAR
@echo off
mode 44,8
title installrar (v0.8.0.701)
:: uses PwshBatch.cmd <https://gist.github.com/neuralpain/4ca8a6c9aca4f0a1af2440f474e92d05>
setlocal EnableExtensions DisableDelayedExpansion
set ARGS=%*
if defined ARGS set ARGS=%ARGS:"=\"%
if defined ARGS set ARGS=%ARGS:'=''%
:: uses cmdUAC.cmd <https://gist.github.com/neuralpain/4bcc08065fe79e4597eb65ed707be90d>
fsutil dirty query %systemdrive% >nul
if %ERRORLEVEL% NEQ 0 (
cls & echo.
echo Please grant admin priviledges.
echo Attempting to elevate...
goto UAC_Prompt
) else ( goto :begin_script )
:UAC_Prompt
set n=%0 %*
set n=%n:"=" ^& Chr(34) ^& "%
echo Set objShell = CreateObject("Shell.Application")>"%tmp%\cmdUAC.vbs"
echo objShell.ShellExecute "cmd.exe", "/c start " ^& Chr(34) ^& "." ^& Chr(34) ^& " /d " ^& Chr(34) ^& "%CD%" ^& Chr(34) ^& " cmd /c %n%", "", "runas", ^1>>"%tmp%\cmdUAC.vbs"
cscript "%tmp%\cmdUAC.vbs" //Nologo
del "%tmp%\cmdUAC.vbs"
goto :eof
:begin_script
PowerShell -NoP -C ^"$CMD_NAME='%~n0';Invoke-Expression ('^& {' + (get-content -raw '%~f0') + '} %ARGS%')"
exit /b
# --- PS --- #>
$global:ProgressPreference = "SilentlyContinue"
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
$script_name = "installrar"
$winrar = "winrar-x\d{2}-\d{3}\w*\.exe" # catch any version for any language
$wrar = "wrar\d{3}\w*\.exe" # catch the old version of WinRAR for any language
$LATEST = 701
$Script:WINRAR_EXE = $null
$Script:FETCH_WINRAR = $false # regular WinRAR
$Script:FETCH_WRAR = $false # old 32-bit WinRAR
$Script:CUSTOM_DOWNLOAD = $false
$Script:ARCH = $null # download architecture
$Script:RARVER = $null # download version
$Script:TAGS = $null # other download types, i.e. beta, language
function New-Toast {
[CmdletBinding()] Param ([String]$AppId = "oneclickwinrar", [String]$Url, [String]$ToastTitle, [String]$ToastText, [String]$ToastText2, [string]$Attribution, [String]$ActionButtonUrl, [String]$ActionButtonText = "Open documentation", [switch]$KeepAlive, [switch]$LongerDuration)
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
$Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText04)
$RawXml = [xml] $Template.GetXml(); ($RawXml.toast.visual.binding.text | Where-Object { $_.id -eq "1" }).AppendChild($RawXml.CreateTextNode($ToastTitle)) | Out-Null; ($RawXml.toast.visual.binding.text | Where-Object { $_.id -eq "2" }).AppendChild($RawXml.CreateTextNode($ToastText)) | Out-Null; ($RawXml.toast.visual.binding.text | Where-Object { $_.id -eq "3" }).AppendChild($RawXml.CreateTextNode($ToastText2)) | Out-Null
$XmlDocument = New-Object Windows.Data.Xml.Dom.XmlDocument; $XmlDocument.LoadXml($RawXml.OuterXml)
if ($Url) { $XmlDocument.DocumentElement.SetAttribute("activationType", "protocol"); $XmlDocument.DocumentElement.SetAttribute("launch", $Url) }
if ($Attribution) { $attrElement = $XmlDocument.CreateElement("text"); $attrElement.SetAttribute("placement", "attribution"); $attrElement.InnerText = $Attribution; $bindingElement = $XmlDocument.SelectSingleNode('//toast/visual/binding'); $bindingElement.AppendChild($attrElement) | Out-Null }
if ($ActionButtonUrl) { $actionsElement = $XmlDocument.CreateElement("actions"); $actionElement = $XmlDocument.CreateElement("action"); $actionElement.SetAttribute("content", $ActionButtonText); $actionElement.SetAttribute("activationType", "protocol"); $actionElement.SetAttribute("arguments", $ActionButtonUrl); $actionsElement.AppendChild($actionElement) | Out-Null; $XmlDocument.DocumentElement.AppendChild($actionsElement) | Out-Null }
if ($KeepAlive) { $XmlDocument.DocumentElement.SetAttribute("scenario", "incomingCall") } elseif ($LongerDuration) { $XmlDocument.DocumentElement.SetAttribute("duration", "long") }
$Toast = [Windows.UI.Notifications.ToastNotification]::new($XmlDocument); $Toast.Tag = "PowerShell"; $Toast.Group = "PowerShell"
if (-not($KeepAlive -or $LongerDuration)) { $Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1) }
$Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppId); $Notifier.Show($Toast)
}
function Get-WinRARData {
$Script:CUSTOM_DOWNLOAD = $true
$_data = [regex]::matches($CMD_NAME, '[^_]+')
# `$_data[0]` is the script name
$Script:ARCH = $_data[1].Value
$Script:RARVER = $_data[2].Value
$Script:TAGS = $_data[3].Value
if ($_data[0].Value -ne $script_name) {
New-Toast -LongerDuration -ActionButtonUrl "https://github.com/neuralpain/oneclickwinrar#customization" -ToastTitle "What script is this?" -ToastText "Script name is invalid. Check the script name for any typos and try again."; exit
}
if ($Script:ARCH -ne "x64" -and $Script:ARCH -ne "x32") {
New-Toast -ToastTitle "Unable to process data" -ToastText "The WinRAR architecture is invalid." -ToastText2 "Only x64 and x32 are supported."; exit
}
if ($Script:RARVER.Length -gt 0 -and $Script:RARVER.Length -ne 3) {
New-Toast -ToastTitle "Unable to process data" -ToastText "The WinRAR version is invalid." -ToastText2 "The version number must have 3 digits."; exit
}
if ($null -eq $Script:RARVER) {
$Script:RARVER = $LATEST
}
}
function Invoke-Installer($x, $v) {
Write-Host "Installing WinRAR $v... " -NoNewLine
try {
Start-Process $x "/s" -Wait
Write-Host "Done."
}
catch {
New-Toast -ToastTitle "Installation error" -ToastText "The script has run into a problem during installation. Please restart the script."; exit
}
finally {
if ($Script:FETCH_WINRAR) { Remove-Item $Script:WINRAR_EXE }
}
}
function Get-OldInstaller {
$files = Get-ChildItem -Path $pwd | Where-Object { $_.Name -match '^wrar' }
if ($CUSTOM_DOWNLOAD) {
$download = "wrar${Script:RARVER}${Script:TAGS}.exe"
foreach ($file in $files) { if ($file -match $download) { return $file } }
} else {
foreach ($file in $files) { if ($file -match $wrar) { return $file } }
}
}
function Get-Installer {
$files = Get-ChildItem -Path $pwd | Where-Object { $_.Name -match '^winrar-x' }
if ($CUSTOM_DOWNLOAD) {
if ($Script:RARVER -lt 611 -and $Script:ARCH -eq "x32") {
# if the user wants to download an older version of 32-bit WinRAR
$Script:FETCH_WRAR = $true
Get-OldInstaller
} else {
$download = "winrar-${Script:ARCH}-${Script:RARVER}${Script:TAGS}.exe"
foreach ($file in $files) { if ($file -match $download) { return $file } }
}
} else {
foreach ($file in $files) {
if ($file -match $winrar) { return $file }
else { Get-OldInstaller }
}
}
}
function Get-WinRARExeVersion {
param($x, [Switch]$IntToDouble)
if ($IntToDouble) {
return "{0:N2}" -f ($x / 100)
} elseif ($x -match "(?<version>\d{3})") {
return "{0:N2}" -f ($matches['version'] / 100)
}
}
# --- MAIN ---
# grab the name of the script file and process any
# customization data set by the user
if ($CMD_NAME -ne $script_name) { Get-WinRARData }
# this ensures that the script does not
# unnecessarily download a new installer if one
# is available in the current directory
$Script:WINRAR_EXE = (Get-Installer)
# if there are no installers, proceed to download one
if ($null -eq $Script:WINRAR_EXE) {
Write-Host "Testing connection... " -NoNewLine
if (Test-Connection www.rarlab.com -Count 2 -Quiet) {
# a try-catch block didn't work here, so instead I'm using the
# `$Error` variable paired with `-ErrorAction SilentlyContinue`
# to suppress error messages
if ($Script:CUSTOM_DOWNLOAD) {
Write-Host -NoNewLine "OK.`nDownloading WinRAR $(Get-WinRARExeVersion $Script:RARVER -IntToDouble)... "
$Error.Clear()
if ($Script:FETCH_WRAR) {
# get older 32-bit WinRAR
Start-BitsTransfer "https://www.rarlab.com/rar/wrar${Script:RARVER}${Script:TAGS}.exe" $pwd\ -ErrorAction SilentlyContinue
}
else {
Start-BitsTransfer "https://www.rarlab.com/rar/winrar-${Script:ARCH}-${Script:RARVER}${Script:TAGS}.exe" $pwd\ -ErrorAction SilentlyContinue
}
if ($Error) {
New-Toast -ToastTitle "Unable to fetch download" -ToastText "WinRAR $(Get-WinRARExeVersion $Script:RARVER -IntToDouble) may not exist on the server." -ToastText2 "Check the version number and try again."; exit
}
}
else {
Write-Host -NoNewLine "OK.`nDownloading WinRAR $(Get-WinRARExeVersion $LATEST -IntToDouble)... "
$Error.Clear()
Start-BitsTransfer "https://www.rarlab.com/rar/winrar-x64-${LATEST}.exe" $pwd\ -ErrorAction SilentlyContinue
if ($Error) {
New-Toast -ToastTitle "Unable to fetch download" -ToastText "Are you still connected to the internet?" -ToastText2 "Please check your internet connection."; exit
}
}
$Script:FETCH_WINRAR = $true # WinRAR was downloaded
$Script:WINRAR_EXE = (Get-Installer) # get the new installer
Write-Host "Done."
}
else {
New-Toast -ToastTitle "No internet" -ToastText "Please check your internet connection."; exit
}
}
Invoke-Installer $Script:WINRAR_EXE (Get-WinRARExeVersion $Script:WINRAR_EXE)
New-Toast -Url "https://ko-fi.com/neuralpain" -ToastTitle "WinRAR installed successfully" -ToastText2 "Thanks for using oneclickwinrar!"; exit