Ian M
Ian M

Reputation: 25

windows schedule task could not successfully run at startup

I use a script to create a windows scheduled task to call a powershell script in elevated mode to run windows update by using boxstarter (a tool could automatically continue running code even there is reboot during the execution) when system startup. But not sure why, the task could be called after startup, but nothing has been done. If I manually start the scheduled task in task manager, it will run as expected.

Script to register a scheduled task:

$TaskActionArgument ="-noprofile -command "&{start-process powershell -argumentList '-File C:\users\administrator\updatescript\boxstarter.ps1 -verb runas'}""
$TaskAction = New-ScheduledTaskAction -Execute "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" -argument $TaskActionArgument
$TaskTrigger = New-ScheduledTaskTrigger -AtStartup
Register-ScheduledTask -TaskName boxstarter -Action $TaskAction -Trigger $TaskTrigger -User administrator -Password Vmc12svt -RunLevel Highest

I checked the event log viewer and see following error message for the scheduled job:

  • System

  • Provider

    [ Name] PowerShell

  • EventID 403

    [ Qualifiers] 0

    Level 4

    Task 4

    Keywords 0x80000000000000

  • TimeCreated

    [ SystemTime] 2018-01-10T18:21:12.000000000Z

    EventRecordID 267

    Channel Windows PowerShell

    Computer WIN-6HSHKOKP31E

    Security

    • EventData

    Stopped Available NewEngineState=Stopped PreviousEngineState=Available SequenceNumber=16 HostName=ConsoleHost HostVersion=4.0 HostId=13ece112-b027-4051-9ddf-1a195d3aa30f HostApplication=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File C:\users\administrator\updatescript\boxstarter.ps1 -verb runas EngineVersion=4.0 RunspaceId=d158a216-18e3-4e86-9ade-b232201c9cdc PipelineId= CommandName= CommandType= ScriptName= CommandPath= CommandLine=

For the error message, I googled and found explain of error code here

In general, the page says such issue could be caused by following error:

Non of above is true for my system.

So what's error with my scheduled task? How could I make it work?

Upvotes: 1

Views: 1520

Answers (2)

Ctznkane525
Ctznkane525

Reputation: 7465

It looks like all your services are not setup. It's a common problem on things that run on startup/login. There's a -RandomDelay parameter to New-ScheduledTaskTrigger. I recommend you tinker with that if its your own machine you are testing this with. My example uses 1 minute.

$TaskTrigger = New-ScheduledTaskTrigger -AtStartup -Delay (New-TimeSpan -Minutes 1)

If you want a minute or so, all the services needed should be started up by then.

Another thing you'll want to do is have the your code in a try/catch, so the error is being written out to a log file, so you can see the error in the context of PowerShell, which might provide a more detailed message than what you are getting in the event log.

Upvotes: 1

Bill_Stewart
Bill_Stewart

Reputation: 24515

You can troubleshoot this by creating the scheduled task manually and then trying to run it.

Try changing the TaskActionArgument property to only the following string:

-NoProfile -ExecutionPolicy Bypass -File C:\users\administrator\updatescript\boxstarter.ps1

You don't need Start-Process or -Verb runas. In fact, -Verb runas will actually keep things from working, because it provokes a UAC prompt, which you don't want when trying to automate.

Upvotes: 0

Related Questions