# ============================================ # WSL Superset 自启动修复脚本 # 功能:修复 WSL-Superset 自启动问题,重新配置任务计划程序 # 创建时间:2025年1月10日 # 版本:1.0 - 修复自启动问题 # ============================================ # 设置控制台编码为 UTF-8 以支持中文显示 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $OutputEncoding = [System.Text.Encoding]::UTF8 # 日志记录函数 function Write-Log { param( [string]$Message, [string]$Level = "INFO" ) $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $logMessage = "[$timestamp] [$Level] $Message" Write-Host $logMessage } # 检查是否以管理员权限运行 function Test-Administrator { $currentUser = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = New-Object Security.Principal.WindowsPrincipal($currentUser) return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } # 主函数 function Fix-AutoStartup { try { Write-Log "Starting WSL-Superset auto-startup fix process" # 检查管理员权限 if (-not (Test-Administrator)) { Write-Log "ERROR: This script requires administrator privileges" "ERROR" Write-Log "Please run PowerShell as Administrator and try again" "ERROR" return 1 } # 获取当前脚本目录 $scriptDir = if ($MyInvocation.MyCommand.Path) { Split-Path -Parent $MyInvocation.MyCommand.Path } else { Get-Location } $batFilePath = Join-Path $scriptDir "startup-superset.bat" # 检查批处理文件是否存在 if (-not (Test-Path $batFilePath)) { Write-Log "ERROR: startup-superset.bat not found in current directory" "ERROR" Write-Log "Expected path: $batFilePath" "ERROR" return 1 } Write-Log "Found batch file: $batFilePath" # 任务配置参数 $taskName = "WSL-Superset-AutoStart" $taskDescription = "Auto restart Superset Docker container via WSL on system startup (Fixed version)" $taskPath = "\WSL-Containers\" Write-Log "Task Name: $taskName" Write-Log "Task Description: $taskDescription" Write-Log "Task Path: $taskPath" # 删除现有任务(如果存在) Write-Log "Removing existing task if present" try { $existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue if ($existingTask) { Unregister-ScheduledTask -TaskName $taskName -Confirm:$false Write-Log "Existing task removed successfully" } else { Write-Log "No existing task found" } } catch { Write-Log "WARNING: Error removing existing task: $($_.Exception.Message)" "WARN" } # 创建任务操作 Write-Log "Creating task action" $action = New-ScheduledTaskAction -Execute $batFilePath -WorkingDirectory $scriptDir # 创建触发器(用户登录后,增加延迟) Write-Log "Creating logon trigger with 3-minute delay" $trigger = New-ScheduledTaskTrigger -AtLogOn $trigger.Delay = "PT3M" # 用户登录后延迟3分钟启动 # 创建任务设置(增强错误处理) Write-Log "Creating task settings with enhanced error handling" $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -ExecutionTimeLimit (New-TimeSpan -Minutes 10) -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 5) # 创建任务主体(以当前用户身份运行) Write-Log "Creating task principal with current user account" $principal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive -RunLevel Highest # 注册任务 Write-Log "Registering scheduled task with enhanced configuration" try { $null = Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings -Principal $principal -Description $taskDescription -TaskPath $taskPath Write-Log "Task registration completed successfully" } catch { Write-Log "ERROR: Failed to register scheduled task" "ERROR" Write-Log "Exception: $($_.Exception.Message)" "ERROR" return 1 } # 验证任务创建 Write-Log "Verifying task creation" $taskExists = $false try { $null = Get-ScheduledTask -TaskName $taskName -ErrorAction Stop $taskExists = $true } catch { $taskExists = $false } if ($taskExists) { Write-Log "SUCCESS: Task created successfully with enhanced configuration" "SUCCESS" Write-Log "Task Name: $taskName" Write-Log "Task Path: $taskPath" Write-Log "Trigger: User Logon with 3-minute delay" Write-Log "Execution Time Limit: 10 minutes" Write-Log "Restart Count: 3 attempts" Write-Log "Restart Interval: 5 minutes" Write-Log "Auto-startup fix completed successfully" "SUCCESS" Write-Log "The startup-superset.bat will now run automatically after user logon with enhanced error handling" return 0 } else { Write-Log "ERROR: Failed to create scheduled task" "ERROR" return 1 } } catch { Write-Log "ERROR: An exception occurred during fix process" "ERROR" Write-Log "Exception: $($_.Exception.Message)" "ERROR" Write-Log "Stack Trace: $($_.ScriptStackTrace)" "ERROR" return 1 } } # 执行主函数 Write-Log "=== WSL Superset Auto-Startup Fix ===" Write-Log "This script will fix the WSL-Superset auto-startup issues" Write-Log "" $result = Fix-AutoStartup if ($result -eq 0) { Write-Log "" Write-Log "=== Fix Completed Successfully ===" "SUCCESS" Write-Log "Your WSL-Superset auto-startup has been fixed with the following improvements:" Write-Log "- User logon trigger with 3-minute delay to ensure WSL is ready" Write-Log "- Enhanced error handling with retry mechanisms" Write-Log "- 10-minute execution time limit" Write-Log "- 3 restart attempts with 5-minute intervals" Write-Log "- Improved logging and error reporting" Write-Log "" Write-Log "The task will now run automatically after user logon" Write-Log "You can view the task in Task Scheduler under: WSL-Containers\WSL-Superset-AutoStart" } else { Write-Log "" Write-Log "=== Fix Failed ===" "ERROR" Write-Log "Please check the error messages above and try again" } Write-Log "" Write-Log "Press any key to exit..." $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")