【问题标题】:schtasks not run the scriptschtasks 不运行脚本
【发布时间】:2019-01-29 03:31:56
【问题描述】:

在 powershell 中(管理员与否)C:\Users\mine\Desktop\LOL\test.ps1 在没有 schtasks 的情况下可以正常工作,但是当我使用它创建 schtasks 时,它无法正常工作。什么都没发生。

test.ps1可以在powershell中正常运行。
当我查询schtasks 时,出现“备份数据库”,状态已准备就绪。
假设我将其设置为10:08。 当我在10:08 之前查询时,下一个运行时间是今天10:08。 当我在10:08 之后查询时,下一个运行时间是明天10:08, 但中间什么也没发生。

PS C:\Users\mine> Schtasks /create /tn "Backup DB" /sc daily /st 10:08 /tr "C:\Users\mine\Desktop\LOL\test.ps1"
WARNING: The task name "Backup DB" already exists. Do you want to replace it (Y/N)? y
SUCCESS: The scheduled task "Backup DB" has successfully been created.

简而言之,我想每天使用 powershell 运行我的test.ps1

【问题讨论】:

  • 正常创建,程序为powershell.exe,参数为-File C:\Users\mine\Desktop\LOL\test.ps1
  • @Drew 仍然没有发生任何事情。我不知道出了什么问题。

标签: powershell windows-task-scheduler


【解决方案1】:

您的任务创建很好。因为您没有调用 powershell.exe 来调用 .ps1,所以从技术上讲,它只是像记事本文件一样调用 .ps1。

                $TaskName = "Backup DB"
                $TaskDescr = "Automated Backup DB"
                $TaskCommand = "c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
                $TaskScript = '"C:\Users\mine\Desktop\LOL\test.ps1"+'"'
                $TaskArg = "-Executionpolicy unrestricted -file $TaskScript"


                        $service = new-object -ComObject("Schedule.Service")
                        # connect to the local machine.

                        $service.Connect()
                        $rootFolder = $service.GetFolder("\")
                        $TaskDefinition = $service.NewTask(0)
                        $TaskDefinition.RegistrationInfo.Description = "$TaskDescr"
                        $TaskDefinition.Settings.Enabled = $true
                        $TaskDefinition.Settings.AllowDemandStart = $true
                        $TaskDefinition.Settings.StartWhenAvailable = $true
                        $TaskDefinition.Settings.StopIfGoingOnBatteries=$false
                        $TaskDefinition.Settings.DisallowStartIfOnBatteries=$false
                        $TaskDefinition.Settings.MultipleInstances=2
                        $taskdefinition.Settings.WakeToRun=$true
                        $triggers = $TaskDefinition.Triggers
                        $trigger = $triggers.Create(1) # Creates a "One time" trigger
                        $trigger.StartBoundary = $TaskStartTime.ToString("yyyy-MM-dd'T'HH:mm:ss")
                        $time_interval=New-TimeSpan -Minutes $interval
                        $time_interval=$time_interval.TotalSeconds
                        $trigger.Repetition.Interval= "PT"+"$time_interval"+"S"
                        $trigger.Enabled = $true
                        $TaskDefinition.Principal.RunLevel =1
                        $Action = $TaskDefinition.Actions.Create(0)
                        $action.Path = "$TaskCommand"
                        $action.Arguments = "$TaskArg"
                        # In Task Definition,
                        #   6 indicates "the task will not execute when it is registered unless a time-based trigger causes it to execute on registration."
                        #   5 indicates "Indicates that a Local System, Local Service, or Network Service account is being used as a security context to run the task.In this case, its the SYSTEM"
                        $rootFolder.RegisterTaskDefinition("$TaskName",$TaskDefinition,6,"System",$null,5) | Out-Null

我已经添加了相应的 cmets 来相应地创建任务和触发器。

替代方法是像这样直接使用Powershell:

Import-Module TaskScheduler $task = New-Task
$task.Settings.Hidden = $true
Add-TaskAction -Task $task -Path C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe –Arguments “-File C:\Users\mine\Desktop\LOL\test.ps1”
Add-TaskTrigger -Task $task -Daily -At “10:06”
Register-ScheduledJob –Name ”Monitor Group Management” -Task $task

直接方法:

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NonInteractive -File "C:\Users\mine\Desktop\LOL\test.ps1" 

希望对你有帮助。

【讨论】:

  • 如何查看任务调度器状态?
  • 手动进入调度器并点击作业进行检查。这样,如果失败,您将收到正确的消息。
猜你喜欢
  • 1970-01-01
  • 2015-03-18
  • 2011-04-20
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多