You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Task Scheduler执行PowerShell脚本?任务创建后无响应

定时任务运行PowerShell弹窗脚本无响应的解决办法

问题场景

已将重启确认脚本保存至C:\Test\RealScript.ps1,脚本通过WPF弹窗询问用户是否重启电脑;使用PowerShell创建了每分钟执行一次的定时任务,任务已在Task Scheduler中生成,但脚本未执行,也无弹窗提示。

脚本内容

Add-Type -AssemblyName PresentationFramework

# Create the prompt
$messageBoxText = "Do you want to restart the PC?"
$caption = "Restart Confirmation"
$buttonType = [System.Windows.MessageBoxButton]::YesNo
$iconType = [System.Windows.MessageBoxImage]::Warning

# Show the message box
$result = [System.Windows.MessageBox]::Show($messageBoxText, $caption, $buttonType, $iconType)

# Check the user's response
if ($result -eq [System.Windows.MessageBoxResult]::Yes) {
    # Force restart the PC
    Restart-Computer -Force
}

定时任务创建代码

# Define variables
$scriptPath = "C:\Test\RealScript.ps1"
$taskName = "RunScriptEveryMinute"
$taskDescription = "This task runs the specified script every minute."

# Create the action for the task
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -File `"$scriptPath`""

# Create the trigger for the task to run every 1 minute with a reasonable repetition duration
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration (New-TimeSpan -Days 365)

# Create task settings to run the task whether the user is logged on or not
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -MultipleInstances Parallel

# Register the scheduled task
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskName -Description $taskDescription -Settings $settings -User "SYSTEM"

解决步骤

1. 修复SYSTEM账户无法显示交互式弹窗的问题

SYSTEM账户运行的进程默认处于会话0,而用户登录后的交互式会话为会话1及以上,导致WPF弹窗无法在用户桌面显示。可二选一解决:

  • 改用当前登录用户运行任务:修改定时任务的用户为当前登录的管理员账户,在任务配置中选择「仅当用户登录时运行」,并勾选「显示任务窗口」(部分系统版本可见)。
  • 配置任务允许交互(仅适用于Windows旧版本):在Task Scheduler中编辑任务,进入「常规」选项卡,勾选「不管用户是否登录都要运行」,同时勾选「允许此任务与桌面交互」(Windows 10/11后续版本已移除该选项,优先推荐第一种方法)。

2. 确保脚本具备执行权限

PowerShell默认执行策略可能阻止脚本运行,可通过以下方式处理:

  • 在任务的PowerShell参数中添加-ExecutionPolicy Bypass,修改后的任务Action代码:
    $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`""
    
  • 或全局设置PowerShell执行策略(需管理员权限):
    Set-ExecutionPolicy RemoteSigned -Force
    

3. 添加管理员权限运行任务

重启电脑需要管理员权限,需确保任务以最高权限运行:

  • 在Task Scheduler中编辑任务,进入「常规」选项卡,勾选「使用最高权限运行」。
  • 或在创建任务时添加-RunLevel Highest参数:
    Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskName -Description $taskDescription -Settings $settings -User "你的用户名" -RunLevel Highest
    

4. 排查任务执行日志

通过Task Scheduler历史记录定位问题:

  1. 打开Task Scheduler,找到目标任务。
  2. 右键点击任务,选择「查看」→「历史记录」。
  3. 检查是否有任务触发记录、报错信息(如脚本路径错误、权限不足等)。

内容的提问来源于stack exchange,提问作者dev4Life

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 11:07:45