Schedule Tasks

I’m running into a problem where I’m writing an “Onboarding” script for Windows 10 and using Schedule Tasks to run the next PoSh script after a reboot and auto login. It works fine if it never joins the domain which defeats the purpose. Below is the error I am getting with the code

function Invoke-PrepareScheduledTask
{
$actionscript = '-NonInteractive -WindowStyle Normal -NoLogo -NoProfile -NoExit  -Command "&''c:\temp\onboarding\ps\restart.ps1''"'

$pstart =  "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"

$act = New-ScheduledTaskAction -Execute $pstart -Argument $actionscript
 
$trig = New-ScheduledTaskTrigger -RandomDelay (New-TimeSpan -Minutes 5) -AtLogon

Get-ScheduledTask -TaskName joindomain | Unregister-ScheduledTask -Confirm:$false

Register-ScheduledTask -TaskName "restart" -Action $act -Trigger $trig -User $encrypteduser -Password $convertedpw
}

Invoke-PrepareScheduledTask

When it fires this off I get the below error. The computer joins the domain as it logins after the reboot automatically under the domain account

Register-ScheduleTask : The security database on the server does not have a computer account for this workstation trust relationship.

+Register-ScheduleTask -TaskName "restart" -Action $act -Trigger $tri....

+CategoryInfo  : NotSpecified (PS_ScheduleTask:Root/Microsoft/...S_ScheduleTask)
+FullQualifyErrorId: HRESULT 0x800706fb, Register-ScheduleTask

That’s because after a computer joins a domain, it has a different identity. That’s a design feature of Windows; you’ll probably have to find some other way around the problem.