JerryD Posted March 12, 2006 Share Posted March 12, 2006 Hey again!Found this VB script at http://www.microsoft.com/technet/scriptcen...s_man_rsxs.mspxstrComputer = "." Set objService = GetObject("winmgmts:\\" & strComputer) Set objNewJob = objService.Get("Win32_ScheduledJob") errJobCreated = objNewJob.Create _ ("C:\Windows\System32\defrag.exe C: -f", "********123000.000000-420", _ True , 1 OR 4 OR 16, , , JobID) If Err.Number = 0 Then Wscript.Echo "New Job ID: " & JobID Else Wscript.Echo "An error occurred: " & errJobCreated End IfThe VB code works perfectly, and creates a scheduled task, run by the SYSTEM account which can not be modified, and can only be deleted by Administrator level users. The only change on the code above is the original ran the program "MONITOR.EXE", and mine uses Defrag.exe with parameters, but it still performs as expected.There is better documentation for Win32_ScheduledJob at Create Method of the Win32_ScheduledJob Class - http://msdn.microsoft.com/library/default....cheduledjob.aspHere is my AutoIt translation:$oMyError = ObjEvent ( 'AutoIt.Error', 'MyErrFunc' ); Install a custom error handler $strComputer = '.' $JobID = '' $objService = ObjGet ('winmgmts:\\' & $strComputer ) If NOT IsObj ( $objService ) OR @error Then MsgBox ( 0, @ScriptName, 'No Object!', 5 ) Exit EndIf $objNewJob = $objService.Get ( 'Win32_ScheduledJob' ) $errJobCreated = $objNewJob.Create _ ( "C:\Windows\System32\defrag.exe C: -f", "********123000.000000-420", _ True, 1 OR 4 OR 16, 0, False, $JobID ) If @error Then Exit MsgBox(262144,'Debug line ~133','Selection:' & @lf & '$errJobCreated.Err.Number' & @lf & @lf & 'Return:' & @lf & $errJobCreated.Err.Number & @lf & @lf & '@Error:' & @lf & @Error);### Debug MSGBOX If $errJobCreated.Err.Number = 0 Then MsgBox ( 0, @ScriptName, 'New Job ID: ' & $JobID, 10 ) Else MsgBox ( 0, @ScriptName, 'An error occurred: ' & $errJobCreated, 10 ) EndIf Func MyErrFunc() $HexNumber=hex($oMyError.number,8) Msgbox ( 0, '', 'COM Error intercepted!' & @CRLF & _ 'Error #: ' & $HexNumber & @CRLF & _ 'Description: ' & $oMyError.windescription ) ClipPut ( 'COM Error: ' & $HexNumber & @CRLF & 'Description: ' & $oMyError.windescription ) SetError(1); something to check for when this function returns EndfuncThe two empty parameters in the original code are "DaysOfMonth" which is left empty or blank if using a daily schedule that always runs, and "InteractWithDesktop" which should almost always be set to FLASE. AutoIt doesn't allow for empty parameters, but I've tried everything with no success.For some reason it's not working. I keep getting error 80020009 returned by $oMyError when $objNewJob.Create is called.Any thoughts?TIA! Link to comment Share on other sites More sharing options...
JerryD Posted March 15, 2006 Author Share Posted March 15, 2006 No takers? Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 15, 2006 Moderators Share Posted March 15, 2006 No takers?I'll take New Orleans + 16 Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
eltorro Posted March 16, 2006 Share Posted March 16, 2006 I'll take a double meat combo with cheese, green chile, bacon. Hold the onions and easy on the mayo. Coke or Pepsi, easy on the ice. Seasoned fries.$oMyError = ObjEvent ( 'AutoIt.Error', 'MyErrFunc' ); Install a custom error handler $strComputer = '.' $JobID = '' $objService = ObjGet ('winmgmts:\\' & $strComputer ) If NOT IsObj ( $objService ) OR @error Then MsgBox ( 0, @ScriptName, 'No Object!', 5 ) Exit EndIf $objNewJob = $objService.Get ( 'Win32_ScheduledJob' ) $errJobCreated = $objNewJob.Create _ ( "C:\Windows\System32\defrag.exe C: -f", "********123000.000000-420", _ True,BitOR( 1 , 4 , 16), 0, False, $JobID ) If $errJobCreated = 0 and Not @error Then MsgBox(262144,@ScriptName,'Job successfully created.'&@lf&'New Job ID: ' & $JobID,10) Else MsgBox (4096, @ScriptName, 'An error has occurred!' &@lf&'Error code :'& $errJobCreated, 10 ) EndIf Func MyErrFunc() $HexNumber=hex($oMyError.number,8) Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !" & @CRLF & @CRLF & _ "err.description is: " & @TAB & $oMyError.description & @CRLF & _ "err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _ "err.number is: " & @TAB & $HexNumber & @CRLF & _ "err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _ "err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _ "err.source is: " & @TAB & $oMyError.source & @CRLF & _ "err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _ "err.helpcontext is: " & @TAB & $oMyError.helpcontext _ ) ClipPut ( 'COM Error: ' & $HexNumber & @CRLF & 'Description: ' & $oMyError.windescription ) SetError(1); something to check for when this function returns Endfunc$errJobCreated = $objNewJob.Create ( "C:\Windows\System32\defrag.exe C: -f","********123000.000000-420",True,BitOR( 1 , 4 , 16), 0, False, $JobID )Steve Regards, [indent]ElTorro[/indent][font="Book"] Decide, Commit, Achieve[/font]_ConfigIO.au3Language Translation --uses Google(tm) MsgBox Move XML wrapper UDF XML2TreeView Zip functionality Split your GUI Save Print ScreenZipPluginEdit In Place listviewSome of my scripts on Google code Link to comment Share on other sites More sharing options...
JerryD Posted March 16, 2006 Author Share Posted March 16, 2006 I'll take a double meat combo with cheese, green chile, bacon. Hold the onions and easy on the mayo. Coke or Pepsi, easy on the ice. Seasoned fries.$errJobCreated = $objNewJob.Create ( "C:\Windows\System32\defrag.exe C: -f","********123000.000000-420",True,BitOR( 1 , 4 , 16), 0, False, $JobID )SteveThanks Steve. I never would have gotten that! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now