Based on posts I have read about ScheduleTask(), the following code should work but it doesn’t and I do not know why. I am trying to schedule this task on the PC where I am ruining the script. I am getting error function “1” incorrect function although I logged on with administrator account. Please help. Thank you.
$name='test.job'
$comp='bc301828'
$date='TODAY'
$time='now'
$type='ONCE'
$user='administrator'
$pw='AbcN.16'
$comment='test'
$cmd='notepad'
$jt= 'D:\jt.exe'
scheduletask($name, $comp, $date, $time, $type, $cmd, $user, $pw, $comment, $jt)
? @SERROR
:END
Sleep 3
exit
Function scheduletask($name, $comp, $date, $time, $type, $cmd, optional $prms, optional $user, optional $pw, optional $comment, optional $typeargs, optional $jt)

Dim $jtexe, $shellcmd
If $name='' OR $comp='' OR $date='' OR $time='' OR $type='' OR $cmd=''
Exit 87
EndIf

$jtexe= 'D:\jt.exe'

$shellcmd = $jtexe
If $user AND $pw
$shellcmd = $shellcmd+ ' /SC "'+$user+'" "'+$pw+'"'
EndIf

$shellcmd = $shellcmd+ ' /SM '+$comp
$shellcmd = $shellcmd+ ' /SD "'+$name+'"'
Shell '%COMSPEC% /e:1024 /c '+$shellcmd+' >NUL 2>NUL'
$shellcmd = $jtexe

If $user AND $pw
$shellcmd = $shellcmd+ ' /SC "'+$user+'" "'+$pw+'"'
EndIf
$shellcmd = $shellcmd+ ' /SM '+$comp
$shellcmd = $shellcmd+ ' /SJ ApplicationName="'+$cmd+'"'
If $prms
$shellcmd = $shellcmd+ ' Parameters="'+$prms+'"'
EndIf
$shellcmd = $shellcmd+ ' WorkingDirectory="%SYSTEMROOT%"'
$shellcmd = $shellcmd+ ' Comment="'+$comment+'"'
$shellcmd = $shellcmd+ ' Creator="'+@userid+'"'
$shellcmd = $shellcmd+ ' Priority=Normal'
$shellcmd = $shellcmd+ ' MaxRunTime=3600000'
$shellcmd = $shellcmd+ ' DontStartIfOnBatteries=0'
$shellcmd = $shellcmd+ ' KillIfGoingOnBatteries=0'
$shellcmd = $shellcmd+ ' RunOnlyIfLoggedOn=0'
$shellcmd = $shellcmd+ ' SystemRequired=0'
$shellcmd = $shellcmd+ ' DeleteWhenDone=1'
$shellcmd = $shellcmd+ ' Suspend=0'
$shellcmd = $shellcmd+ ' StartOnlyIfIdle=0'
$shellcmd = $shellcmd+ ' KillOnIdleEnd=0'
$shellcmd = $shellcmd+ ' RestartOnIdleResume=0'
$shellcmd = $shellcmd+ ' Hidden=0'
$shellcmd = $shellcmd+ ' TaskFlags=0'
$shellcmd = $shellcmd+ ' /CTJ StartDate='+$date
$shellcmd = $shellcmd+ ' StartTime='+$time
$shellcmd = $shellcmd+ ' HasEndDate=0'
$shellcmd = $shellcmd+ ' KillAtDuration=0'
$shellcmd = $shellcmd+ ' Disabled=0'
$shellcmd = $shellcmd+ ' Type='+$type
If $typeargs
$shellcmd = $shellcmd+ ' TypeArguments='+$typeargs
EndIf
$shellcmd = $shellcmd+ ' /SAJ "'+$name+'"'
Shell '%COMSPEC% /e:1024 /c '+$shellcmd+' >NUL 2>NUL'
$scheduletask=@ERROR
Exit @ERROR
EndFunction