#script not continuing after restart

3 messages · Page 1 of 1 (latest)

hardy carbon
#

on this powershell code:

Function Schedule-RemainingTasks {
    $scriptPath = "C:\Setup-WSL-RockyLinux.ps1"

    # Directly call PowerShell.exe to run the script
    $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -WindowStyle Normal -File `"$scriptPath`""

    # Create a startup trigger with a 30-second delay
    $trigger = New-ScheduledTaskTrigger -AtStartup -Delay "00:00:30"

    # Run as the current user (interactive) with highest privileges
    $principal = New-ScheduledTaskPrincipal -UserId "$env:USERNAME" -LogonType Interactive -RunLevel Highest

    # Reliable task settings
    $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable

    # Register the scheduled task
    Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -Settings $settings -TaskName "WSLSetup" -Force

    Write-Host "`nScheduled the script to run at startup with a visible window." -ForegroundColor Green
}

It does not iniate the script after restarting the pc.
What I am doing wrong here?

fiery kelp
#

the task doesn't trigger or the script doesn't run?

spiral kettle
#

It's an AtStartup task which requires an Interactive session to run. Are you sure you don't want AtLogon?