为什么此脚本无法通过Powershell脚本生成计划任务事件?

时间:2012-08-07 13:42:46

标签: windows powershell windows-server-2003

我遇到了一个我一直在努力的脚本的小问题。

代码本身非常好用;当我在我的本地工作站上运行它时,它是完美的。但是,当我在其中一个需要安装SCCM的服务器(Win 2003 R2 32位)上运行它时(该脚本被设计为自动安装),它在我的一个功能中失败。相关代码如下:

    # Schedules Weekly tasks
    function SchWeekly
    {param ([string]$Comp, [string]$Date, [string]$RunTime, [int]$DayOfWeek, [string]        $commandpath)

    $service = new-object -comobject "Schedule.Service"             # Forms object reference
    $service.Connect($Comp)                                         # Connects to host machine
    $rootFolder = $service.GetFolder("\")                       # Sets root folder of Task Scheduler
    $taskDefinition = $service.NewTask(0)                           # Begins task creation
    $regInfo = $taskDefinition.RegistrationInfo                     # Sets registry info
    $regInfo.Description = 'SCCM Check For Update'
    $regInfo.Author = "Administrator"
    $settings=$taskDefinition.Settings                              # Sets default settings for the new task
    $settings.StartWhenAvailable = $True
    $settings.Hidden = $False
    $triggers = $taskDefinition.Triggers                            # Creates Trigger object reference
    $trigger = $triggers.Create(3)                                  # Sets to create (Number 3 means Weekly trigger)
    # Start Time ex. 2012-6-10 22:00:00
    $trigger.StartBoundary = $Date + "T" + $RunTime                 # Sets start date (End date can be set) e
                                                            # ex. $trigger.EndBoundary = Date + "T" + Time
    $trigger.DaysOfWeek = $DayOfWeek                                # Sends integer for day of week to run on (1-7)
                                                            # 1 = Sunday, 2 = Monday, etc....
    $trigger.WeeksInterval = 1                                      # Sets to run every week
    $trigger.Id = "WeeklyTriggerId"                                 # Trigger ID name
    $trigger.Enabled = $True
    $Action = $taskDefinition.Actions.Create(0)
    $Action.Path = $commandpath                                         # Links to script
    $rootFolder.RegisterTaskDefinition('SCCM Check For Update', $taskDefinition, 6, "", "", 3)  # Registers scheduled task
    }

错误具体在这一行:         $ service = new-object -comobject“Schedule.Service”

它报告“无法加载COM类型Schedule.Service”

1 个答案:

答案 0 :(得分:1)

原因是你的本地方框是widows 7 server 2003没有comobject“Schedule.Service”它是Windows 7和2008的一部分。

如果您按照下面的链接,它将向您展示如何使用PowerShell在Windows 2003服务器上安排任务。

Schedules tasks on w2k3