1

我在处理一个脚本时遇到了一个小问题。

代码本身运行良好;当我在本地工作站上运行它时,它是完美的。但是,当我在其中一台需要安装 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”

4

1 回答 1

1

原因是您的本地机器是 widows 7 server 2003 没有 comobject "Schedule.Service" 它是 windows 7 和 2008 的一部分。

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

在 w2k3 上安排任务

于 2012-08-07T14:27:34.753 回答