attiahia
(Hey THIS is FUN)
2004-03-06 01:53 PM
ScheduleTask()

I am quite confused with the Jt.exe potential file paths part in ScheduleTask() function.

If I am sure of JT.exe location, can I replace the following part of the function:

; create an array of potential filepaths to jt.exe
$jtexe[0]=$jt
$jtexe[1]=@STARTDIR+'\jt.exe'
$jtexe[2]=@SCRIPTDIR+'\jt.exe'
$jtexe[3]=@CURDIR+'\jt.exe'
$path=split('%PATH%',';')
for each $jt in $path
redim preserve $jtexe[ubound($jtexe)+1]
$jtexe[ubound($jtexe)]=join(split($jt+'\jt.exe','\\'),'\')
next

; check each filepath for the presence of jt.exe
for each $jt in $jtexe
if exist($jt) and not (getfileattr($jt) & 16) and ubound($jtexe)+1
$jtexe=$jt
endif
next
select
case ubound($jtexe)+1
exit 2
case not exist($jtexe)
exit 2
endselect


with just:
$JT=’@curdir\jt.exe’


Kdyer
(KiX Supporter)
2004-03-06 03:38 PM
Re: ScheduleTask()

Why not do it simpler, you doing an array..
Code:

IF EXIST(@SCRIPTDIR+'\jt.exe')
;DO SCHEDULING
ENDIF



Let's look at the definitions of the macros you list:
@STARTDIR > Directory from which KiXtart was started
@SCRIPTDIR > Directory of current script
@CURDIR > Current directory

In the example you provided, these seem redundant.

Kent


Sealeopard
(KiX Master)
2004-03-06 04:07 PM
Re: ScheduleTask()

I do not recommed making changes to the UDF. There's a reason why the UDF looks in different directories for the JT.EXE executable.

Secondly, I'm not going to support UDFs that I wrote and where somebody else made unauthorized chanegs to it. You can always request changes/improvements.

Quote:


If I am sure of JT.exe location, can I replace the following part of the function:




You can alwasy provide the path to your copy of JT.EXE as part of the UDF parameters.

Is it not working for you?


Co
(MM club member)
2004-03-07 12:44 AM
Re: ScheduleTask()

Jens,

It works for me since a long time. And I agree with you...


attiahia
(Hey THIS is FUN)
2004-03-07 07:17 AM
Re: ScheduleTask()

Thanks lot gentlemen for your kind support.
For beginner like me (or even experts) simplify code usually is one of the criteria at building script. Therefore, I am just thinking what if we instead of this part (checking JT.exe existence) which contain about 20 lines of code, modify the script to generate error message if JT.exe was not found in the @Curdir then programmer will easily add the file.
Simplify code will allow easer debugging.


LonkeroAdministrator
(KiX Master Guru)
2004-03-07 10:34 AM
Re: ScheduleTask()

that's weird.
how many newbie programmers you think go and mod the libraries done by MS?
I know, they are cryptic as the code I produce, but anyway.

the base line is, if you have something proven working, don't mess with it.
if your code does fail, don't mess with it. trust it.

you are just asking for problems if you go and tweak the UDFs.
well, most of them.

there is still some UDFs (even by moderators) that don't qualify as UDFs as they require you to tweak them to make them work.
won't name anybody here as have personally complained to them multiple times already.

you were already told how to call the UDF with specified path.
you can use your curdir (which I never ever use btw) there.
after the function-call, you can easily put if @error "error found" endif line.

it's so simple I don't see any reason but stubberness forcing you to insist tweaking and braking the code.


Sealeopard
(KiX Master)
2004-03-07 03:05 PM
Re: ScheduleTask()

Also, UDFs are supposed to be generalized functions that work in all kinds of circumstances. Not everybody puts, e.g. JT.EXE into the same directory as the script. Actualy, I'd consider it a best practive NOT to have a executable such as JT.EXE in the same directory as a KiXtart script.

You're welcome to modify UDFs on your own. However, as soon as you ask for help on such modified UDFs, I for example ahve to go throguht teh compleet UDF and trace down all the modifications you've potentially made and how they affect the UDF. And quiet frankly, I don't have time for such things. If you have a request for improvement, post it, and I'll incorporate it into the UDF.


Co
(MM club member)
2004-03-07 03:37 PM
Re: ScheduleTask()

Quote:

...You're welcome to modify UDFs on your own. However, as soon as you ask for help on such modified UDFs, I for example ahve to go throguht teh compleet UDF and trace down all the modifications you've potentially made and how they affect the UDF. And quiet frankly, I don't have time for such things. If you have a request for improvement, post it, and I'll incorporate it into the UDF.





@attiahia

First: Like Jens has said "You're welcome to modify UDFs and scripts on your own". That isn't the problem. If it is we shouldn't post our scripts/UDFs in this Forum.

Please tell us when you have changed an UDF and tell us what you have changed. (like you did ). When we know this we can pay attention to it...

If you have changed a lot you can give the the UDF another name like ScheduleTask2().

Good luck...





Glenn BarnasAdministrator
(KiX Supporter)
2004-03-07 04:23 PM
Re: ScheduleTask()

Well, Lonk, for some of us, reading, understanding, and modifying code from others is just another way to learn. Don't you think we would do the same if we had access to the O/S source? Maybe we'd close some Windows buffer overrun holes! I had access to the Unix SVR2 source and was credited with fixing a bug in the terminal session init code (WAY too many years ago).

Back to the topic...

The short answer to the original question is "yes", replacing the source determination code with a fixed path will work. In fact, I've done this and simply reference "JT.exe" since it is available via the system PATH or it isn't installed in my environment.

Jens appears to have written this piece of code to account for other people installing the JT executable in various locations. Since he isn't clarvoyant and can't know where you've installed it OR assume it's available via the PATH, this code searches the most common locations to insure it is available. It is an example of good coding when you are writing for a universal audience.

Changing is best done by commenting out the original code (or - heaven forbid!! temporarily using a GOTO to jump over it) and adding your own code changes. Follow this with lots of testing!! You should be aware that when you modify someone else's code, they shouldn't be expected to help you debug it. But by all means, tear it apart and rewrite it. If we all didn't do that, we wouldn't improve each others code - and we'd all close our minds to new ideas.

Finally, if you make changes that add functionality, or potentially improve performance or reliability, post your chanegs for discussion and review in the Scripts topic with an explaination of what you did.

Glenn


Co
(MM club member)
2004-03-07 09:20 PM
Re: ScheduleTask()

Quote:

Well, Lonk, for some of us, reading, understanding, and modifying code from others is just another way to learn....




Well said... Since I'm not a programmer I have used and still use this tactics to become a better scripter/programmer. I also use it to compare my own code with someone else his code. Sometimes you are in the wrong track. Comparing codes can save lots of time .

Still I agree with Jens. Since lots of UDF's are containing Jens's name we can not expect him to support all those UDFs. surely when they are modified..

Quote:

...Finally, if you make changes that add functionality, or potentially improve performance or reliability, post your chanegs for discussion and review in the Scripts topic with an explaination of what you did.




This is the way to discuss changes.... When changes are accepted version numbers must change too.. just like in the real world


LonkeroAdministrator
(KiX Master Guru)
2004-03-08 06:28 AM
Re: ScheduleTask()

well well...
who has talked about providing OS code?
talking about basic coding libraries that come with normal programming tools.
and I know, ppl tend to mod these too, but surely NOT ONE NEWBIE even looks in them as they are pure hebrew to them.

sure, it's different with scripting as basically it is lots simpler.
sure, you can go and screw things up to learn.

but if you want to make working code, you observe and leave the original source untouched!
that's the basic first rule, if you are to do your own stuff, do it but modding original code does not make it yours.
heh, basic way would be to mod just the author tag.

what comes to inventing...
you surely kill invention if you go and learn everything by studying from other ppl's codes.
this way, your mind becomes like theirs and your brains became tracked.


Co
(MM club member)
2004-03-08 04:05 PM
Re: ScheduleTask()

Quote:

...your mind becomes like theirs...




Ohooh!! lets hope people don't use your posts to much


attiahia
(Hey THIS is FUN)
2004-03-10 07:12 AM
Re: ScheduleTask()

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


Co
(MM club member)
2004-03-10 08:23 AM
Re: ScheduleTask()

Strange...

When I compare your script with one of my own. The only thing which is different is the name of the task. Leave .job, just call it test...

http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Board=UBB2&Number=110124&Forum=UBB2&Words=array&Match=Entire%20Phrase&Searchpage=1&Limit=25&Old=6months&Main=109672&Search=true#Post110124


attiahia
(Hey THIS is FUN)
2004-03-10 10:00 AM
Re: ScheduleTask()

Thanks lot Co for advise, I already did this action but was giving the same error. (I even change Jt.exe location).
Maybe I am still missing some thing . Please help. thank you.
Note: OS is XP


ShaneEP
(MM club member)
2004-03-10 08:27 PM
Re: ScheduleTask()

Do you have to maybe put a machine or domain in front the username? I know from my own experiences that if the authentication fails...It will return the error you specified. I struggled with it for hours before I realised I had simply mistyped a password.

Co
(MM club member)
2004-03-10 08:39 PM
Re: ScheduleTask()

Use the original UDF:

Code:

; Function scheduletask
Function scheduletask($name, $comp, $date, $time, $type, $cmd, optional $prms, optional $user, optional $pw, optional $comment, optional $typeargs, optional $jt)
Dim $jtexe[3], $shellcmd, $path

If $name='' OR $comp='' OR $date='' OR $time='' OR $type='' OR $cmd=''
Exit 87
EndIf

; create an array of potential filepaths to jt.exe
$jtexe[0]=$jt
$jtexe[1]=@STARTDIR+'\jt.exe'
$jtexe[2]=@SCRIPTDIR+'\jt.exe'
$jtexe[3]=@CURDIR+'\jt.exe'
$path=Split('%PATH%',';')
For Each $jt In $path
ReDim preserve $jtexe[Ubound($jtexe)+1]
$jtexe[Ubound($jtexe)]=Join(Split($jt+'\jt.exe','\\'),'\')
Next

; check each filepath for the presence of jt.exe
For Each $jt In $jtexe
If Exist($jt) AND NOT (GetFileAttr($jt) & 16) AND Ubound($jtexe)+1
$jtexe=$jt
EndIf
Next
Select
Case Ubound($jtexe)+1
Exit 2
Case NOT Exist($jtexe)
Exit 2
EndSelect

If Left($comp,2)<>'\\'
$comp='\\'+$comp
EndIf

If Right($name,4)<>'.job'
$name=$name+'.job'
EndIf

; delete a potentially existing task
$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



Glenn BarnasAdministrator
(KiX Supporter)
2004-03-10 11:24 PM
Re: ScheduleTask()

Try this UDF modification to the end if the file to test it:
Code:

; don't run the command
;Shell '%COMSPEC% /e:1024 /c '+$shellcmd+' >NUL 2>NUL'
$ = RedirectOutput('jtcmd.bat')
$shellcmd ?
$ = RedirectOutput('')


This will place the complete JT command syntax into a BAT file that you can edit. Edit the file and review the syntax, making sure that commands don't run together (have proper spaces between them), are all on one line (turn line-wrap OFF), and maybe even review the syntax for correctness. If you download the zip file for my tsControl library, there's a JT doc file that better describes the functions and arguments of JT.

Finally, after validating the bat file, execute it and see if any other errors are reported. There may be a "JT.exe is not a recognized command" if it can't find the file. You may see other errors from JT.

This process will help you isolate whether the UDF is not functioning, if it is not properly generating JT syntax, or JT itself is failing. The UDF has been well tested, so - other than your (supposedly) minor modification, the only other errors could be introduced when you downloaded it. Some spaces may not be present, affecting the JT syntax generation or something similar. Until you can begin to narrow it down, you're shooting in the dark.

Glenn


attiahia
(Hey THIS is FUN)
2004-03-13 09:57 AM
Re: ScheduleTask()

Glenn

Thanks for the valuable advice. I was in sick live for the last 2 days. It is working from bat file while it doesn’t from the script.
I will work on finding out the reason and update the post.


CitrixMan

You are right, domain or machine name should precede the user ide.

Thank you very much.





attiahia
(Hey THIS is FUN)
2004-03-14 09:45 AM
Re: ScheduleTask()

All what I hade to do is changing JT location and then it worked.
Now I came across with other authorization problem, your help is appreciated.
The function\jt will pick up the current existing user id if you omitted it; is there any way to do the same thing for password also?

Through an official request to access control group I have created a “service account”. Before modifying the login script to add this account automatically to the power user group on all the user workstations, I added it manually to my PC then (while I already logged on with my own user id attiahia) I run the script with the service account & its password. Job has created but does not execute. Should the service account have admin privilege?

Thank you for any help.


LonkeroAdministrator
(KiX Master Guru)
2004-03-14 10:52 AM
Re: ScheduleTask()

if it is there, it already has rights to add scheduled jobs.
you should check what the error is.


Co
(MM club member)
2004-03-14 11:04 AM
Re: ScheduleTask()

Yeah, but maybe no permissions to execute them...

LonkeroAdministrator
(KiX Master Guru)
2004-03-14 11:32 AM
Re: ScheduleTask()

well...
haven't herd there is separate scheduler permissions for exec and add and maybe even delete.
but, the file rights might be worth checking.
once again, the error recorded would simply clear the clouds and remove the need for quessing.


attiahia
(Hey THIS is FUN)
2004-03-14 01:14 PM
Re: ScheduleTask()

Error is: [FAIL ] ITaskScheduler::AddWorkItemn hr=0x80070005

As I mentioned, job created but it does not execute at the specified time. Furthermore, when I check the job setting in GUI schedule task I found the Run as setting is still attiahia which is the user id I used to login to the machine.

Below is the bat file were redirect output (as Glenn helpful advice)

D:\>D:\jt.exe /SC "Domain Name\Service Account" "Service.123" /SM \\bc301828 /SJ ApplicationName="notepad" Parameters="d:\test.txt" WorkingDirectory="C:\WINDOWS" Comment="cool!" Creator=" Service Account " Priority=Normal MaxRunTime=3600000 DontStartIfOnBatteries=0 KillIfGoingOnBatteries=0 RunOnlyIfLoggedOn=0 SystemRequired=0 DeleteWhenDone=1 Suspend=0 StartOnlyIfIdle=0 KillOnIdleEnd=0 RestartOnIdleResume=0 Hidden=0 TaskFlags=0 /CTJ StartDate=TODAY StartTime=14:46 HasEndDate=0 KillAtDuration=0 Disabled=0 Type=ONCE /SAJ "Exam.job"

[TRACE] Setting account information
[TRACE] Setting target computer to '\\bc301828'
[TRACE] Setting job's properties
[TRACE] Created trigger 0
[TRACE] Adding job 'Exam.job'
[FAIL ] ITaskScheduler::AddWorkItemn hr=0x80070005

Thank you


Co
(MM club member)
2004-03-14 02:47 PM
Re: ScheduleTask()

I think you don't start the job with the service account but with the logged in useraccount... You have to fill in username and password

you can test this by giving your own account admin credentials... When the job starts this time it means you don't use the service account.

Still my opinion is to use the original UDF. Give it a try. I have used it a lot of times and never had problems with it...

Code:
Break On


$name='sync_scripts_dir'
$comp='comp1'
$date='TODAY'
$time='17:30'
$type='ONCE'
$cmd='\\comp1\Scripts\kix\sync_scripts_dir.exe'
;$prms=''
$user='admin'
$pw='adminpw'
;$comment='test'
;$typeargs=''
;$jt= 'c:\jt.exe'
scheduletask($name, $comp, $date, $time, $type, $cmd, $user, $pw)
? 'Error '+@ERROR+' - '+@SERROR

Exit


;FUNCTION ScheduleTask
;
;ACTION Schedules a task on any computer using the Task Scheduler
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;VERSION 1.41 (corrected typos in the date checker)
; 1.4 (added support for both MM/DD/YYY and YYYY/MM/DD date formats
; 1.3 (made username/password/comment optional parameters, changed parameter order, added
; optional parameter for JT.EXE location, checks %PATH% for presence of JT.EXE)
; 1.2 (removed error code trigger for deleting non-existing tasks, changed order of
; parameters that create task, updated header with regards to quote usage, added
; optional typearguments)
; 1.1 (added error codes, streamlined code)
; 1.0
;
;DATE CREATED 2001/12/04
;
;DATE MODIFIED 2004/01/06
;
;KIXTART 4.12+
;
;SYNTAX SCHEDULETASK($NAME,$COMP,$DATE,$TIME,$TYPE,$CMD [,$PRMS,$USER,$PW,$COMMENT,TYPEARGS,JT])
;
;PARAMETERS NAME
; name of the task
;
; COMP
; computername on which the task is to be run, computer must have the Task Scheduler installed
;
; DATE
; date on which to run the task (MM/DD/YYYY, YYYY/MM/DD, or TODAY)
;
; TIME
; time on which to run the task (H:M or NOW) with NOW indicating 60 seconds in the future
;
; TYPE
; trigger types (DAILY n | WEEKLY n, | MONTHLYDATE , | MONTHLYDOW n,,months |
; ONCE | ONIDLE | ATSTARTUP | ATLOGON)
;
; CMD
; the command to be executed by the task scheduler, cannot contain the quote character "
;
; USER
; userid under which to run the task (can be any valid local or domain user). If no userid
; is provided then the current userid is being used
;
; PW
; password for the userid
;
; PRMS
; optional parameters that are provided to the command, cannot contain the quote character "
;
; COMMENT
; comment field with information about task, cannot contain the quote character "
;
; TYPEARGS
; Optional parameter for trigger types, see jt /? ctj for details
;
; JT
; optional string denoting the location of JT.EXE, by default the UDF checks for JT.EXE
; in @STARTDIR, @SCRIPTDIR, @CURDIR, and %PATH%. @SCRIPTDIR will point to the location of the file
; containing the ScheduleTask UDF which does not necessarily need to be in the same directory
; as the actual script.
;
;RETURNS 0 if successful, otherwise error code
;
;REMARKS Requires the file JT.EXE, which is part of the Microsoft Windows 2000 Resource Kit Supplement
; and can be downloaded at ftp://ftp.microsoft.com/reskit/win2000/jt.zip
; Best used to schedule batch files due to problems with double quote usage in some of the JT.EXE parameters.
; Administrative privileges on the remote computer are required in order to schedule a task on the remote computer
; User environment variables like %USERNAME% or %LOGONSERVER% are not available in the scheduled tasks. System-wide
; environmentvariables like %COMSPEC% or %WINDIR% can be used, however.
;
;DEPENDENCIES 'Task Scheduler' service, can be installed via IE5.5 'Offline Browsing Pack' component under Windows NT 4.0
;
;EXAMPLE $name='demo.job'
; $comp='SERVER'
; $date='TODAY'
; $time='NOW'
; $type='ONCE'
; $cmd='notepad'
; $prms='%TEMP%\test.txt'
; $user='administrator'
; $pw='adminpassword'
; $comment='cool!'
; $typeargs=''
; $rc=scheduletask($name,$comp,$date,$time,$type,$cmd,$prms,$user,$pw,$comment,$typeargs,$jt)
;
;KIXTART BBS http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Board=UBB12&Number=81907
;
function scheduletask($name, $comp, $date, $time, $type, $cmd, optional $prms, optional $user, optional $pw, optional $comment, optional $typeargs, optional $jt)
Dim $jtexe[3], $shellcmd, $path

if $name='' or $comp='' or $date='' or $time='' or $type='' or $cmd=''
exit 87
endif

; create an array of potential filepaths to jt.exe
$jtexe[0]=$jt
$jtexe[1]=@STARTDIR+'\jt.exe'
$jtexe[2]=@SCRIPTDIR+'\jt.exe'
$jtexe[3]=@CURDIR+'\jt.exe'
$path=split('%PATH%',';')
for each $jt in $path
redim preserve $jtexe[ubound($jtexe)+1]
$jtexe[ubound($jtexe)]=join(split($jt+'\jt.exe','\\'),'\')
next

; check each filepath for the presence of jt.exe
for each $jt in $jtexe
if exist($jt) and not (getfileattr($jt) & 16) and ubound($jtexe)+1
$jtexe=$jt
endif
next
select
case ubound($jtexe)+1
exit 2
case not exist($jtexe)
exit 2
endselect

if left($comp,2)<>'\\'
$comp='\\'+$comp
endif

if right($name,4)<>'.job'
$name=$name+'.job'
endif

select
case $date='TODAY'
; valid format
case len($date)<>10
exit 87
case substr($date,3,1)='/'
; valid format
case substr($date,5,1)='/'
; reformat date
$date=split($date,'/')
$date=$date[1]+'/'+$date[2]+'/'+$date[0]
case 1
exit 87
endselect

; delete a potentially existing task
$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




Sealeopard
(KiX Master)
2004-03-14 03:21 PM
Re: ScheduleTask()

Yes, you have to supply both[/b[] username and password, otherwise the account that runs the ScheduleTask() script will be used. There is no way that the password for a username can be retreived automatically in any way, unless you crack the passwords hash value or brute-force generate the password.

attiahia
(Hey THIS is FUN)
2004-03-24 01:51 PM
Re: ScheduleTask()

Lady & Gentlemen
I have received a great support from Glenn and you and I can say 80 % of the problem has been solved.

The job has been scheduled under the service account but here is the strange different.

When I schedule the task under my account which I used to logon, the task executed at the specified time and NOTEPAD.exe appear to me but when I logon with my account and schedule the task with the service account it still work & executed at the specified time but NOTEPAD.exe does not appear to me.
In the task statues I can see that task is running and NOTEPAD processor appear within windows task manager\active processors.

I have tried to add Interactive parameter but also did not work. Please help. Thank you.


D:\jt.exe /SC "Domain\ServiceAccount" "password" /SM \\bc301828 /SJ ApplicationName="NOTEPAD" Parameters="C:\WINDOWS\NOTEPAD" WorkingDirectory="C:\WINDOWS\NOTEPAD" Comment=" Service Account Test" Creator="Domain\attiahia" Priority=Normal MaxRunTime=3600000 DontStartIfOnBatteries=0 KillIfGoingOnBatteries=0 RunOnlyIfLoggedOn=0 SystemRequired=0 DeleteWhenDone=1 Suspend=0 StartOnlyIfIdle=0 KillOnIdleEnd=0 RestartOnIdleResume=0 Hidden=0 TaskFlags=0 /CTJ StartDate=TODAY StartTime=15:23 HasEndDate=0 KillAtDuration=0 Disabled=0 Type=ONCE /SAJ "JT1a.job"


Glenn BarnasAdministrator
(KiX Supporter)
2004-03-24 03:16 PM
Re: ScheduleTask()

I'm thinking that this is normal and expected behavior. An "interactive" event would only appear/interact with the event owner. Appearing on any other users desktop would be a potential (and possibly severe) security violation. Imagine if you scheduled (via an admin account) the user manager for domains to open interactively. If "Joe Schmoe" was logged in, he would be able to create any level of user id or group. An extreme idea, but it illustrates the point of why this would not be possible.

Glenn


Sealeopard
(KiX Master)
2004-03-24 04:56 PM
Re: ScheduleTask()

Yes, that is expected and correct behavior. You can only interact with tasks scheduled under the same account that is logged in interactively to the computer.

attiahia
(Hey THIS is FUN)
2004-03-27 08:53 AM
Re: ScheduleTask()

This is not fair from JT and I am still hope that there is a trick, solution or alternative for this situation.
All what I need is to schedule a job that presenting employees with a power point slides at specific time.
your help is appreciated. Thank you.


Co
(MM club member)
2004-03-27 12:11 PM
Re: ScheduleTask()

It isn't JT it is the Task Scheduler Service...

ShaneEP
(MM club member)
2004-03-27 09:51 PM
Re: ScheduleTask()

If you are only wanting to show a PPT slide at a specific time then why do you need to schedule it with an admin account? Why dont you just let it run under the user thats logged in?

Sealeopard
(KiX Master)
2004-03-28 12:04 AM
Re: ScheduleTask()

What's up with showing the PPT slide anyway while a user is working? I'd find that annoying and intrusive.

Also, the described behavior exists for (valid) security reasons.


Co
(MM club member)
2004-03-28 01:02 AM
Re: ScheduleTask()

...Or show it when a user turns his computer on. Use your loginscript to start the presentation. You don't disturb your users yet while they are working...

Sealeopard
(KiX Master)
2004-03-28 03:27 PM
Re: ScheduleTask()

Actually, my and many other user's fiurst thought would be that there's a virus/worm/??? loose on my computer if such events happen that were not specifcally initiated by myself. I'd also start building a huge distrust toward the IT department as thay seem to have the ability to do things to my computer while I'm working on it. Remember, not everybody is IT-savy and you should evaluate how a standard user who's able to use Word but doesn't understand much else will see this.

attiahia
(Hey THIS is FUN)
2004-03-30 02:31 PM
Re: ScheduleTask()

Shane:
This what I was doing but it is not practical because the PPS slide appear to the user when he login but since most of our employees keep their
Workstations only locked off without logoff & logon for long time (can be a week), I found that I need to schedule executing this PPS slide on their BC’s.
The group policy on our main domain does not force logoff & logon and it is very difficult for me to ask for changing this policy.

CO:
If I placed the slide\script on the computer shutdown, no one will see it because they (if they decide to turn off the computer after long time) will simply Ctrl + Alt + Del and select shutdown and leave the office.

Jens:
It is a 20 seconds slide and we want to schedule it to appear to the employees every couple of days.
They are about 12 slides, each one show a success story for one of our employees who added value to the company.


Now,
I have faced another problem with JT and please do not tell me it is expected behavior:

All what I want to do is to set the EndDate parameter for the task.
The following code worked successfully under my account that I used to login but it did not when I login with my account and use the service account to schedule the task.

D:\jt.exe /SC "Domain Name\service account" "password.123" /SM \\bc301828 /SJ ApplicationName="Ping1" Parameters="C:\WINDOWS\Ping1" WorkingDirectory="C:\WINDOWS\Ping1" Comment="hhhhhh" Creator="domain name\attiahia" Priority=Normal MaxRunTime=3600000 DontStartIfOnBatteries=0 KillIfGoingOnBatteries=0 RunOnlyIfLoggedOn=0 SystemRequired=0 DeleteWhenDone=1 Suspend=0 StartOnlyIfIdle=0 KillOnIdleEnd=0 RestartOnIdleResume=0 Hidden=0 TaskFlags=0
/CTJ StartDate=03/30/2004 EndDate=04/05/2004 StartTime=11:03 HasEndDate=1
KillAtDuration=0 Disabled=0
Type=Daily /SAJ "Task1.job"

[TRACE] Setting account information
[TRACE] Setting target computer to '\\bc301828'
[TRACE] Setting job's properties
[TRACE] Created trigger 0
[TRACE] Setting properties on trigger 0
[TRACE] Adding job 'Task1.job'
[FAIL ] ITaskScheduler::AddWorkItemn hr=0x80070005


so when I keep the same code but with replacing the service account with my account that I am currently logged in with , it will work. Please help.

I have tried to use /STJ EndDate=04/05/2004 but also did not work.


ShaneEP
(MM club member)
2004-03-30 11:53 PM
Re: ScheduleTask()

Quote:

Shane:
This what I was doing but it is not practical because the PPS slide appear to the user when he login but since most of our employees keep their
Workstations only locked off without logoff & logon for long time (can be a week), I found that I need to schedule executing this PPS slide on their BC’s.
The group policy on our main domain does not force logoff & logon and it is very difficult for me to ask for changing this policy.




I didnt mean to run it when the user logs in...I meant to schedule it so that it runs as the user that is logged in. Im not sure if there is a way to make it default to the currently logged in person or not. Just an idea.


Sealeopard
(KiX Master)
2004-03-31 05:59 AM
Re: ScheduleTask()

By not providing username/password the task will run under the credential of the user that called JT.EXE

attiahia
(Hey THIS is FUN)
2004-03-31 06:49 AM
Re: ScheduleTask()

Jens
As you can see in the code and as I mentioned that I am providing the user name (service account) and password and the task scheduled & executed successfully. The problem is that once I add EndDate parameter to the same code it stop working and generate error code “[FAIL ] ITaskScheduler::AddWorkItemn hr=0x80070005”.

What I need is to specify EndDate for the task. Thank you very much.


attiahia
(Hey THIS is FUN)
2004-03-31 09:06 AM
Re: ScheduleTask()

I have just tried many time to run the command without providing login id & password because I thought in such case the current credential will be used (as you mentioned) to schedule the task.

I found it is scheduling the task and in the GUI you can see that the task scheduled under the current credential but at the specific time the task will not be executed. I am getting error message
[TRACE] Setting account information
[TRACE] Setting job's properties
[TRACE] Created trigger 0
[TRACE] Adding job 'Aramco2K.job'
[FAIL ] ITaskScheduler::AddWorkItemn hr=0x80041310


Sealeopard
(KiX Master)
2004-03-31 04:48 PM
Re: ScheduleTask()

Maybe you want to try Glenn Barnas' Task Scheduler UDFs. The ScheduleTask() UDF is designed for simple one-off task scheduling. Secondly, you're complaining about problems with JT.EXE and not the ScheduleTask UDF, thus you'll need to contact Microsoft to figure out why certain things don't work, in general JT.EXE is buggy and does not support the full range of the Task Scheduler options, also the JT.EXE documentation is not entirely correct either. The ScheduleTask UDF works as written.