找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 5202|回复: 1

[问题求助] PowerShell创建计划任务win7系统无法执行

[复制链接]
发表于 2025-5-19 11:30:05 | 显示全部楼层 |阅读模式

  1. # 创建计划任务
  2. function Create-ScheduledTask {
  3.     $taskName = "A1"
  4.     $taskDescription = "Checks if the file exists, copies it if missing, and starts the program."
  5.     $cmdCommand = "C:\1.exe"
  6.       $action = New-ScheduledTaskAction -Execute "1.exe" -Argument " $cmdCommand"
  7.     $trigger = New-ScheduledTaskTrigger -AtLogon
  8.     $principal = New-ScheduledTaskPrincipal -UserId "$env:USERNAME" -LogonType Interactive -RunLevel Highest
  9.     Register-ScheduledTask -TaskName $taskName -Trigger $trigger -Action $action -Principal $principal -Description $taskDescription
  10. }

  11. # 检查计划任务是否存在
  12. if (-not (Get-ScheduledTask -TaskName "doistry" -ErrorAction SilentlyContinue)) {
  13.     Create-ScheduledTask
  14. } else {
  15.     exit
  16. }
复制代码
在win10系统是可以的,哪里出错了呢,在win7又不行
发表于 2025-5-20 03:25:29 | 显示全部楼层
本帖最后由 1139054012 于 2025-5-20 03:31 编辑

# 分析Windows 7上PowerShell计划任务创建失败的原因

在Windows 7系统上执行此脚本可能会失败的原因包括:

1. **ScheduledTasks模块兼容性问题**:Windows 7的PowerShell版本较旧,可能不支持`New-ScheduledTask*`等cmdlet
2. **语法差异**:Windows 7使用的是较旧的Task Scheduler API
3. **执行策略限制**:可能阻止脚本执行
4. **路径问题**:硬编码路径可能不存在于Windows 7系统中

## 修改后的兼容Windows 7的代码

```powershell
# 创建计划任务 - Windows 7兼容版本
function Create-ScheduledTask {
    $taskName = "A1"
    $taskDescription = "Checks if the file exists, copies it if missing, and starts the program."
    $cmdCommand = "C:\1.exe"
   
    # 使用schtasks.exe命令替代PowerShell cmdlet
    $command = "schtasks /create /tn `"$taskName`" /tr `"$cmdCommand`" /sc onlogon /rl highest /f"
   
    # 执行命令
    cmd /c $command
   
    # 添加描述(需要额外步骤)
    $xmlPath = [System.IO.Path]::GetTempFileName()
    schtasks /query /tn $taskName /xml > $xmlPath
    $xmlContent = [System.IO.File]::ReadAllText($xmlPath)
    $xmlContent = $xmlContent -replace '<Description></Description>', "<Description>$taskDescription</Description>"
    [System.IO.File]::WriteAllText($xmlPath, $xmlContent)
    schtasks /delete /tn $taskName /f
    schtasks /create /xml $xmlPath /tn $taskName
    Remove-Item $xmlPath
}

# 检查计划任务是否存在 - Windows 7兼容方法
$taskExists = $false
try {
    $output = schtasks /query /tn "doistry" 2>&1
    if ($LASTEXITCODE -eq 0) {
        $taskExists = $true
    }
} catch {
    $taskExists = $false
}

if (-not $taskExists) {
    Create-ScheduledTask
} else {
    exit
}
```

## 替代方案:使用COM对象

另一种兼容Windows 7的方法是使用Task Scheduler COM对象:

```powershell
function Create-ScheduledTask {
    $taskName = "A1"
    $taskDescription = "Checks if the file exists, copies it if missing, and starts the program."
    $cmdCommand = "C:\1.exe"
   
    $service = New-Object -ComObject "Schedule.Service"
    $service.Connect()
    $rootFolder = $service.GetFolder("\")
   
    $taskDefinition = $service.NewTask(0)
    $taskDefinition.RegistrationInfo.Description = $taskDescription
   
    # 设置触发器(登录时)
    $trigger = $taskDefinition.Triggers.Create(9)  # TASK_TRIGGER_LOGON = 9
    $trigger.Enabled = $true
   
    # 设置动作
    $action = $taskDefinition.Actions.Create(0)  # TASK_ACTION_EXEC = 0
    $action.Path = $cmdCommand
   
    # 注册任务
    $rootFolder.RegisterTaskDefinition(
        $taskName,
        $taskDefinition,
        6,  # TASK_CREATE_OR_UPDATE
        $null,
        $null,
        1   # TASK_LOGON_INTERACTIVE_TOKEN
    )
}

# 检查任务是否存在
function TaskExists($taskName) {
    try {
        $service = New-Object -ComObject "Schedule.Service"
        $service.Connect()
        $service.GetFolder("\").GetTask($taskName) | Out-Null
        return $true
    } catch {
        return $false
    }
}

if (-not (TaskExists "doistry")) {
    Create-ScheduledTask
} else {
    exit
}
```

## 使用建议

1. 确保脚本以管理员权限运行
2. 检查`C:\1.exe`路径是否真实存在
3. 考虑添加错误处理逻辑
4. 如果需要更复杂的任务设置,建议使用第二种方法(COM对象)

这些修改应该能在Windows 7系统上成功创建计划任务。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 04:28 , Processed in 0.016634 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表