Sign in to follow this  
Followers 0
Diver2006

Run AutoIT Scripts as a service

6 posts in this topic

hi,

My Script runs as service. The subject and the AttachFiles should have variable output. In the subject the starting time of the service is always spent not the starting time of the script. Otherwise everything runs.

Where my error ?

Starttime

(So, 27. Jan 15:57) "Tägliche Datensicherung vom 27.01.2008 15:47:01 wurde beendet."

#include "date.au3"
#include "misc.au3"
#include "inet.au3"
#include "file.au3"
#include "Array.au3"
#include "String.au3"

$file = "C:\WINDOWS\system\nav.ini"
$mal = "C:\WINDOWS\system\Mail.ini"

Func Subject()
    If IniRead($file, "003", "Zeit", "Nichts gefunden") = _NowTime() Then
        Return IniRead($mal, "001", "1", "Nichts gefunden")
    Else
        Return IniRead($file, "005", "5", "Nichts gefunden")
    EndIf
EndFunc  ;==>Subject
;
$user = _StringEncrypt(0, IniRead($file, "001", "1", "Nichts gefunden"), "pass", 1)
$pw = _StringEncrypt(0, IniRead($file, "001", "2", "Nichts gefunden"), "pass", 1)
$user_network = _StringEncrypt(0, IniRead($file, "001", "3", "Nichts gefunden"), "pass", 1)
$pw_Network = _StringEncrypt(0, IniRead($file, "001", "4", "Nichts gefunden"), "pass", 1)
;Mail
$s_SmtpServer = IniRead($file, "005", "1", "Nichts gefunden")
$s_FromName = IniRead($file, "005", "2", "Nichts gefunden")
$s_FromAddress = IniRead($file, "005", "3", "Nichts gefunden")
$s_ToAddress = IniRead($file, "005", "4", "Nichts gefunden")
$s_Subject = Subject()
$as_Body = IniRead($file, "007", "1", "Nichts gefunden")
$s_AttachFiles = IniRead($file, "005", "7", "Nichts gefunden")
$s_CcAddress = ""
$s_BccAddress = IniRead($file, "005", "6", "Nichts gefunden")
$s_Username = $user
$s_Password = $pw
$IPPort = 25
$ssl = 0
;
While 1
    If IniRead($file, "003", "Zeit", "Nichts gefunden") = _NowTime() Then
        Sleep(1500)
        IniWrite($file, "005", "5", "Tägliche Datensicherung vom " & _Now() & " wurde beendet."); Subject in nav.ini
        Sleep(1500)
        IniWrite($file, "005", "7", ""); AttachFiles 
        Sleep(1500)
        IniWrite($file, "006", "8", StringLeft(IniRead($file, "006", "8", "error"), 3) & "Log\Ivent_" & _NowDate() & ".prob")
        Sleep(30000)
        $rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_AttachFiles, $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl)
        Sleep(1500)
        Sleep(60000)
        IniWrite($file, "005", "7", IniRead($file, "006", "8", "nichts gefunden")); AttachFiles
        Sleep(1500)
        IniWrite($mal, "001", "1", "Tägliche Datensicherung vom " & $new & " " & IniRead($file, "003", "Zeit", "nicht gefunden") & " wurde gestartet."); Subject in mail.ini 
        $rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_AttachFiles, $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl)
    EndIf
    Sleep(100)
WEnd
;
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
    $objEmail = ObjCreate("CDO.Message")
    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress
    Local $i_Error = 0
    Local $i_Error_desciption = ""
    If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
    If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
    $objEmail.Subject = $s_Subject
    If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
        $objEmail.HTMLBody = $as_Body
    Else
        $objEmail.Textbody = $as_Body & @CRLF
    EndIf
    If $s_AttachFiles <> "" Then
        Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
        For $x = 1 To $S_Files2Attach[0]
            $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
            If FileExists($S_Files2Attach[$x]) Then
                $objEmail.AddAttachment($S_Files2Attach[$x])
            Else
                $i_Error_desciption = $i_Error_desciption & @LF & 'File not found to attach: ' & $S_Files2Attach[$x]
                SetError(1)
                Return 0
            EndIf
        Next
    EndIf
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
;Authenticated SMTP
    If $s_Username <> "" Then
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
    EndIf
    If $ssl Then
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    EndIf
;Update settings
    $objEmail.Configuration.Fields.Update
