' ********************************************************************* ' ' Name : AlertToAFile.vbs ' Author : 3Ds (UK) Limited ' Description : Example VBScript to record Sentry-go alert information ' to a database table using ODBC. ' ' Arguments : None. ' ' WIZARD:PARAMS=This script requires no parameters ' WIZARD:PARAMEXAMPLE= ' ' Returns : 0 - Success ' 1 - An error occurred and the information could not be ' logged. ' ' WIZARD:RESULTS=Return code ...||0 = OK|1 = An error prevented the script from running.||Script output also indicates "OK" for success, or "Error" with details of the faults found. ' ' Notes : Requires Windows Scripting Host (for VBScript), ' ActiveX Data Objects (ADO) & an ODBC data source. ' Should be called from Sentry-go as an Alert Engine ' file in order to expand the error-specific ' place-markers. ' ' Change the "TODOs" for your own requirements. ' ' ********************************************************************* ' Enable in-line error handling On Error Resume Next ' ------ Local declarations ------ Dim objDatabase ' Our database reference Dim strDSN ' ODBC Data source name Dim strUser ' User ID for database Dim strPassword ' Password Dim strSource ' The file to write Dim strError ' Text to write ' ------ Main Logic ------ ' TODO: Set the ODBC DSN & optional user/password strDSN = "Sentrygo" strUser = "sa" strPassword = "smartcar" ' TODO: Format the error & source strError = "<$$ERROR>" strSource = "<$$SOURCE>" ' Ensure the error text won't blow the buffer. ' TODO: This is dependent on the length of the field in the ' target database. This logic assumes a VARCHAR 255 or equivalent. strError = Left(strError, 254) ' Ensure they don't have any characters that would affect the database strError = Replace (strError, "'", "") strSource = Replace (strSource, "'", "") ' Create a link to the database Set objDatabase = CreateObject("ADODB.Connection") If Err.Number <> 0 Then ' Error WScript.Echo "Error. Unable to create ADO connection object. " & Err.Description WScript.Quit(1) Else ' Connect to it If Len (strUser) > 0 Then ' Use authentication details provided objDatabase.Open "ODBC;Driver=MSDASQL;DSN=" & strDSN & ";UID=" & strUser & ";PWD=" & strPassword Else objDatabase.Open "ODBC;Driver=MSDASQL;DSN=" & strDSN End If If Err.Number <> 0 Then ' Error Set objDatabase = Nothing WScript.Echo "Error. Unable to connect to the DSN " & strDSN & ". " & Err.Description WScript.Quit(1) Else ' TODO: Insert the appropriate error information into the database objDatabase.Execute "INSERT INTO Errors (DateTimeOccurred, PCName, Checkname, Sourcename, ErrorText) VALUES (<$$TIMELOGGED>, '<$$SERVER>', '<$$TEST>', '" & strSource & "', '" & strError & "')" If Err.Number <> 0 Then ' Error objDatabase.Close Set objDatabase = Nothing WScript.Echo "Error. Failed to execute SQL statement. " & Err.Description WScript.Quit(1) End If ' Close the connection objDatabase.Close End If Set objDatabase = Nothing End If WScript.Echo "OK. Message logged successfully" WScript.Quit(0) ' ------ End of Script ------