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

如何解决DSC创建的计划任务无法手动启动的问题?

使用DSC创建计划任务(配置创建时启动、5分钟内停止自动重启)

1. 创建DSC配置

保存为tasks.ps1

Configuration Tasks {

    Import-DscResource -ModuleName ComputerManagementDsc -ModuleVersion 8.4.0

    Node 'localhost' {
        ScheduledTask 'MyTask' {
            TaskName = 'My Task'
            TaskPath = '\\My Scheduled Tasks'
            ActionExecutable = '%ProgramFiles%\\PowerShell\\7\\pwsh.exe'
            ActionArguments = '-ExecutionPolicy Bypass %ProgramData%\\MyTasks\\MyTask.ps1 -Param1 a -Param2 b'
            ActionWorkingPath = '%ProgramData%\\MyTasks\\'
            ScheduleType = 'Once'
            ExecutionTimeLimit = '00:00:00'
            MultipleInstances = 'IgnoreNew'
            RestartCount = 2
            RepeatInterval = '00:05:00'
            RepetitionDuration = 'Indefinitely'
            ExecuteAsCredential = $TaskCredential
            Ensure = 'Present'
        }
    }
    
}

2. 创建示例脚本

mkdir $env:ProgramData\\MyTasks

@'
[CmdletBinding()]
param (
  $Param1,
  $Param2
)

Sleep -Seconds 120

Get-Process
'@ | Out-File $env:ProgramData\\MyTasks\\MyTask.ps1 -Encoding utf8

3. 准备先决条件

New-LocalUser `
  -Name TaskEngine `
  -Password (ConvertTo-SecureString 'mypassword!1' -AsPlainText -Force)

为该账户配置**“作为批处理作业登录”**权限(理想情况下应纳入DSC配置,此处为简化示例暂未添加)

注:需在本地安全策略中操作,路径为「本地策略 -> 用户权限分配 -> 作为批处理作业登录」,将目标用户添加至权限列表

4. 创建MOF文件

Install-Module ComputerManagementDsc -RequiredVersion 8.4.0

$TaskCredential = New-Object System.Management.Automation.PsCredential(
  'TaskEngine', 
  (ConvertTo-SecureString 'mypassword!1' -AsPlainText -Force)
)


$ConfigurationStructure = @{
  AllNodes = @(
   @{
      NodeName = 'localhost'
      PSDscAllowPlainTextPassword = $true
    }
  )
}

. .\\Tasks.ps1

tasks -ConfigurationData $ConfigurationStructure

5. 应用MOF文件

Start-DscConfiguration -Path .\\Tasks\\ -Verbose -Wait

6. 任务计划程序检查结果

计划任务已创建,但存在以下问题:

  • 执行一段时间后,“上次运行结果”显示为0x800710e0,提示信息为The operator or administrator has refused the request(操作员或管理员已拒绝请求),手动创建任务也会出现此问题
  • 无法手动启动任务,提示The user account does not have the permission to run this task(用户账户无权限运行此任务)
  • 任务运行时导出会导致MMC崩溃(此现象可能为正常情况)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 08:30:01