2

(commands like Set-ScheduledTask work only on Powershell v4.0 which has the "ScheduledTasks" module builtin)

I'm running the below code but receiving an error. Could someone please help me understand where the problem is.

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

Code that receives user input and initializing dates for weekdays thereafter

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

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

Code to update

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
}

Calling the function

UpdateScheduledTask($startFriday10PM,"QLAT_WinPatching_Friday_10PM")
Matt
  • 45,022
  • 8
  • 78
  • 119
Kumar
  • 21
  • 3
  • 7
  • After the error mesaages,text that appear as headings are comments from powershell script.All the text starting from the first heading is the actual code. – Kumar Mar 12 '15 at 13:01
  • Function call is malformed. Should just be `UpdateScheduledTask $startFriday10PM "QLAT_WinPatching_Friday_10PM"` – Matt Mar 12 '15 at 13:39
  • Also since you are specifying the datatype of `$TriggerTime` there is not need to check to see what its type is. The function would fail otherwise. – Matt Mar 12 '15 at 13:53
  • Hi Matt,as you pointed out the function call i made was wrong.I got that corrected using http://stackoverflow.com/questions/4988226/how-do-i-pass-multiple-parameters-into-a-function-in-powershell – Kumar Mar 13 '15 at 05:49

1 Answers1

0

This error means that parameter 'TriggerTime' need to be of System.DateTime type.

In your code:

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

New-ScheduledTaskTrigger returns a new scheduled task trigger object but you pass variable of type Object to -Trigger parameter

Set-ScheduledTask -TaskName $ScheduleName -Trigger $Time 

which can take variables of System.DateTime type.

Kostia Shiian
  • 1,024
  • 7
  • 12
  • Hi Kostia,you make a very valid point and i was convinced about it too.However,the original issue was because of the wrong function call syntax that i made.I've updated the comments with the link that proved useful.And i continued using the $time variable for -Trigger parameter and surprisingly did not get any issues.Thanks for taking time to look into this and help. http://stackoverflow.com/questions/4988226/how-do-i-pass-multiple-parameters-into-a-function-in-powershell – Kumar Mar 13 '15 at 05:52