简体   繁体   中英

Why can't this script make a scheduled task event through Powershell script?

I've run into a small problem with a script I've been working on.

The code itself works perfectly fine; when I run it on my local workstation, it's perfect. However, when I run it on one of the servers (Win 2003 R2 32-bit) that need SCCM installed on it (which this script is designed to automatically install) it fails in one of my functions. Relevent code follows:

    # 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
    }

The error specifically is on this line: $service = new-object -comobject "Schedule.Service"

It reports that "Cannot load COM type Schedule.Service"

The reason is your local box is widows 7 server 2003 does not have the comobject "Schedule.Service" it is part of windows 7 and 2008.

If you follow the link below it will show you how to schedule a task on a windows 2003 server using powershell.

Schedules tasks on w2k3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM