计划任务不会显示在Get-ScheduledTask结果中

时间:2017-02-15 04:29:16

标签: powershell scheduled-tasks octopus-deploy windows2012

我在" \"下使用Windows任务计划程序GUI定义了一些计划任务。 [默认]路径但是当我在powershell中运行Get-ScheduledTask时,它不会返回它们。为什么呢?

我尝试使用Get-ScheduledTask -TaskName "MyTaskName"使用我的任务名称之一,但它提供了"没有找到带有属性' TaskName'的MSFT_ScheduledTask对象。等于' MyTaskName'"

其实我已经尝试了https://library.octopusdeploy.com/step-template/actiontemplate-windows-scheduled-task-disable但它没有用,所以我尝试直接运行脚本。

更新 我找到了以下脚本来获取http://www.fixitscripts.com/problems/getting-a-list-of-scheduled-tasks上的任务列表:

# PowerShell script to get scheduled tasks from local computer
$schedule = new-object -com("Schedule.Service")
$schedule.connect() 
$tasks = $schedule.getfolder("\").gettasks(0)
$tasks  | Format-Table   Name , LastRunTime    # -AutoSize
IF($tasks.count -eq 0) {Write-Host “Schedule is Empty”}
Read-Host

先谢谢你的帮助。

3 个答案:

答案 0 :(得分:2)

您是否尝试过使用com对象?这段代码适合我:

# FOR A REMOTE MACHINE
$s = 'SERVER_NAME' # update this with server name
($TaskScheduler = New-Object -ComObject Schedule.Service).Connect($s)


# FOR LOCAL MACHINE
($TaskScheduler = New-Object -ComObject Schedule.Service).Connect()

#now we can query the schedules...
cls;$TaskScheduler.GetFolder('\').GetTasks(0) | Select Name, State, Enabled, LastRunTime, LastTaskResult | Out-GridView

此代码将检索特定任务并启用它:

$task = $TaskScheduler.GetFolder('\').GetTask("TASKNAME")
$task.Enabled = $true

答案 1 :(得分:1)

从用户级提示符运行Get-ScheduledTask时,即使用户是管理员,他们也只会看到可以使用用户级权限读取的任务。在TaskSchd.Msc窗口中查看它们表示该程序正在以不同的权限运行。

因此,以管理员身份运行PowerShell提示可以解决问题。

从命令提示符处使用SchTasks.exe时会出现同样的问题。

只是我的两分钱。

答案 2 :(得分:-1)

答案可能是由于UAC尝试以管理员身份运行PowerShell(右键单击以管理员身份运行)并运行命令 - 请参阅此处 http://david-homer.blogspot.co.uk/2017/10/not-all-scheduled-tasks-show-up-when.html