; Sent the Message
    $objEmail.Send
    If @error Then
        SetError(2)
        Return $oMyRet[1]
    EndIf
EndFunc  ;==>_INetSmtpMailCom
; Com Error Handler
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    $oMyRet[0] = $HexNumber
    $oMyRet[1] = StringStripWS($oMyError.description, 3)
    ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & $oMyRet[1] & @LF)
    SetError(1); something to check for when this function returns
    Return
EndFunc  ;==>MyErrFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

#include "date.au3"
#include "misc.au3"
#include "inet.au3"
#include "file.au3"
#include "Array.au3"
#include "String.au3"
$squirrely1 = _NowTime()

Include that last line there, near the top of your script, and just send $squirrely1 as the subject. Then the time that the script started will always be included in the subject.

Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites

hi,

Thanks for the answer. However the accurate starting time in the subject would like to show. It we always the time taken by the start of the service, which is not intended.

Tägliche Datensicherung vom 28.01.2008 [b]08:43:08[/b]
are the starting time of the service and 08:45:01 the execution of the service

This time is to also appear. It is to appear however the at the beginning and for the end of the Scripts.

Beginning

Tägliche Datensicherung vom 28.01.2008 [b]08:45:00[/b]

End

Tägliche Datensicherung vom 28.01.2008 [b]08:50:00[/b]

diver :D

Share this post


Link to post
Share on other sites

Upon cursory examination of your script, my conclusions are these:

1. The script runs as a process and not as a true Windows service (unless you have set it up that way somewhere else).

2. The script is the process, and there is nothing apparent to distinquish between your script and the process it constitutes when it is run.

Hope this helps. :D


Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites

thanks, with this code as service. Thus the Script should run also as service.

Diver2006

;-------------------------------------
;
; Run AutoIT Scripts as a service
; PTREX 03/11/2005
;
;-------------------------------------
#include <GUIConstants.au3>
#include <Process.au3>

#NoTrayIcon

;Declare Vars
Global $objWMIService
Global $strComputer = "."

Const $OWN_PROCESS = 16;16 is own process
Const $INTERACTIVE = True;True changes the $Own_Process to 272 is interact with desktop
Const $NORMAL_ERROR_CONTROL = 1
Dim $Script, $SrvAny, $info, $Help, $ServiceName = "Navision_Dienst"

;Main Gui
;Generated with Form Designer preview, great Job !!
GUICreate("Install/Uninstall Dienste", 500, 160)
GUICtrlCreateLabel("SRVANY.EXE", 30, 32)
GUICtrlCreateLabel("Dienst Name", 30, 07)
$IB_SrvAny = GUICtrlCreateInput($SrvAny, 150, 30, 250)
$ServiceName = GUICtrlCreateInput("", 150, 05, 250)
$But_SrvAny = GUICtrlCreateButton("...", 410, 30, 20, 20)
GUICtrlCreateLabel("Dienst ExE", 30, 57)
$IB_Script = GUICtrlCreateInput($Script, 150, 55, 250)
$But_Script = GUICtrlCreateButton("...", 410, 55, 20, 20)
$But_InstallService = GUICtrlCreateButton("Install Service", 150, 90, 120)
$But_StartService = GUICtrlCreateButton("Start Service", 280, 90, 120)
$But_DeleteService = GUICtrlCreateButton("Delete Service", 150, 120, 120)
$But_StopService = GUICtrlCreateButton("Stop Service", 280, 120, 120)

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $But_SrvAny
            _File_Open()
        Case $msg = $But_Script
            _Select_Script()
        Case $msg = $But_InstallService
            _Install_Service()
        Case $msg = $But_DeleteService
            _Delete_Service()
        Case $msg = $But_StartService
            _Start_Service()
        Case $msg = $But_StopService
            _Stop_Service()
    EndSelect
WEnd
Exit

