Jump to content

COM problems again


Recommended Posts

Hey again!

Found this VB script at http://www.microsoft.com/technet/scriptcen...s_man_rsxs.mspx

strComputer = "."
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 If

The 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.asp

Here 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 
Endfunc

The 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

  • Moderators

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

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

Link to comment
Share on other sites

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 )

Steve

Thanks Steve. I never would have gotten that!
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...