|
| 1 | +# Alist Manager Script for Windows |
| 2 | +# Version: 1.8.0 (自动编码兼容 PS5.1 + PS7+) |
| 3 | +# Author: Troray (改写 by ChatGPT) |
| 4 | + |
| 5 | +# ----------------------------- |
| 6 | +# 自动检测 PowerShell 版本并设置输出编码 |
| 7 | +$psVersion = $PSVersionTable.PSVersion.Major |
| 8 | + |
| 9 | +if ($psVersion -ge 7) { |
| 10 | + # PowerShell 7+ UTF-8 |
| 11 | + chcp 65001 > $null |
| 12 | + [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 |
| 13 | + $EncName = "UTF-8" |
| 14 | +} else { |
| 15 | + # PowerShell 5.1 / CMD GBK |
| 16 | + chcp 936 > $null |
| 17 | + [Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(936) |
| 18 | + $EncName = "GBK" |
| 19 | +} |
| 20 | + |
| 21 | +# 提示用户当前编码(可选) |
| 22 | +Write-Host "当前终端编码: $EncName" -ForegroundColor Yellow |
| 23 | +# ----------------------------- |
| 24 | + |
| 25 | +param($Action, $InstallPath) |
| 26 | +if (-not $Action) { $Action = "menu" } |
| 27 | +if (-not $InstallPath) { $InstallPath = "C:\alist" } |
| 28 | + |
| 29 | +# 颜色定义 |
| 30 | +$Green = "Green" |
| 31 | +$Red = "Red" |
| 32 | +$Yellow = "Yellow" |
| 33 | +$White = "White" |
| 34 | +$ServiceName = "AlistService" |
| 35 | + |
| 36 | +# ----------------------------- |
| 37 | +# 输出函数 |
| 38 | +function Write-Info($msg, $color="White") { |
| 39 | + $validColors = @("Black","DarkBlue","DarkGreen","DarkCyan","DarkRed", |
| 40 | + "DarkMagenta","DarkYellow","Gray","DarkGray","Blue", |
| 41 | + "Green","Cyan","Red","Magenta","Yellow","White") |
| 42 | + if ($validColors -contains $color) { |
| 43 | + [Console]::ForegroundColor = $color |
| 44 | + [Console]::WriteLine($msg) |
| 45 | + [Console]::ResetColor() |
| 46 | + } else { |
| 47 | + [Console]::WriteLine($msg) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +# 下载文件 |
| 52 | +function Download-File($url, $output) { |
| 53 | + Try { |
| 54 | + Invoke-WebRequest -Uri $url -OutFile $output -UseBasicParsing -TimeoutSec 30 |
| 55 | + return $true |
| 56 | + } Catch { |
| 57 | + Write-Info "下载失败: $url" $Red |
| 58 | + return $false |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +# 获取最新版本 |
| 63 | +function Get-LatestVersion { |
| 64 | + $apiUrl = "https://dapi.alistgo.com/v0/version/latest" |
| 65 | + Try { |
| 66 | + $json = Invoke-RestMethod -Uri $apiUrl -TimeoutSec 10 |
| 67 | + return $json |
| 68 | + } Catch { |
| 69 | + return $null |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +# 安装 Alist |
| 74 | +function Install-Alist { |
| 75 | + if (-Not (Test-Path $InstallPath)) { |
| 76 | + New-Item -ItemType Directory -Path $InstallPath | Out-Null |
| 77 | + } |
| 78 | + |
| 79 | + $latest = Get-LatestVersion |
| 80 | + if ($null -eq $latest) { |
| 81 | + Write-Info "获取版本信息失败,使用 GitHub 源" $Yellow |
| 82 | + $url = "https://github.com/alist-org/alist/releases/latest/download/alist-windows-amd64.zip" |
| 83 | + } else { |
| 84 | + $version = $latest.version |
| 85 | + Write-Info "最新版本: $version" $Green |
| 86 | + $url = "https://github.com/alist-org/alist/releases/download/v$version/alist-windows-amd64.zip" |
| 87 | + } |
| 88 | + |
| 89 | + $tmpZip = "$env:TEMP\alist.zip" |
| 90 | + if (-Not (Download-File $url $tmpZip)) { exit 1 } |
| 91 | + |
| 92 | + Expand-Archive -Path $tmpZip -DestinationPath $InstallPath -Force |
| 93 | + Remove-Item $tmpZip -Force |
| 94 | + |
| 95 | + Write-Info "Alist 已安装到 $InstallPath" $Green |
| 96 | + Write-Info "运行: `"$InstallPath\alist.exe server`"" $Yellow |
| 97 | +} |
| 98 | + |
| 99 | +# 更新 Alist |
| 100 | +function Update-Alist { |
| 101 | + if (-Not (Test-Path "$InstallPath\alist.exe")) { |
| 102 | + Write-Info "未检测到已安装的 Alist,请先安装" $Red |
| 103 | + exit 1 |
| 104 | + } |
| 105 | + Write-Info "开始更新..." $Green |
| 106 | + Install-Alist |
| 107 | +} |
| 108 | + |
| 109 | +# 卸载 Alist |
| 110 | +function Uninstall-Alist { |
| 111 | + if (Test-Path $InstallPath) { |
| 112 | + Remove-Item -Recurse -Force $InstallPath |
| 113 | + Write-Info "Alist 已卸载" $Green |
| 114 | + } else { |
| 115 | + Write-Info "未检测到安装目录 $InstallPath" $Yellow |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +# 注册服务 |
| 120 | +function Service-Install { |
| 121 | + if (-Not (Test-Path "$InstallPath\alist.exe")) { |
| 122 | + Write-Info "请先安装 Alist 再注册服务" $Red |
| 123 | + exit 1 |
| 124 | + } |
| 125 | + Write-Info "正在注册 Windows 服务 $ServiceName ..." $Green |
| 126 | + sc.exe create $ServiceName binPath= "`"$InstallPath\alist.exe`" server" start= auto DisplayName= "Alist Service" | Out-Null |
| 127 | + sc.exe description $ServiceName "Alist Web 文件管理" | Out-Null |
| 128 | + Write-Info "服务注册完成,已设置为开机自启" $Green |
| 129 | +} |
| 130 | + |
| 131 | +# 删除服务 |
| 132 | +function Service-Remove { |
| 133 | + Write-Info "正在删除 Windows 服务 $ServiceName ..." $Yellow |
| 134 | + sc.exe stop $ServiceName | Out-Null |
| 135 | + sc.exe delete $ServiceName | Out-Null |
| 136 | + Write-Info "服务已删除" $Green |
| 137 | +} |
| 138 | + |
| 139 | +function Service-Start { sc.exe start $ServiceName } |
| 140 | +function Service-Stop { sc.exe stop $ServiceName } |
| 141 | +function Service-Restart { Service-Stop; Start-Sleep -Seconds 2; Service-Start } |
| 142 | +function Service-Status { sc.exe query $ServiceName } |
| 143 | + |
| 144 | +# 菜单 |
| 145 | +function Show-Menu { |
| 146 | + while ($true) { |
| 147 | + Clear-Host |
| 148 | + Write-Info "`n=== Alist Windows 管理脚本 ===`n" $Green |
| 149 | + Write-Info "1. 安装 Alist" $White |
| 150 | + Write-Info "2. 更新 Alist" $White |
| 151 | + Write-Info "3. 卸载 Alist" $White |
| 152 | + Write-Info "-------------------------" $White |
| 153 | + Write-Info "4. 注册为 Windows 服务 (开机自启)" $White |
| 154 | + Write-Info "5. 删除 Windows 服务" $White |
| 155 | + Write-Info "6. 启动服务" $White |
| 156 | + Write-Info "7. 停止服务" $White |
| 157 | + Write-Info "8. 重启服务" $White |
| 158 | + Write-Info "9. 查看服务状态" $White |
| 159 | + Write-Info "-------------------------" $White |
| 160 | + Write-Info "0. 退出" $White |
| 161 | + $choice = Read-Host "请输入选项" |
| 162 | + |
| 163 | + switch ($choice) { |
| 164 | + "1" { Install-Alist; Pause } |
| 165 | + "2" { Update-Alist; Pause } |
| 166 | + "3" { Uninstall-Alist; Pause } |
| 167 | + "4" { Service-Install; Pause } |
| 168 | + "5" { Service-Remove; Pause } |
| 169 | + "6" { Service-Start; Pause } |
| 170 | + "7" { Service-Stop; Pause } |
| 171 | + "8" { Service-Restart; Pause } |
| 172 | + "9" { Service-Status; Pause } |
| 173 | + "0" { exit 0 } |
| 174 | + default { Write-Info "无效选择" $Red; Pause } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +# 主程序 |
| 180 | +switch ($Action) { |
| 181 | + "install" { Install-Alist } |
| 182 | + "update" { Update-Alist } |
| 183 | + "uninstall" { Uninstall-Alist } |
| 184 | + "service-install" { Service-Install } |
| 185 | + "service-remove" { Service-Remove } |
| 186 | + "start" { Service-Start } |
| 187 | + "stop" { Service-Stop } |
| 188 | + "restart" { Service-Restart } |
| 189 | + "status" { Service-Status } |
| 190 | + "menu" { Show-Menu } |
| 191 | + default { Show-Menu } |
| 192 | +} |
0 commit comments