(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")