调用函数时出错 - 无法在参数'TriggerTime'上处理参数转换

时间:2015-03-12 12:57:40

标签: powershell

Set-ScheduledTask之类的命令仅适用于内置“ScheduledTasks”模块的Powershell v4.0

我正在运行以下代码但收到错误。有人可以帮我理解问题所在。

SetScheduledTask : Cannot process argument transformation on parameter 'TriggerTime'. Cannot convert the "System.Object[]" value 
of type "System.Object[]" to type "System.DateTime".
At C:\Users\skadithi\Desktop\Script.ps1:33 char:17
+ SetScheduledTask([datetime]$startFriday10PM,"QLAT_WinPatching_Friday_10PM")
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [SetScheduledTask], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,SetScheduledTask

接收用户输入并在之后的工作日初始化日期的代码

$startDate=Read-Host "Enter the StartDate in MM/DD/YYYY format"
$startDate=[datetime]$startDate

$startFriday=$startDate.AddDays(1)
$startFriday10PM=$startFriday.AddHours(22)

要更新的代码

Function UpdateScheduledTask
{
[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True)]
  [datetime]$TriggerTime,

   [Parameter(Mandatory=$True)]
   [string]$ScheduleName
     )
    if($TriggerTime -is [DateTime])
    {
        Write-Host "Running on $TriggerTime" -ForegroundColor Green
    }
    else
    {
        Write-Host "Event date must be a DateTime object" -ForegroundColor Yellow
    }
    $Time = New-ScheduledTaskTrigger -At $TriggerTime -Once
    Set-ScheduledTask -TaskName $ScheduleName -Trigger $Time
}

调用函数

UpdateScheduledTask($startFriday10PM,"QLAT_WinPatching_Friday_10PM")

1 个答案:

答案 0 :(得分:0)

此错误表示参数' TriggerTime'需要是System.DateTime类型。

在您的代码中:

$Time = New-ScheduledTaskTrigger -At $TriggerTime -Once 

New-ScheduledTaskTrigger返回一个新的计划任务触发器对象,但是将Object类型的变量传递给-Trigger参数

Set-ScheduledTask -TaskName $ScheduleName -Trigger $Time 

可以采用System.DateTime类型的变量。