11# Alist Manager Script for Windows
2- # Version: 1.8.0 (自动编码兼容 PS5.1 + PS7+)
3- # Author: Troray (改写 by ChatGPT)
2+ # Version: 3.57.0(仅首次安装后随机密码)
43
54# -----------------------------
65# 自动检测 PowerShell 版本并设置输出编码
76$psVersion = $PSVersionTable.PSVersion.Major
8-
97if ($psVersion -ge 7 ) {
10- # PowerShell 7+ UTF-8
118 chcp 65001 > $null
129 [Console ]::OutputEncoding = [System.Text.Encoding ]::UTF8
1310 $EncName = " UTF-8"
1411} else {
15- # PowerShell 5.1 / CMD GBK
1612 chcp 936 > $null
1713 [Console ]::OutputEncoding = [System.Text.Encoding ]::GetEncoding(936 )
1814 $EncName = " GBK"
1915}
20-
21- # 提示用户当前编码(可选)
2216Write-Host " 当前终端编码: $EncName " - ForegroundColor Yellow
2317# -----------------------------
2418
2519param ($Action , $InstallPath )
2620if (-not $Action ) { $Action = " menu" }
2721if (-not $InstallPath ) { $InstallPath = " C:\alist" }
2822
29- # 颜色定义
23+ # -----------------------------
24+ # 颜色与常量
3025$Green = " Green"
3126$Red = " Red"
3227$Yellow = " Yellow"
3328$White = " White"
3429$ServiceName = " AlistService"
30+ $nssmPath = " $InstallPath \nssm.exe"
3531
3632# -----------------------------
37- # 输出函数
3833function Write-Info ($msg , $color = " White" ) {
3934 $validColors = @ (" Black" , " DarkBlue" , " DarkGreen" , " DarkCyan" , " DarkRed" ,
4035 " DarkMagenta" , " DarkYellow" , " Gray" , " DarkGray" , " Blue" ,
@@ -48,7 +43,6 @@ function Write-Info($msg, $color="White") {
4843 }
4944}
5045
51- # 下载文件
5246function Download-File ($url , $output ) {
5347 Try {
5448 Invoke-WebRequest - Uri $url - OutFile $output - UseBasicParsing - TimeoutSec 30
@@ -59,89 +53,177 @@ function Download-File($url, $output) {
5953 }
6054}
6155
62- # 获取最新版本
6356function Get-LatestVersion {
64- $apiUrl = " https://dapi.alistgo .com/v0/version /latest"
57+ $apiUrl = " https://api.github .com/repos/alist-org/alist/releases /latest"
6558 Try {
66- $json = Invoke-RestMethod - Uri $apiUrl - TimeoutSec 10
67- return $json
59+ $json = Invoke-RestMethod - Uri $apiUrl - Headers @ { " User-Agent " = " PowerShell " } - TimeoutSec 10
60+ return $json.tag_name.TrimStart ( " v " )
6861 } Catch {
69- return $null
62+ Write-Info " 获取版本信息失败,使用默认版本 3.52.0" $Yellow
63+ return " 3.52.0"
7064 }
7165}
7266
73- # 安装 Alist
74- function Install-Alist {
75- if (-Not (Test-Path $InstallPath )) {
76- New-Item - ItemType Directory - Path $InstallPath | Out-Null
67+ function Get-LocalIP {
68+ $ip = (Get-NetIPAddress - AddressFamily IPv4 |
69+ Where-Object {$_.IPAddress -ne " 127.0.0.1" -and $_.IPAddress -notlike " 169.*" } |
70+ Select-Object - First 1 - ExpandProperty IPAddress)
71+ if (-not $ip ) { $ip = " 127.0.0.1" }
72+ return $ip
73+ }
74+
75+ function Install-NSSM {
76+ if (-Not (Test-Path $nssmPath )) {
77+ $tmpZip = " $env: TEMP \nssm.zip"
78+ $tmpDir = " $env: TEMP \nssm"
79+ Write-Info " 正在下载 nssm ..." $Green
80+ if (-Not (Download- File " https://nssm.cc/release/nssm-2.24.zip" $tmpZip )) {
81+ Write-Info " nssm 下载失败,请手动下载 nssm-2.24.zip 并放到 $InstallPath " $Red
82+ exit 1
83+ }
84+ if (Test-Path $tmpDir ) { Remove-Item $tmpDir - Recurse - Force }
85+ Expand-Archive $tmpZip - DestinationPath $tmpDir - Force
86+
87+ $nssmExeSrc = Get-ChildItem - Path $tmpDir - Recurse - Filter " nssm.exe" | Where-Object { $_.FullName -match " win64" } | Select-Object - First 1
88+ if (-Not $nssmExeSrc ) {
89+ Write-Info " 未找到 nssm.exe" $Red
90+ exit 1
91+ }
92+
93+ Copy-Item $nssmExeSrc.FullName $InstallPath - Force
94+ Remove-Item $tmpZip - Force
95+ Remove-Item $tmpDir - Recurse - Force
96+ Write-Info " nssm 安装完成" $Green
7797 }
98+ }
7899
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"
100+ function Get-Arch {
101+ switch ($env: PROCESSOR_ARCHITECTURE ) {
102+ " AMD64" { return " amd64" }
103+ " ARM64" { return " arm64" }
104+ default { return " 386" }
87105 }
106+ }
107+
108+ function Install-Alist {
109+ if (-Not (Test-Path $InstallPath )) { New-Item - ItemType Directory - Path $InstallPath | Out-Null }
110+
111+ $arch = Get-Arch
112+ Write-Info " 检测到 CPU 架构: $arch " $Green
88113
114+ $version = Get-LatestVersion
115+ Write-Info " 最新版本: $version " $Green
116+
117+ # 官方镜像下载 URL(根据 CPU 架构)
118+ $filename = " alist-$version -windows-$arch .zip"
119+ $officialUrl = " https://alistgo.com/download/Alist/v$version /$filename "
89120 $tmpZip = " $env: TEMP \alist.zip"
90- if (-Not (Download- File $url $tmpZip )) { exit 1 }
121+ Write-Info " 尝试从官方镜像下载: $officialUrl " $Green
122+ $success = Download- File $officialUrl $tmpZip
123+
124+ if (-not $success ) {
125+ Write-Info " 官方镜像下载失败!" $Yellow
126+ # 提示用户选择 GitHub 下载
127+ Write-Info " 是否使用 GitHub 源下载?" $Green
128+ Write-Info " 1. 使用 GitHub 默认地址" $Green
129+ Write-Info " 2. 使用 GitHub 代理" $Green
130+ $choice = Read-Host " 请选择 [1-2] (默认 1)"
131+ if ($choice -eq " 2" ) {
132+ $proxyInput = Read-Host " 请输入代理地址 (https://开头,/结尾)"
133+ if ($proxyInput ) {
134+ $ghProxy = $proxyInput
135+ $downloadBase = " ${ghProxy} https://github.com/alist-org/alist/releases/latest/download"
136+ } else {
137+ $downloadBase = " https://github.com/alist-org/alist/releases/latest/download"
138+ }
139+ } else {
140+ $downloadBase = " https://github.com/alist-org/alist/releases/latest/download"
141+ }
91142
143+ $url = " $downloadBase /$filename "
144+ Write-Info " 开始从 GitHub 下载: $url " $Green
145+ if (-Not (Download- File $url $tmpZip )) {
146+ Write-Info " GitHub 下载失败!请检查网络或代理" $Red
147+ exit 1
148+ }
149+ }
150+
151+ # 解压
92152 Expand-Archive - Path $tmpZip - DestinationPath $InstallPath - Force
93153 Remove-Item $tmpZip - Force
94-
95154 Write-Info " Alist 已安装到 $InstallPath " $Green
96- Write-Info " 运行: `" $InstallPath \alist.exe server`" " $Yellow
97155}
98156
99- # 更新 Alist
100- function Update-Alist {
157+ # -----------------------------
158+ function Invoke-AlistAdminRandom {
101159 if (-Not (Test-Path " $InstallPath \alist.exe" )) {
102- Write-Info " 未检测到已安装的 Alist,请先安装" $Red
103- exit 1
160+ throw " 未找到 $InstallPath \alist.exe,请先安装 Alist。"
161+ }
162+ Push-Location $InstallPath
163+ try {
164+ $output = & " $InstallPath \alist.exe" admin random 2>&1
165+ } finally {
166+ Pop-Location
104167 }
105- Write-Info " 开始更新..." $Green
106- Install-Alist
107- }
108168
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
169+ $uMatch = ($output | Select-String - Pattern ' username:\s*(\S+)' - AllMatches).Matches | Select-Object - First 1
170+ $pMatch = ($output | Select-String - Pattern ' password:\s*(\S+)' - AllMatches).Matches | Select-Object - First 1
171+ if (-not $uMatch -or -not $pMatch ) {
172+ throw " 解析随机账号/密码失败。命令输出:`n $output "
116173 }
174+ $username = $uMatch.Groups [1 ].Value
175+ $password = $pMatch.Groups [1 ].Value
176+ return @ { Username = $username ; Password = $password ; Raw = $output }
117177}
118178
119- # 注册服务
120- function Service-Install {
179+ # -----------------------------
180+ function Service-InstallAndStart {
121181 if (-Not (Test-Path " $InstallPath \alist.exe" )) {
122182 Write-Info " 请先安装 Alist 再注册服务" $Red
123183 exit 1
124184 }
185+
186+ Install-NSSM
125187 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
188+ & $nssmPath install $ServiceName " $InstallPath \alist.exe" " server"
189+ & $nssmPath set $ServiceName Start SERVICE_AUTO_START
190+
191+ Write-Info " 正在启动服务 $ServiceName ..." $Green
192+ & $nssmPath start $ServiceName
193+
194+ Write-Info " 首次安装,随机生成 admin 账号/密码..." $Green
195+ try {
196+ $creds = Invoke-AlistAdminRandom
197+ Write-Info " 账号: $ ( $creds.Username ) " $Green
198+ Write-Info " 密码: $ ( $creds.Password ) " $Green
199+ } catch {
200+ Write-Info $_.Exception.Message $Red
201+ Write-Info " 随机生成失败(服务已启动)。如需重试,可手动执行:alist.exe admin random" $Yellow
202+ }
203+
204+ Write-Info " 登录地址: http://$ ( Get-LocalIP ) :5244" $Yellow
205+ }
206+
207+ function Service-Start {
208+ Install-NSSM
209+ Write-Info " 正在启动服务 $ServiceName ..." $Green
210+ & $nssmPath start $ServiceName
211+ Write-Info " 已启动。后续不会自动改密码;如需重置请手动执行:alist.exe admin random" $Yellow
212+ Write-Info " 登录地址: http://$ ( Get-LocalIP ) :5244" $Yellow
129213}
130214
131- # 删除服务
215+ function Service-Stop { Install-NSSM ; & $nssmPath stop $ServiceName }
216+ function Service-Restart { Service- Stop; Start-Sleep - Seconds 2 ; Service- Start }
132217function Service-Remove {
218+ Install-NSSM
133219 Write-Info " 正在删除 Windows 服务 $ServiceName ..." $Yellow
134- sc.exe stop $ServiceName | Out-Null
135- sc.exe delete $ServiceName | Out-Null
220+ & $nssmPath stop $ServiceName | Out-Null
221+ & $nssmPath remove $ServiceName confirm | Out-Null
136222 Write-Info " 服务已删除" $Green
137223}
224+ function Service-Status { Install-NSSM ; & $nssmPath status $ServiceName }
138225
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- # 菜单
226+ # -----------------------------
145227function Show-Menu {
146228 while ($true ) {
147229 Clear-Host
@@ -150,21 +232,21 @@ function Show-Menu {
150232 Write-Info " 2. 更新 Alist" $White
151233 Write-Info " 3. 卸载 Alist" $White
152234 Write-Info " -------------------------" $White
153- Write-Info " 4. 注册为 Windows 服务 (开机自启) " $White
235+ Write-Info " 4. 注册并启动服务(首次安装后随机一次) " $White
154236 Write-Info " 5. 删除 Windows 服务" $White
155- Write-Info " 6. 启动服务" $White
237+ Write-Info " 6. 启动服务(不改密码) " $White
156238 Write-Info " 7. 停止服务" $White
157- Write-Info " 8. 重启服务" $White
239+ Write-Info " 8. 重启服务(不改密码) " $White
158240 Write-Info " 9. 查看服务状态" $White
159241 Write-Info " -------------------------" $White
160242 Write-Info " 0. 退出" $White
161243 $choice = Read-Host " 请输入选项"
162244
163245 switch ($choice ) {
164246 " 1" { Install-Alist ; Pause }
165- " 2" { Update -Alist ; Pause }
166- " 3" { Uninstall-Alist ; Pause }
167- " 4" { Service- Install ; Pause }
247+ " 2" { Install -Alist ; Pause }
248+ " 3" { Remove-Item - Recurse - Force $InstallPath ; Write-Info " 已卸载 " ; Pause }
249+ " 4" { Service- InstallAndStart ; Pause }
168250 " 5" { Service- Remove; Pause }
169251 " 6" { Service- Start; Pause }
170252 " 7" { Service- Stop; Pause }
@@ -176,12 +258,12 @@ function Show-Menu {
176258 }
177259}
178260
179- # 主程序
261+ # -----------------------------
180262switch ($Action ) {
181263 " install" { Install-Alist }
182- " update" { Update -Alist }
183- " uninstall" { Uninstall-Alist }
184- " service-install" { Service- Install }
264+ " update" { Install -Alist }
265+ " uninstall" { Remove-Item - Recurse - Force $InstallPath ; Write-Info " 已卸载 " }
266+ " service-install" { Service- InstallAndStart }
185267 " service-remove" { Service- Remove }
186268 " start" { Service- Start }
187269 " stop" { Service- Stop }
0 commit comments