do not exit entire script

This commit is contained in:
Calyx Hikari 2025-06-29 13:27:02 +08:00
parent 4ada8f2fde
commit a238ee84d2
4 changed files with 392 additions and 368 deletions

90
32
View file

@ -3,19 +3,20 @@
Add-Type -AssemblyName System.IO.Compression.FileSystem Add-Type -AssemblyName System.IO.Compression.FileSystem
if ($PSVersionTable.PSEdition -ne 'Desktop' -and $env:OS -ne 'Windows_NT') { function Main {
if ($PSVersionTable.PSEdition -ne 'Desktop' -and $env:OS -ne 'Windows_NT') {
Write-Warning "Please run it on Windows. " Write-Warning "Please run it on Windows. "
exit 1 return 1
} }
$flagPath = "$env:TEMP\elevation_success.flag" $flagPath = "$env:TEMP\elevation_success.flag"
if (Test-Path $flagPath) { Remove-Item $flagPath -ErrorAction SilentlyContinue } if (Test-Path $flagPath) { Remove-Item $flagPath -ErrorAction SilentlyContinue }
# --- Elevation check --- # --- Elevation check ---
$IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( $IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator) [Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $IsAdmin) { if (-not $IsAdmin) {
Write-Host "Elevation required. Relaunching..." Write-Host "Elevation required. Relaunching..."
$tempScript = [IO.Path]::ChangeExtension([IO.Path]::GetTempFileName(), ".ps1") $tempScript = [IO.Path]::ChangeExtension([IO.Path]::GetTempFileName(), ".ps1")
Invoke-WebRequest -Uri "https://get.msvc.win/32" -OutFile $tempScript Invoke-WebRequest -Uri "https://get.msvc.win/32" -OutFile $tempScript
@ -23,44 +24,46 @@ if (-not $IsAdmin) {
Start-Sleep -Seconds 10 Start-Sleep -Seconds 10
if (-Not (Test-Path $flagPath)) { if (-Not (Test-Path $flagPath)) {
Write-Warning "User did not confirm UAC prompt or something went wrong." Write-Warning "User did not confirm UAC prompt or something went wrong."
exit 1 return 1
} else { }
else {
Remove-Item $flagPath -ErrorAction SilentlyContinue Remove-Item $flagPath -ErrorAction SilentlyContinue
Write-Host "Setup ran successfully." Write-Host "Setup ran successfully."
exit 0 return 0
}
} }
}
# --- Elevated section --- # --- Elevated section ---
New-Item -Path $flagPath -ItemType File -Force | Out-Null New-Item -Path $flagPath -ItemType File -Force | Out-Null
Write-Host "Running with elevated privileges..." Write-Host "Running with elevated privileges..."
$tempDir = New-Item -ItemType Directory -Path ([IO.Path]::Combine($env:TEMP, [guid]::NewGuid().ToString())) $tempDir = New-Item -ItemType Directory -Path ([IO.Path]::Combine($env:TEMP, [guid]::NewGuid().ToString()))
$aria2Dir = Join-Path $tempDir "aria2" $aria2Dir = Join-Path $tempDir "aria2"
$aria2Exe = Join-Path $aria2Dir "aria2c.exe" $aria2Exe = Join-Path $aria2Dir "aria2c.exe"
# --- Check User IP location--- # --- Check User IP location---
$geoInfo = Invoke-RestMethod -Uri 'https://ipapi.co/json/' $geoInfo = Invoke-RestMethod -Uri 'https://ipapi.co/json/'
$countryCode = $geoInfo.country $countryCode = $geoInfo.country
# --- Ensure aria2c is available --- # --- Ensure aria2c is available ---
if (-not (Test-Path $aria2Exe)) { if (-not (Test-Path $aria2Exe)) {
Write-Host "Downloading aria2c..." Write-Host "Downloading aria2c..."
$aria2Zip = Join-Path $tempDir "aria2.zip" $aria2Zip = Join-Path $tempDir "aria2.zip"
if ($countryCode -eq 'CN') { if ($countryCode -eq 'CN') {
$aria2Url = "https://gitee.com/HikariCalyx/OSTRemote/releases/download/v1.0/aria2-1.37.0-win-32bit-build1.zip" $aria2Url = "https://gitee.com/HikariCalyx/OSTRemote/releases/download/v1.0/aria2-1.37.0-win-32bit-build1.zip"
} else { }
else {
$aria2Url = "https://github.com/aria2/aria2/releases/download/release-1.37.0/aria2-1.37.0-win-32bit-build1.zip" $aria2Url = "https://github.com/aria2/aria2/releases/download/release-1.37.0/aria2-1.37.0-win-32bit-build1.zip"
} }
Invoke-WebRequest -Uri $aria2Url -OutFile $aria2Zip Invoke-WebRequest -Uri $aria2Url -OutFile $aria2Zip
Expand-Archive -Path $aria2Zip -DestinationPath $aria2Dir -Force Expand-Archive -Path $aria2Zip -DestinationPath $aria2Dir -Force
$found = Get-ChildItem -Path $aria2Dir -Recurse -Filter "aria2c.exe" | Select-Object -First 1 $found = Get-ChildItem -Path $aria2Dir -Recurse -Filter "aria2c.exe" | Select-Object -First 1
if ($found) { Copy-Item $found.FullName -Destination $aria2Exe -Force } if ($found) { Copy-Item $found.FullName -Destination $aria2Exe -Force }
} }
# --- Define all downloads --- # --- Define all downloads ---
$downloads = @( $downloads = @(
@{ Name = "directx_redist.exe"; Url = "https://download.microsoft.com/download/8/4/a/84a35bf1-dafe-4ae8-82af-ad2ae20b6b14/directx_jun2010_redist.exe" }, @{ Name = "directx_redist.exe"; Url = "https://download.microsoft.com/download/8/4/a/84a35bf1-dafe-4ae8-82af-ad2ae20b6b14/directx_jun2010_redist.exe" },
@{ Name = "vcredist_2005_x86.exe"; Url = "https://download.microsoft.com/download/8/b/4/8b42259f-5d70-43f4-ac2e-4b208fd8d66a/vcredist_x86.exe"; Year = "2005" }, @{ Name = "vcredist_2005_x86.exe"; Url = "https://download.microsoft.com/download/8/b/4/8b42259f-5d70-43f4-ac2e-4b208fd8d66a/vcredist_x86.exe"; Year = "2005" },
@{ Name = "vcredist_2008_x86.exe"; Url = "https://download.microsoft.com/download/5/D/8/5D8C65CB-C849-4025-8E95-C3966CAFD8AE/vcredist_x86.exe"; Year = "2008" }, @{ Name = "vcredist_2008_x86.exe"; Url = "https://download.microsoft.com/download/5/D/8/5D8C65CB-C849-4025-8E95-C3966CAFD8AE/vcredist_x86.exe"; Year = "2008" },
@ -68,10 +71,10 @@ $downloads = @(
@{ Name = "vcredist_2012_x86.exe"; Url = "https://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x86.exe"; Year = "2012" }, @{ Name = "vcredist_2012_x86.exe"; Url = "https://download.microsoft.com/download/1/6/B/16B06F60-3B20-4FF2-B699-5E9B7962F9AE/VSU_4/vcredist_x86.exe"; Year = "2012" },
@{ Name = "vcredist_2013_x86.exe"; Url = "https://aka.ms/highdpimfc2013x86enu"; Year = "2013" }, @{ Name = "vcredist_2013_x86.exe"; Url = "https://aka.ms/highdpimfc2013x86enu"; Year = "2013" },
@{ Name = "vcredist_2015-2022_x86.exe"; Url = "https://aka.ms/vs/17/release/vc_redist.x86.exe"; Year = "2015-2022" } @{ Name = "vcredist_2015-2022_x86.exe"; Url = "https://aka.ms/vs/17/release/vc_redist.x86.exe"; Year = "2015-2022" }
) )
# --- Download everything up front --- # --- Download everything up front ---
foreach ($item in $downloads) { foreach ($item in $downloads) {
$outFile = Join-Path $tempDir $item.Name $outFile = Join-Path $tempDir $item.Name
Write-Host "Downloading $($item.Name)..." Write-Host "Downloading $($item.Name)..."
Start-Process -FilePath $aria2Exe -ArgumentList @( Start-Process -FilePath $aria2Exe -ArgumentList @(
@ -84,23 +87,26 @@ foreach ($item in $downloads) {
"--min-split-size=1M", "--min-split-size=1M",
"`"$($item.Url)`"" "`"$($item.Url)`""
) -NoNewWindow -Wait ) -NoNewWindow -Wait
} }
# --- Install DirectX --- # --- Install DirectX ---
Write-Host "Installing DirectX..." Write-Host "Installing DirectX..."
Start-Process -FilePath "$tempDir\directx_redist.exe" -ArgumentList "/Q /T:`"$tempDir`"" -Wait Start-Process -FilePath "$tempDir\directx_redist.exe" -ArgumentList "/Q /T:`"$tempDir`"" -Wait
Start-Process -FilePath "$tempDir\DXSETUP.exe" -ArgumentList "/silent" -Wait Start-Process -FilePath "$tempDir\DXSETUP.exe" -ArgumentList "/silent" -Wait
# --- Install VC++ Redistributables --- # --- Install VC++ Redistributables ---
foreach ($item in $downloads | Where-Object { $_.Name -like "vcredist*" }) { foreach ($item in $downloads | Where-Object { $_.Name -like "vcredist*" }) {
$exePath = Join-Path $tempDir $item.Name $exePath = Join-Path $tempDir $item.Name
$args = if ($item.Year -in @("2005", "2008")) { "/q" } else { "/quiet /norestart" } $args = if ($item.Year -in @("2005", "2008")) { "/q" } else { "/quiet /norestart" }
Write-Host "Installing $($item.Name)..." Write-Host "Installing $($item.Name)..."
Start-Process -FilePath $exePath -ArgumentList $args -Wait Start-Process -FilePath $exePath -ArgumentList $args -Wait
}
# --- Cleanup ---
Write-Host "Cleaning up temporary files..."
Start-Sleep -Seconds 2
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path $flagPath -Force -ErrorAction SilentlyContinue
} }
# --- Cleanup --- Main
Write-Host "Cleaning up temporary files..."
Start-Sleep -Seconds 2
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path $flagPath -Force -ErrorAction SilentlyContinue

82
64
View file

@ -3,19 +3,20 @@
Add-Type -AssemblyName System.IO.Compression.FileSystem Add-Type -AssemblyName System.IO.Compression.FileSystem
if ($PSVersionTable.PSEdition -ne 'Desktop' -and $env:OS -ne 'Windows_NT') { function Main {
if ($PSVersionTable.PSEdition -ne 'Desktop' -and $env:OS -ne 'Windows_NT') {
Write-Warning "Please run it on Windows. " Write-Warning "Please run it on Windows. "
exit 1 return 1
} }
$flagPath = "$env:TEMP\elevation_success.flag" $flagPath = "$env:TEMP\elevation_success.flag"
if (Test-Path $flagPath) { Remove-Item $flagPath -ErrorAction SilentlyContinue } if (Test-Path $flagPath) { Remove-Item $flagPath -ErrorAction SilentlyContinue }
# --- Elevation check --- # --- Elevation check ---
$IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( $IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator) [Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $IsAdmin) { if (-not $IsAdmin) {
Write-Host "Elevation required. Relaunching..." Write-Host "Elevation required. Relaunching..."
$tempScript = [IO.Path]::ChangeExtension([IO.Path]::GetTempFileName(), ".ps1") $tempScript = [IO.Path]::ChangeExtension([IO.Path]::GetTempFileName(), ".ps1")
Invoke-WebRequest -Uri "https://get.msvc.win/64" -OutFile $tempScript Invoke-WebRequest -Uri "https://get.msvc.win/64" -OutFile $tempScript
@ -23,44 +24,46 @@ if (-not $IsAdmin) {
Start-Sleep -Seconds 10 Start-Sleep -Seconds 10
if (-Not (Test-Path $flagPath)) { if (-Not (Test-Path $flagPath)) {
Write-Warning "User did not confirm UAC prompt or something went wrong." Write-Warning "User did not confirm UAC prompt or something went wrong."
exit 1 return 1
} else { }
else {
Remove-Item $flagPath -ErrorAction SilentlyContinue Remove-Item $flagPath -ErrorAction SilentlyContinue
Write-Host "Setup ran successfully." Write-Host "Setup ran successfully."
exit 0 return 0
}
} }
}
# --- Elevated section --- # --- Elevated section ---
New-Item -Path $flagPath -ItemType File -Force | Out-Null New-Item -Path $flagPath -ItemType File -Force | Out-Null
Write-Host "Running with elevated privileges..." Write-Host "Running with elevated privileges..."
$tempDir = New-Item -ItemType Directory -Path ([IO.Path]::Combine($env:TEMP, [guid]::NewGuid().ToString())) $tempDir = New-Item -ItemType Directory -Path ([IO.Path]::Combine($env:TEMP, [guid]::NewGuid().ToString()))
$aria2Dir = Join-Path $tempDir "aria2" $aria2Dir = Join-Path $tempDir "aria2"
$aria2Exe = Join-Path $aria2Dir "aria2c.exe" $aria2Exe = Join-Path $aria2Dir "aria2c.exe"
# --- Check User IP location--- # --- Check User IP location---
$geoInfo = Invoke-RestMethod -Uri 'https://ipapi.co/json/' $geoInfo = Invoke-RestMethod -Uri 'https://ipapi.co/json/'
$countryCode = $geoInfo.country $countryCode = $geoInfo.country
# --- Ensure aria2c is available --- # --- Ensure aria2c is available ---
if (-not (Test-Path $aria2Exe)) { if (-not (Test-Path $aria2Exe)) {
Write-Host "Downloading aria2c..." Write-Host "Downloading aria2c..."
$aria2Zip = Join-Path $tempDir "aria2.zip" $aria2Zip = Join-Path $tempDir "aria2.zip"
if ($countryCode -eq 'CN') { if ($countryCode -eq 'CN') {
$aria2Url = "https://gitee.com/HikariCalyx/OSTRemote/releases/download/v1.0/aria2-1.37.0-win-64bit-build1.zip" $aria2Url = "https://gitee.com/HikariCalyx/OSTRemote/releases/download/v1.0/aria2-1.37.0-win-64bit-build1.zip"
} else { }
else {
$aria2Url = "https://github.com/aria2/aria2/releases/download/release-1.37.0/aria2-1.37.0-win-64bit-build1.zip" $aria2Url = "https://github.com/aria2/aria2/releases/download/release-1.37.0/aria2-1.37.0-win-64bit-build1.zip"
} }
Invoke-WebRequest -Uri $aria2Url -OutFile $aria2Zip Invoke-WebRequest -Uri $aria2Url -OutFile $aria2Zip
Expand-Archive -Path $aria2Zip -DestinationPath $aria2Dir -Force Expand-Archive -Path $aria2Zip -DestinationPath $aria2Dir -Force
$found = Get-ChildItem -Path $aria2Dir -Recurse -Filter "aria2c.exe" | Select-Object -First 1 $found = Get-ChildItem -Path $aria2Dir -Recurse -Filter "aria2c.exe" | Select-Object -First 1
if ($found) { Copy-Item $found.FullName -Destination $aria2Exe -Force } if ($found) { Copy-Item $found.FullName -Destination $aria2Exe -Force }
} }
# --- Define all downloads --- # --- Define all downloads ---
$downloads = @( $downloads = @(
@{ Name = "vcredist_2005_x86.exe"; Url = "https://download.microsoft.com/download/8/b/4/8b42259f-5d70-43f4-ac2e-4b208fd8d66a/vcredist_x86.exe"; Year = "2005" }, @{ Name = "vcredist_2005_x86.exe"; Url = "https://download.microsoft.com/download/8/b/4/8b42259f-5d70-43f4-ac2e-4b208fd8d66a/vcredist_x86.exe"; Year = "2005" },
@{ Name = "vcredist_2005_x64.exe"; Url = "https://download.microsoft.com/download/8/b/4/8b42259f-5d70-43f4-ac2e-4b208fd8d66a/vcredist_x64.exe"; Year = "2005" }, @{ Name = "vcredist_2005_x64.exe"; Url = "https://download.microsoft.com/download/8/b/4/8b42259f-5d70-43f4-ac2e-4b208fd8d66a/vcredist_x64.exe"; Year = "2005" },
@{ Name = "vcredist_2008_x86.exe"; Url = "https://download.microsoft.com/download/5/D/8/5D8C65CB-C849-4025-8E95-C3966CAFD8AE/vcredist_x86.exe"; Year = "2008" }, @{ Name = "vcredist_2008_x86.exe"; Url = "https://download.microsoft.com/download/5/D/8/5D8C65CB-C849-4025-8E95-C3966CAFD8AE/vcredist_x86.exe"; Year = "2008" },
@ -73,10 +76,10 @@ $downloads = @(
@{ Name = "vcredist_2013_x64.exe"; Url = "https://aka.ms/highdpimfc2013x64enu"; Year = "2013" }, @{ Name = "vcredist_2013_x64.exe"; Url = "https://aka.ms/highdpimfc2013x64enu"; Year = "2013" },
@{ Name = "vcredist_2015-2022_x86.exe"; Url = "https://aka.ms/vs/17/release/vc_redist.x86.exe"; Year = "2015-2022" }, @{ Name = "vcredist_2015-2022_x86.exe"; Url = "https://aka.ms/vs/17/release/vc_redist.x86.exe"; Year = "2015-2022" },
@{ Name = "vcredist_2015-2022_x64.exe"; Url = "https://aka.ms/vs/17/release/vc_redist.x64.exe"; Year = "2012-2022" } @{ Name = "vcredist_2015-2022_x64.exe"; Url = "https://aka.ms/vs/17/release/vc_redist.x64.exe"; Year = "2012-2022" }
) )
# --- Download everything up front --- # --- Download everything up front ---
foreach ($item in $downloads) { foreach ($item in $downloads) {
$outFile = Join-Path $tempDir $item.Name $outFile = Join-Path $tempDir $item.Name
Write-Host "Downloading $($item.Name)..." Write-Host "Downloading $($item.Name)..."
Start-Process -FilePath $aria2Exe -ArgumentList @( Start-Process -FilePath $aria2Exe -ArgumentList @(
@ -89,18 +92,21 @@ foreach ($item in $downloads) {
"--min-split-size=1M", "--min-split-size=1M",
"`"$($item.Url)`"" "`"$($item.Url)`""
) -NoNewWindow -Wait ) -NoNewWindow -Wait
} }
# --- Install VC++ Redistributables --- # --- Install VC++ Redistributables ---
foreach ($item in $downloads | Where-Object { $_.Name -like "vcredist*" }) { foreach ($item in $downloads | Where-Object { $_.Name -like "vcredist*" }) {
$exePath = Join-Path $tempDir $item.Name $exePath = Join-Path $tempDir $item.Name
$args = if ($item.Year -in @("2005", "2008")) { "/q" } else { "/quiet /norestart" } $args = if ($item.Year -in @("2005", "2008")) { "/q" } else { "/quiet /norestart" }
Write-Host "Installing $($item.Name)..." Write-Host "Installing $($item.Name)..."
Start-Process -FilePath $exePath -ArgumentList $args -Wait Start-Process -FilePath $exePath -ArgumentList $args -Wait
}
# --- Cleanup ---
Write-Host "Cleaning up temporary files..."
Start-Sleep -Seconds 2
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path $flagPath -Force -ErrorAction SilentlyContinue
} }
# --- Cleanup --- Main
Write-Host "Cleaning up temporary files..."
Start-Sleep -Seconds 2
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path $flagPath -Force -ErrorAction SilentlyContinue

86
dx
View file

@ -3,19 +3,20 @@
Add-Type -AssemblyName System.IO.Compression.FileSystem Add-Type -AssemblyName System.IO.Compression.FileSystem
if ($PSVersionTable.PSEdition -ne 'Desktop' -and $env:OS -ne 'Windows_NT') { function Main {
if ($PSVersionTable.PSEdition -ne 'Desktop' -and $env:OS -ne 'Windows_NT') {
Write-Warning "Please run it on Windows. " Write-Warning "Please run it on Windows. "
exit 1 return 1
} }
$flagPath = "$env:TEMP\elevation_success.flag" $flagPath = "$env:TEMP\elevation_success.flag"
if (Test-Path $flagPath) { Remove-Item $flagPath -ErrorAction SilentlyContinue } if (Test-Path $flagPath) { Remove-Item $flagPath -ErrorAction SilentlyContinue }
# --- Elevation check --- # --- Elevation check ---
$IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( $IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator) [Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $IsAdmin) { if (-not $IsAdmin) {
Write-Host "Elevation required. Relaunching..." Write-Host "Elevation required. Relaunching..."
$tempScript = [IO.Path]::ChangeExtension([IO.Path]::GetTempFileName(), ".ps1") $tempScript = [IO.Path]::ChangeExtension([IO.Path]::GetTempFileName(), ".ps1")
Invoke-WebRequest -Uri "https://get.msvc.win/" -OutFile $tempScript Invoke-WebRequest -Uri "https://get.msvc.win/" -OutFile $tempScript
@ -23,49 +24,51 @@ if (-not $IsAdmin) {
Start-Sleep -Seconds 10 Start-Sleep -Seconds 10
if (-Not (Test-Path $flagPath)) { if (-Not (Test-Path $flagPath)) {
Write-Warning "User did not confirm UAC prompt or something went wrong." Write-Warning "User did not confirm UAC prompt or something went wrong."
exit 1 return 1
} else { }
else {
Remove-Item $flagPath -ErrorAction SilentlyContinue Remove-Item $flagPath -ErrorAction SilentlyContinue
Write-Host "Setup ran successfully." Write-Host "Setup ran successfully."
exit 0 return 0
}
} }
}
# --- Elevated section --- # --- Elevated section ---
New-Item -Path $flagPath -ItemType File -Force | Out-Null New-Item -Path $flagPath -ItemType File -Force | Out-Null
Write-Host "Running with elevated privileges..." Write-Host "Running with elevated privileges..."
$tempDir = New-Item -ItemType Directory -Path ([IO.Path]::Combine($env:TEMP, [guid]::NewGuid().ToString())) $tempDir = New-Item -ItemType Directory -Path ([IO.Path]::Combine($env:TEMP, [guid]::NewGuid().ToString()))
$aria2Dir = Join-Path $tempDir "aria2" $aria2Dir = Join-Path $tempDir "aria2"
$aria2Exe = Join-Path $aria2Dir "aria2c.exe" $aria2Exe = Join-Path $aria2Dir "aria2c.exe"
# --- Check User IP location--- # --- Check User IP location---
$geoInfo = Invoke-RestMethod -Uri 'https://ipapi.co/json/' $geoInfo = Invoke-RestMethod -Uri 'https://ipapi.co/json/'
$countryCode = $geoInfo.country $countryCode = $geoInfo.country
# --- Ensure aria2c is available --- # --- Ensure aria2c is available ---
if (-not (Test-Path $aria2Exe)) { if (-not (Test-Path $aria2Exe)) {
Write-Host "Downloading aria2c..." Write-Host "Downloading aria2c..."
$aria2Zip = Join-Path $tempDir "aria2.zip" $aria2Zip = Join-Path $tempDir "aria2.zip"
if ($countryCode -eq 'CN') { if ($countryCode -eq 'CN') {
$aria2Url = "https://gitee.com/HikariCalyx/OSTRemote/releases/download/v1.0/aria2-1.37.0-win-32bit-build1.zip" $aria2Url = "https://gitee.com/HikariCalyx/OSTRemote/releases/download/v1.0/aria2-1.37.0-win-32bit-build1.zip"
} else { }
else {
$aria2Url = "https://github.com/aria2/aria2/releases/download/release-1.37.0/aria2-1.37.0-win-32bit-build1.zip" $aria2Url = "https://github.com/aria2/aria2/releases/download/release-1.37.0/aria2-1.37.0-win-32bit-build1.zip"
} }
Invoke-WebRequest -Uri $aria2Url -OutFile $aria2Zip Invoke-WebRequest -Uri $aria2Url -OutFile $aria2Zip
Expand-Archive -Path $aria2Zip -DestinationPath $aria2Dir -Force Expand-Archive -Path $aria2Zip -DestinationPath $aria2Dir -Force
$found = Get-ChildItem -Path $aria2Dir -Recurse -Filter "aria2c.exe" | Select-Object -First 1 $found = Get-ChildItem -Path $aria2Dir -Recurse -Filter "aria2c.exe" | Select-Object -First 1
if ($found) { Copy-Item $found.FullName -Destination $aria2Exe -Force } if ($found) { Copy-Item $found.FullName -Destination $aria2Exe -Force }
} }
# --- Define all downloads --- # --- Define all downloads ---
$downloads = @( $downloads = @(
@{ Name = "directx_redist.exe"; Url = "https://download.microsoft.com/download/8/4/a/84a35bf1-dafe-4ae8-82af-ad2ae20b6b14/directx_jun2010_redist.exe" } @{ Name = "directx_redist.exe"; Url = "https://download.microsoft.com/download/8/4/a/84a35bf1-dafe-4ae8-82af-ad2ae20b6b14/directx_jun2010_redist.exe" }
) )
# --- Download everything up front --- # --- Download everything up front ---
foreach ($item in $downloads) { foreach ($item in $downloads) {
$outFile = Join-Path $tempDir $item.Name $outFile = Join-Path $tempDir $item.Name
Write-Host "Downloading $($item.Name)..." Write-Host "Downloading $($item.Name)..."
Start-Process -FilePath $aria2Exe -ArgumentList @( Start-Process -FilePath $aria2Exe -ArgumentList @(
@ -78,15 +81,18 @@ foreach ($item in $downloads) {
"--min-split-size=1M", "--min-split-size=1M",
"`"$($item.Url)`"" "`"$($item.Url)`""
) -NoNewWindow -Wait ) -NoNewWindow -Wait
}
# --- Install DirectX ---
Write-Host "Installing DirectX..."
Start-Process -FilePath "$tempDir\directx_redist.exe" -ArgumentList "/Q /T:`"$tempDir`"" -Wait
Start-Process -FilePath "$tempDir\DXSETUP.exe" -ArgumentList "/silent" -Wait
# --- Cleanup ---
Write-Host "Cleaning up temporary files..."
Start-Sleep -Seconds 2
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path $flagPath -Force -ErrorAction SilentlyContinue
} }
# --- Install DirectX --- Main
Write-Host "Installing DirectX..."
Start-Process -FilePath "$tempDir\directx_redist.exe" -ArgumentList "/Q /T:`"$tempDir`"" -Wait
Start-Process -FilePath "$tempDir\DXSETUP.exe" -ArgumentList "/silent" -Wait
# --- Cleanup ---
Write-Host "Cleaning up temporary files..."
Start-Sleep -Seconds 2
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path $flagPath -Force -ErrorAction SilentlyContinue

View file

@ -3,19 +3,20 @@
Add-Type -AssemblyName System.IO.Compression.FileSystem Add-Type -AssemblyName System.IO.Compression.FileSystem
if ($PSVersionTable.PSEdition -ne 'Desktop' -and $env:OS -ne 'Windows_NT') { function Main {
if ($PSVersionTable.PSEdition -ne 'Desktop' -and $env:OS -ne 'Windows_NT') {
Write-Warning "Please run it on Windows. " Write-Warning "Please run it on Windows. "
exit 1 return 1
} }
$flagPath = "$env:TEMP\elevation_success.flag" $flagPath = "$env:TEMP\elevation_success.flag"
if (Test-Path $flagPath) { Remove-Item $flagPath -ErrorAction SilentlyContinue } if (Test-Path $flagPath) { Remove-Item $flagPath -ErrorAction SilentlyContinue }
# --- Elevation check --- # --- Elevation check ---
$IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( $IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator) [Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $IsAdmin) { if (-not $IsAdmin) {
Write-Host "Elevation required. Relaunching..." Write-Host "Elevation required. Relaunching..."
$tempScript = [IO.Path]::ChangeExtension([IO.Path]::GetTempFileName(), ".ps1") $tempScript = [IO.Path]::ChangeExtension([IO.Path]::GetTempFileName(), ".ps1")
Invoke-WebRequest -Uri "https://get.msvc.win/" -OutFile $tempScript Invoke-WebRequest -Uri "https://get.msvc.win/" -OutFile $tempScript
@ -23,44 +24,46 @@ if (-not $IsAdmin) {
Start-Sleep -Seconds 10 Start-Sleep -Seconds 10
if (-Not (Test-Path $flagPath)) { if (-Not (Test-Path $flagPath)) {
Write-Warning "User did not confirm UAC prompt or something went wrong." Write-Warning "User did not confirm UAC prompt or something went wrong."
exit 1 return 1
} else { }
else {
Remove-Item $flagPath -ErrorAction SilentlyContinue Remove-Item $flagPath -ErrorAction SilentlyContinue
Write-Host "Setup ran successfully." Write-Host "Setup ran successfully."
exit 0 return 0
}
} }
}
# --- Elevated section --- # --- Elevated section ---
New-Item -Path $flagPath -ItemType File -Force | Out-Null New-Item -Path $flagPath -ItemType File -Force | Out-Null
Write-Host "Running with elevated privileges..." Write-Host "Running with elevated privileges..."
$tempDir = New-Item -ItemType Directory -Path ([IO.Path]::Combine($env:TEMP, [guid]::NewGuid().ToString())) $tempDir = New-Item -ItemType Directory -Path ([IO.Path]::Combine($env:TEMP, [guid]::NewGuid().ToString()))
$aria2Dir = Join-Path $tempDir "aria2" $aria2Dir = Join-Path $tempDir "aria2"
$aria2Exe = Join-Path $aria2Dir "aria2c.exe" $aria2Exe = Join-Path $aria2Dir "aria2c.exe"
# --- Check User IP location--- # --- Check User IP location---
$geoInfo = Invoke-RestMethod -Uri 'https://ipapi.co/json/' $geoInfo = Invoke-RestMethod -Uri 'https://ipapi.co/json/'
$countryCode = $geoInfo.country $countryCode = $geoInfo.country
# --- Ensure aria2c is available --- # --- Ensure aria2c is available ---
if (-not (Test-Path $aria2Exe)) { if (-not (Test-Path $aria2Exe)) {
Write-Host "Downloading aria2c..." Write-Host "Downloading aria2c..."
$aria2Zip = Join-Path $tempDir "aria2.zip" $aria2Zip = Join-Path $tempDir "aria2.zip"
if ($countryCode -eq 'CN') { if ($countryCode -eq 'CN') {
$aria2Url = "https://gitee.com/HikariCalyx/OSTRemote/releases/download/v1.0/aria2-1.37.0-win-64bit-build1.zip" $aria2Url = "https://gitee.com/HikariCalyx/OSTRemote/releases/download/v1.0/aria2-1.37.0-win-64bit-build1.zip"
} else { }
else {
$aria2Url = "https://github.com/aria2/aria2/releases/download/release-1.37.0/aria2-1.37.0-win-64bit-build1.zip" $aria2Url = "https://github.com/aria2/aria2/releases/download/release-1.37.0/aria2-1.37.0-win-64bit-build1.zip"
} }
Invoke-WebRequest -Uri $aria2Url -OutFile $aria2Zip Invoke-WebRequest -Uri $aria2Url -OutFile $aria2Zip
Expand-Archive -Path $aria2Zip -DestinationPath $aria2Dir -Force Expand-Archive -Path $aria2Zip -DestinationPath $aria2Dir -Force
$found = Get-ChildItem -Path $aria2Dir -Recurse -Filter "aria2c.exe" | Select-Object -First 1 $found = Get-ChildItem -Path $aria2Dir -Recurse -Filter "aria2c.exe" | Select-Object -First 1
if ($found) { Copy-Item $found.FullName -Destination $aria2Exe -Force } if ($found) { Copy-Item $found.FullName -Destination $aria2Exe -Force }
} }
# --- Define all downloads --- # --- Define all downloads ---
$downloads = @( $downloads = @(
@{ Name = "directx_redist.exe"; Url = "https://download.microsoft.com/download/8/4/a/84a35bf1-dafe-4ae8-82af-ad2ae20b6b14/directx_jun2010_redist.exe" }, @{ Name = "directx_redist.exe"; Url = "https://download.microsoft.com/download/8/4/a/84a35bf1-dafe-4ae8-82af-ad2ae20b6b14/directx_jun2010_redist.exe" },
@{ Name = "vcredist_2005_x86.exe"; Url = "https://download.microsoft.com/download/8/b/4/8b42259f-5d70-43f4-ac2e-4b208fd8d66a/vcredist_x86.exe"; Year = "2005" }, @{ Name = "vcredist_2005_x86.exe"; Url = "https://download.microsoft.com/download/8/b/4/8b42259f-5d70-43f4-ac2e-4b208fd8d66a/vcredist_x86.exe"; Year = "2005" },
@{ Name = "vcredist_2005_x64.exe"; Url = "https://download.microsoft.com/download/8/b/4/8b42259f-5d70-43f4-ac2e-4b208fd8d66a/vcredist_x64.exe"; Year = "2005" }, @{ Name = "vcredist_2005_x64.exe"; Url = "https://download.microsoft.com/download/8/b/4/8b42259f-5d70-43f4-ac2e-4b208fd8d66a/vcredist_x64.exe"; Year = "2005" },
@ -74,10 +77,10 @@ $downloads = @(
@{ Name = "vcredist_2013_x64.exe"; Url = "https://aka.ms/highdpimfc2013x64enu"; Year = "2013" }, @{ Name = "vcredist_2013_x64.exe"; Url = "https://aka.ms/highdpimfc2013x64enu"; Year = "2013" },
@{ Name = "vcredist_2015-2022_x86.exe"; Url = "https://aka.ms/vs/17/release/vc_redist.x86.exe"; Year = "2015-2022" }, @{ Name = "vcredist_2015-2022_x86.exe"; Url = "https://aka.ms/vs/17/release/vc_redist.x86.exe"; Year = "2015-2022" },
@{ Name = "vcredist_2015-2022_x64.exe"; Url = "https://aka.ms/vs/17/release/vc_redist.x64.exe"; Year = "2012-2022" } @{ Name = "vcredist_2015-2022_x64.exe"; Url = "https://aka.ms/vs/17/release/vc_redist.x64.exe"; Year = "2012-2022" }
) )
# --- Download everything up front --- # --- Download everything up front ---
foreach ($item in $downloads) { foreach ($item in $downloads) {
$outFile = Join-Path $tempDir $item.Name $outFile = Join-Path $tempDir $item.Name
Write-Host "Downloading $($item.Name)..." Write-Host "Downloading $($item.Name)..."
Start-Process -FilePath $aria2Exe -ArgumentList @( Start-Process -FilePath $aria2Exe -ArgumentList @(
@ -90,23 +93,26 @@ foreach ($item in $downloads) {
"--min-split-size=1M", "--min-split-size=1M",
"`"$($item.Url)`"" "`"$($item.Url)`""
) -NoNewWindow -Wait ) -NoNewWindow -Wait
} }
# --- Install DirectX --- # --- Install DirectX ---
Write-Host "Installing DirectX..." Write-Host "Installing DirectX..."
Start-Process -FilePath "$tempDir\directx_redist.exe" -ArgumentList "/Q /T:`"$tempDir`"" -Wait Start-Process -FilePath "$tempDir\directx_redist.exe" -ArgumentList "/Q /T:`"$tempDir`"" -Wait
Start-Process -FilePath "$tempDir\DXSETUP.exe" -ArgumentList "/silent" -Wait Start-Process -FilePath "$tempDir\DXSETUP.exe" -ArgumentList "/silent" -Wait
# --- Install VC++ Redistributables --- # --- Install VC++ Redistributables ---
foreach ($item in $downloads | Where-Object { $_.Name -like "vcredist*" }) { foreach ($item in $downloads | Where-Object { $_.Name -like "vcredist*" }) {
$exePath = Join-Path $tempDir $item.Name $exePath = Join-Path $tempDir $item.Name
$args = if ($item.Year -in @("2005", "2008")) { "/q" } else { "/quiet /norestart" } $args = if ($item.Year -in @("2005", "2008")) { "/q" } else { "/quiet /norestart" }
Write-Host "Installing $($item.Name)..." Write-Host "Installing $($item.Name)..."
Start-Process -FilePath $exePath -ArgumentList $args -Wait Start-Process -FilePath $exePath -ArgumentList $args -Wait
}
# --- Cleanup ---
Write-Host "Cleaning up temporary files..."
Start-Sleep -Seconds 2
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path $flagPath -Force -ErrorAction SilentlyContinue
} }
# --- Cleanup --- Main
Write-Host "Cleaning up temporary files..."
Start-Sleep -Seconds 2
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path $flagPath -Force -ErrorAction SilentlyContinue