;Functions
Func _File_Open()
    $FileOD = FileOpenDialog("Select SrvAny.exe File", "C:\", "EXE (*.exe)", 3)
    $File = FileOpen($FileOD, 0)
    If $File = -1 Then
        MsgBox(0, "Error", "Unable to open file, or no file selected !!")
    Else
        GUICtrlSetData($IB_SrvAny, Chr(34) & $FileOD & Chr(34))
        $SrvAny = (Chr(34) & $FileOD & Chr(34))
    EndIf
    FileClose($File)
EndFunc  ;==>_File_Open

Func _Select_Script()
    $FileOD = FileOpenDialog("Select Au3 Script File", "C:\", "EXE (*.exe)", 3)
    $File = FileOpen($FileOD, 0)
    If $File = -1 Then
        MsgBox(0, "Error", "Unable to open file, or no file selected !!")
    Else
        GUICtrlSetData($IB_Script, Chr(34) & $FileOD & Chr(34))
        $Script = (Chr(34) & $FileOD & Chr(34))
    EndIf
    FileClose($File)
EndFunc  ;==>_Select_Script

Func _Install_Service()
    $ServiceName = GUICtrlRead($ServiceName)
    If $SrvAny = "" Then
        MsgBox(0, "Warning ", "Select the SrvAny.exe using the File Open first")
    Else
        MsgBox(0, "", $ServiceName)
        MsgBox(0, "Information", "This has to be activated only ONCE !!")
        $objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
        $objService = $objWMIService.Get("Win32_BaseService")
        $errReturn = $objService.Create($ServiceName, $ServiceName, $SrvAny, $OWN_PROCESS, $NORMAL_ERROR_CONTROL, _
                "Automatic", $INTERACTIVE, "LocalSystem", "")
        _Assign_Script()
        _Interact_Desktop()
    EndIf
EndFunc  ;==>_Install_Service

Func _Delete_Service()
    $objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
    $colListOfServices = $objWMIService.ExecQuery("Select * from Win32_Service Where Name = '" & $ServiceName & "'")
    For $objService In $colListOfServices
        $objService.StopService()
        $objService.Delete()
    Next
EndFunc  ;==>_Delete_Service

Func _Start_Service()
    Local $var1 = RegRead("HKLM\SYSTEM\CurrentControlSet\Services\" & $ServiceName, "ImagePath")
    If @error = -1 Or StringRight($var1, 11) <> "SrvAny.exe" & Chr(34) Then
        MsgBox(4096, "", "RegKey SrvAny not yet created, or not correct" & $var1)
    Else
        Local $var2 = RegRead("HKLM\SYSTEM\CurrentControlSet\Services\" & $ServiceName & "\Parameters", "Application")
        If @error = 1 Then
            MsgBox(0, "Warning", "No Script has been assigned yet to the Any service!!")
        EndIf
        _RunDOS("Net start " & $ServiceName)
    EndIf
EndFunc  ;==>_Start_Service

Func _Stop_Service()
    _RunDOS("Net stop " & $ServiceName)
EndFunc  ;==>_Stop_Service

Func _Open_Services()
    _RunDOS("services.msc")
EndFunc  ;==>_Open_Services

Func _Interact_Desktop()
    Local $var1 = RegRead("HKLM\SYSTEM\CurrentControlSet\Services\" & $ServiceName, "ImagePath")
    If @error = -1 Or StringRight($var1, 11) <> "SrvAny.exe" & Chr(34) Then
        MsgBox(4096, "", "RegKey not yet created, or not correct")
        RegWrite("HKLM\SYSTEM\CurrentControlSet\Services\" & $ServiceName, "Type", "REG_DWORD", "272")
;~       RegWrite("HKLM\SYSTEM\CurrentControlSet\Services\" & $ServiceName, "Type", "REG_DWORD", "16")
    EndIf
EndFunc  ;==>_Interact_Desktop

Func _Assign_Script()
    Local $var1 = RegRead("HKLM\SYSTEM\CurrentControlSet\Services\" & $ServiceName, "ImagePath")
    If @error = -1 Or StringRight($var1, 11) <> "SrvAny.exe" & Chr(34) Then
        MsgBox(4096, "", "RegKey not yet created, or not correct")
    Else
        RegWrite("HKLM\SYSTEM\CurrentControlSet\Services\" & $ServiceName & "\Parameters", "AppDirectory", "REG_SZ", @WorkingDir)
        RegWrite("HKLM\SYSTEM\CurrentControlSet\Services\" & $ServiceName & "\Parameters", "Application", "REG_SZ", $Script)
    EndIf
EndFunc  ;==>_Assign_Script

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

In the code in your first post, change the function called _INetSmtpMailCom, change this line:

$objEmail.Subject = $s_Subject

to this:

$objEmail.Subject = $s_Subject & " " & _NowTime()

Then all you need to do is to remove your all other lines in the script that attempt to put _NowTime() into the passed email subject.

Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
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
Sign in to follow this  
Followers 0