Register-Scheduledjob creates a job but it never runs at startup


I have tried 100 ways to register-scheduledjob to run a script that would start the program. nothing seems to be working consistently. my last attempt was using Register-ScheduledTask, this one does create the job but doesn't start it automatically at system startup. the funny thing is if I run it manually once, it would start on its own on future restarts. but if I wanted to run it once myself there is no point in scheduling the job!

this is what I am doing:

$description = "Scheduling job using Tasks" 
$taskPath = "\Microsoft\Windows\PowerShell\ScheduledJobs\" 
$argument = "'Register-ScheduledJob -Name "+$scheduledJobName+" -Trigger "+$trigger+" -FilePath "+$TaskParametersPath+" -ScheduledJobOption "+$option+" -Credential "+$cred+" -RunAs32 2>&1'" 

$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument $argument 
$taskTriger = New-ScheduledTaskTrigger -AtStartup -RandomDelay 00:00:30
$job = Register-ScheduledTask -Action $action -Trigger $taskTriger -TaskName $scheduledJobName -Description $description -TaskPath $taskPath

the original code I had was:

$job = Register-ScheduledJob -Name $scheduledJobName -Trigger $trigger -FilePath $TaskParametersPath -ScheduledJobOption $option -Credential $cred -RunAs32 2>&1

I also tried this one:

$job = Register-ScheduledJob -Name $scheduledJobName -Trigger $trigger -FilePath $TaskParametersPath -ScheduledJobOption $option -Credential $cred -Authentication Credssp 2>&1         

with trigger and option:

$trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:30

$option = New-ScheduledJobOption -RunElevated 

$taskParametersPath is a powershell script that starts another powershell script and passes all the required parameters for it. all of this works occasionally, but sometimes it fails to schedule the job with the error I have here. it works all the time when I am logged into the machine and try to register the job, but it fails occasionally when I run it as a part of VM creation script using azure stack templates. :( this is the error I get:

ERROR : FullyQualifiedErrorId :     CantRegisterScheduledJobDefinition,Microsoft.PowerShell.ScheduledJob.RegisterScheduledJobCommand
2018/06/01 23:41:47 : ERROR : Exception:Microsoft.PowerShell.ScheduledJob.ScheduledJobException: An error occurred while registering scheduled job definition VaaSRestartTask to the Windows Task Scheduler.  The Task Scheduler error is: The user name or password is incorrect. (Exception from HRESULT: 0x8007052E). ---> System.Runtime.InteropServices.COMException: The user name or password is incorrect. (Exception from HRESULT: 0x8007052E)
  at TaskScheduler.ITaskFolder.RegisterTaskDefinition(String Path, ITaskDefinition pDefinition, Int32 flags, Object UserId, Object password, _TASK_LOGON_TYPE LogonType, Object sddl)
  at Microsoft.PowerShell.ScheduledJob.ScheduledJobWTS.CreateTask(ScheduledJobDefinition definition)
  at Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition.AddToWTS()
  at Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition.Register()
  --- End of inner exception stack trace ---
  at Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition.Register()
  at    Microsoft.PowerShell.ScheduledJob.RegisterScheduledJobCommand.ProcessRecord()
2018/06/01 23:41:47 : ERROR : Category:InvalidOperation: (Microsoft.Power...edJobDefinition:ScheduledJobDefinition) [Register-ScheduledJob], ScheduledJobException
asked on Stack Overflow Jun 2, 2018 by Nadia • edited Jun 2, 2018 by briantist

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0