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

关于PowerShell脚本重启后自动运行及脚本路径正确引用的技术咨询

关于PowerShell脚本重启后自动运行及脚本路径正确引用的技术咨询

Hey,我看你遇到了计划任务里脚本路径引用的坑——你的auto_test.ps1(路径C:\Scripts\auto_test.ps1)在设置开机启动任务时,总是没法正确指向脚本本身,反而关联到计划任务或者PowerShell进程对吧?我来帮你拆解问题,给你可行的解决办法。

先贴一下你提供的原脚本,方便对照:

param(
    #File 1 used for update
    [Parameter(Mandatory=$true)]
    [string]$file1='',
    #File 2 used for update
    [string]$file2='',
    #File 3 used for update
    [string]$file3=''
)

Start-Process $file1 -ArgumentList "AC=1 ON=1"

if ( (-not [String]::IsNullOrWhiteSpace($file2)) -and [String]::IsNullorWhiteSpace($file3))
{
    $taskTrigger = New-ScheduledTaskTrigger -AtStartup
    $taskName = "Update"
    $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ($PSCommandPath + " -file1 $file2")
    # Describe the scheduled task.
    $description = "Automation Testing"
    # Register the scheduled task
    Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Description $description
    shutdown -r
}

if ( [String]::IsNullOrWhiteSpace($file2))
{
    Unregister-ScheduledTask -Taskname "Update"
    shutdown -r
}

问题根源

你这里用$PSCommandPath来获取脚本路径,但这个变量在计划任务执行PowerShell的场景下会“失效”:计划任务直接调用的是powershell.exe,此时$PSCommandPath返回的是PowerShell可执行文件的路径,而不是你的脚本路径,这就导致计划任务找不到要运行的脚本。

解决办法:改用可靠的绝对路径获取方式

我们可以用$MyInvocation.MyCommand.Definition来获取脚本的绝对路径,这个变量在几乎所有执行场景下(包括双击脚本、命令行调用、计划任务)都能准确返回脚本的完整路径。同时还要给路径加上双引号,避免路径含空格时出错。

修改后的完整脚本如下:

param(
    #File 1 used for update
    [Parameter(Mandatory=$true)]
    [string]$file1='',
    #File 2 used for update
    [string]$file2='',
    #File 3 used for update
    [string]$file3=''
)

# 可靠获取脚本的绝对路径,替代$PSCommandPath
$scriptAbsolutePath = $MyInvocation.MyCommand.Definition

Start-Process $file1 -ArgumentList "AC=1 ON=1"

if ( (-not [String]::IsNullOrWhiteSpace($file2)) -and [String]::IsNullorWhiteSpace($file3))
{
    $taskTrigger = New-ScheduledTaskTrigger -AtStartup
    $taskName = "Update"
    # 用绝对路径引用脚本,加双引号处理空格问题
    $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ("-File `"$scriptAbsolutePath`" -file1 `"$file2`"")
    $description = "Automation Testing"
    Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Description $description
    shutdown -r
}

if ( [String]::IsNullOrWhiteSpace($file2))
{
    # 添加-Confirm:$false避免删除任务时弹出确认框,实现完全自动化
    Unregister-ScheduledTask -Taskname "Update" -Confirm:$false
    shutdown -r
}

额外优化说明

  1. 替换路径变量:用$MyInvocation.MyCommand.Definition彻底解决路径引用错误的问题;
  2. 双引号包裹路径:确保脚本路径或$file2的路径含空格时,PowerShell能正确识别;
  3. 自动确认删除任务:给Unregister-ScheduledTask加上-Confirm:$false,重启前不会弹出确认提示,让流程更顺畅。

注意事项

  • 确保计划任务的执行账户有足够权限:需要能运行脚本、创建/删除计划任务,以及执行重启操作;
  • 如果以后脚本路径可能变动,也可以把路径写在配置文件里读取,但绝对路径是当前场景下最稳妥的方案。

备注:内容来源于stack exchange,提问作者Wandering

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.22 11:28:05