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