I have two PS scripts created. They use New-ScheduledTask and corresponding commands to create two scheduled tasks. The script worked and scheduled tasks are created fine. However, I noticed that next run time always with one more hour added on top of the set time. The tasks are scheduled to run at 12:30am and 12:45am in two different scripts, and both their Next Run Time shown as 1:30am and 1:45am instead of keeping the set times in the scripts. Tried to run the scripts to remove and add the scheduled tasks multiple times, and still the same. Any idea, why?
I will include one of the scripts here. Hope you can help find the trick. Please see the following.
Set the path for the log file
$logFilePath = "C:\IT\Intune-Script.log"
Check if the scheduled task already exists
$scheduledTaskName = "SFC Scan Weekly"
$existingTask = Get-ScheduledTask -TaskName $scheduledTaskName -ErrorAction SilentlyContinue
if ($existingTask -ne $null) {
# Task already exists, so attempt to remove it
try {
Unregister-ScheduledTask -TaskName $scheduledTaskName -Confirm:$false -ErrorAction Stop | Out-File -FilePath $logFilePath -Append
Write-Output "Existing task '$scheduledTaskName' successfully removed."
} catch {
Write-Output "Error removing existing task '$scheduledTaskName': $_" | Out-File -FilePath $logFilePath -Append
}
}
Set Trigger, Task, Settings
$SFCTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Wednesday -At 00:30
$SFCAction = New-ScheduledTaskAction -Execute "SFC" -Argument "/SCANNOW"
$SFCSetting = New-ScheduledTaskSettingsSet -Compatibility Win8 -AllowStartIfOnBatteries -StartWhenAvailable -DontStopIfGoingOnBatteries
$SFCTask = New-ScheduledTask -Trigger $SFCTrigger -Action $SFCAction -Settings $SFCSetting -Description "SFC Scan Weekly"
Create/Register Scheduled Task
try {
Register-ScheduledTask -TaskPath RASULC -TaskName $scheduledTaskName -InputObject $SFCTask -User "NT AUTHORITY\SYSTEM" -ErrorAction Stop | Out-File -FilePath $logFilePath -Append
Write-Output "Scheduled task '$scheduledTaskName' successfully registered."
} catch {
Write-Output "Error registering scheduled task '$scheduledTaskName': $_" | Out-File -FilePath $logFilePath -Append
}