# Configure qBittorrent: enable WebUI 8080, localhost auth bypass, autostart $ErrorActionPreference = "Continue" # locate qbittorrent.exe $exe = $null foreach ($c in @("C:\Program Files\qBittorrent\qbittorrent.exe","C:\Program Files (x86)\qBittorrent\qbittorrent.exe")) { if (Test-Path $c) { $exe = $c; break } } if (-not $exe) { Write-Output "ERR qbittorrent.exe not found"; exit 1 } Write-Output "qbittorrent.exe: $exe" # stop if running Get-Process qbittorrent -ErrorAction SilentlyContinue | Stop-Process -Force Start-Sleep 1 # write qBittorrent.ini $cfgDir = "$env:APPDATA\qBittorrent" New-Item -ItemType Directory -Force -Path $cfgDir | Out-Null New-Item -ItemType Directory -Force -Path "C:\Downloads" | Out-Null $ini = @( '[LegalNotice]', 'Accepted=true', '', '[Preferences]', 'WebUI\Enabled=true', 'WebUI\Port=8080', 'WebUI\LocalHostAuth=false', 'WebUI\AuthSubnetWhitelistEnabled=true', 'WebUI\AuthSubnetWhitelist=127.0.0.1/8, ::1/128', 'WebUI\Username=admin', 'Downloads\SavePath=C:/Downloads/', 'General\Locale=zh' ) Set-Content -Path "$cfgDir\qBittorrent.ini" -Value $ini -Encoding UTF8 Write-Output "qBittorrent.ini written" # scheduled task: start at logon in interactive session $me = "$env:USERDOMAIN\$env:USERNAME" $action = New-ScheduledTaskAction -Execute $exe $trigger = New-ScheduledTaskTrigger -AtLogOn $principal = New-ScheduledTaskPrincipal -UserId $me -LogonType Interactive -RunLevel Highest Register-ScheduledTask -TaskName "qbittorrent" -Action $action -Trigger $trigger -Principal $principal -Force | Out-Null Start-ScheduledTask -TaskName qbittorrent Start-Sleep 8 try { $r = Invoke-WebRequest "http://127.0.0.1:8080/" -UseBasicParsing -TimeoutSec 6; Write-Output ("qbit WebUI: " + $r.StatusCode) } catch { Write-Output ("qbit WebUI err: " + $_.Exception.Message) } Write-Output "QBIT_DONE"