Jump to content

Return of error code to my exe file.


Valnurat
 Share

Recommended Posts

Is there any change that I can return a code for success or not success in my script?

I have a script that look in a SQL db and write to the registry.

I want to return some kind of a control, because I will call my script *.exe from VBScript.

_Main()

Func _Main()
    Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';', $sQUERY, $oRecordset, $aRecordsetArray, $aRecordset_inner
    Local $oConnection = _ADO_Connection_Create()
    _ADO_Connection_OpenConString($oConnection, $sConnectionString)
    If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)
    $sQUERY = "Select Shipdate from " & $sTableName & " where ComputerName = '" & @ComputerName & "'"
    $oRecordset = _ADO_Execute($oConnection, $sQUERY)
    $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False)
    $aRecordset_inner = _ADO_RecordsetArray_GetContent($aRecordsetArray)
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\ComputerInfo", "Shipdate", "REG_SZ", $aRecordset_inner[0][0])
EndFunc

 

Yours sincerely

Kenneth.

Link to comment
Share on other sites

So I just do this:

_Main()

Func _Main()
    Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';', $sQUERY, $oRecordset, $aRecordsetArray, $aRecordset_inner
    Local $oConnection = _ADO_Connection_Create()
    _ADO_Connection_OpenConString($oConnection, $sConnectionString)
    If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)
    $sQUERY = "Select Shipdate from " & $sTableName & " where ComputerName = '" & @ComputerName & "'"
    $oRecordset = _ADO_Execute($oConnection, $sQUERY)
    $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False)
    $aRecordset_inner = _ADO_RecordsetArray_GetContent($aRecordsetArray)
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\ComputerInfo", "Shipdate", "REG_SZ", $aRecordset_inner[0][0])
    Exit(1)
EndFunc

 

Yours sincerely

Kenneth.

Link to comment
Share on other sites

1 hour ago, Valnurat said:

Is there any change that I can return a code for success or not success in my script?

To achieve this you would first need to decide if the function worked or didn't. Best way is to check every _ADO* function for an error code.
You now only check once (after _ADO_Connection_OpenConString). Do it after every call.

I would drop "Exit(1)" as this always ends your script. End the script in the main function.

_Main()
If @error Then Exit MsgBox(0, "Error", "Script ended with @error = " & @error)
; More code to execute
Exit

Func _Main()
    Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';', $sQUERY, $oRecordset, $aRecordsetArray, $aRecordset_inner
    Local $oConnection = _ADO_Connection_Create()
    _ADO_Connection_OpenConString($oConnection, $sConnectionString)
    If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)
    $sQUERY = "Select Shipdate from " & $sTableName & " where ComputerName = '" & @ComputerName & "'"
    $oRecordset = _ADO_Execute($oConnection, $sQUERY)
    If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)
    $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False)
    If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)
    $aRecordset_inner = _ADO_RecordsetArray_GetContent($aRecordsetArray)
    If @error Then Return SetError(@error, @extended, $ADO_RET_FAILURE)
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\ComputerInfo", "Shipdate", "REG_SZ", $aRecordset_inner[0][0])
EndFunc

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Then you need Exit.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Like this:

_Main()
Exit(1)

Func _Main()
    Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';', $sQUERY, $oRecordset, $aRecordsetArray, $aRecordset_inner
    Local $oConnection = _ADO_Connection_Create()
    _ADO_Connection_OpenConString($oConnection, $sConnectionString)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    $sQUERY = "Select Shipdate from " & $sTableName & " where ComputerName = '" & @ComputerName & "'"
    $oRecordset = _ADO_Execute($oConnection, $sQUERY)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    $aRecordset_inner = _ADO_RecordsetArray_GetContent($aRecordsetArray)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\ComputerInfo", "Shipdate", "REG_SZ", $aRecordset_inner[0][0])
EndFunc

 

Edited by Valnurat

Yours sincerely

Kenneth.

Link to comment
Share on other sites

Would the calling VBS need to know if there was an error in your AutoIt script?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Then you need to use something like this and return different values.
On success the return value is set to 0, else it holds the error code returned by any of the _ADO* functions.

_Main()
Exit(0)

Func _Main()
    Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';', $sQUERY, $oRecordset, $aRecordsetArray, $aRecordset_inner
    Local $oConnection = _ADO_Connection_Create()
    _ADO_Connection_OpenConString($oConnection, $sConnectionString)
    If @error Then Exit(@error)
    $sQUERY = "Select Shipdate from " & $sTableName & " where ComputerName = '" & @ComputerName & "'"
    $oRecordset = _ADO_Execute($oConnection, $sQUERY)
    If @error Then Exit(@error)
    $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False)
    If @error Then Exit(@error)
    $aRecordset_inner = _ADO_RecordsetArray_GetContent($aRecordsetArray)
    If @error Then Exit(@error)
    RegWrite("HKEY_CURRENT_USER\SOFTWARE\ComputerInfo", "Shipdate", "REG_SZ", $aRecordset_inner[0][0])
EndFunc

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

It does return 0 as  you said, even thogh if $aRecordset_inner[0][0] is empty. :)

I would like to return a exitcode if $aRecordset_inner[0][0] is empty.

How can I do that?

EDIT: In my VBScript I check the registry and if it's empty I will run my exefile. Right now my exefile just return 0 no matter if $aRecordset_inner[0][0] is empty or not. I don't want to check the registry 2 times in my VBScript before creating a txtfile with controlinfo.

Edited by Valnurat

Yours sincerely

Kenneth.

Link to comment
Share on other sites

31 minutes ago, Valnurat said:

I would like to return a exitcode if $aRecordset_inner[0][0] is empty.

You already did half of the coding ;)

If $aRecordset_inner[0][0] = "" Then Exit(99) ; Or whatever return value you like

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Sorry, for returning to this ticket, but I don't understand why my VBScript is returning errrorcode 0, when I can see my AutoIT errorcode is 99.

; SetUP internal ADO.au3 UDF COMError Handler
_ADO_ComErrorHandler_UserFunction(_ADO_COMErrorHandler)

_Main()
Exit(0)


Func _Main()
    Local $sConnectionString = 'DRIVER={' & $sDriver & '};SERVER=' & $sServer & ';DATABASE=' & $sDatabase & ';UID=' & $sUser & ';PWD=' & $sPassword & ';', $sQUERY, $oRecordset, $aRecordsetArray, $aRecordset_inner
    Local $oConnection = _ADO_Connection_Create()
    _ADO_Connection_OpenConString($oConnection, $sConnectionString)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    $sQUERY = "Select Shipdate from " & $sTableName & " where ComputerName = '" & @ComputerName & "'"
    $oRecordset = _ADO_Execute($oConnection, $sQUERY)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    $aRecordsetArray = _ADO_Recordset_ToArray($oRecordset, False)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    $aRecordset_inner = _ADO_RecordsetArray_GetContent($aRecordsetArray)
    If @error Then SetError(@error, @extended, $ADO_RET_FAILURE)
    if IsArray($aRecordset_inner) Then
        If $aRecordset_inner[0][0] = '' or $aRecordset_inner[0][0] = Null Then Exit(99)
        RegWrite("HKEY_CURRENT_USER\SOFTWARE\ComputerInfo", "Shipdate", "REG_SZ", $aRecordset_inner[0][0])
    Else
        Exit(99)
    EndIf
EndFunc

Capture.JPG

My VBScript is this:

Sub RegComputerInfo
 	Const HKEY_CURRENT_USER = &H80000001
	Dim lstrKeyPath, lstrValueName, lstrValue, outFile, objFile, oExec
	lstrKeyPath = "SOFTWARE\ComputerInfo\"
	lstrValueName = "Shipdate"
	if ADHelper.IsCurrentUserMember("AllUsers") and (ADHelper.strComputerType <> "Server") then
		objReg.GetStringValue HKEY_CURRENT_USER,lstrKeyPath,lstrValueName,lstrValue
		if IsNull(lstrValue) then
			set oExec = WSHShell.exec(strScriptPath & "\Support\Script\ComInventory\GetShipdate.exe") ' Read a SQL DB
			msgbox "Test1 " & oExec.ExitCode		
			if oExec.ExitCode <> 0 then
				outFile = "\\servername\install\Log\ComputerInfo\" & WSHNetwork.ComputerName & ".txt"
				Set objFile = objFSO.CreateTextFile(outFile,True)
				objFile.Write strUsername & "|" & strModel & "|" & ServiceTag
				objFile.Close
			else
				msgbox "Test " & oExec.ExitCode		
			End if	
		End If	
	End if
End Sub 'FindShipDate

 

Yours sincerely

Kenneth.

Link to comment
Share on other sites

If I put this in my VBScript I do get my errorcode = 99

My VBScript is this:

Sub RegComputerInfo
 	Const HKEY_CURRENT_USER = &H80000001
	Dim lstrKeyPath, lstrValueName, lstrValue, outFile, objFile, oExec
	lstrKeyPath = "SOFTWARE\ComputerInfo\"
	lstrValueName = "Shipdate"
	if ADHelper.IsCurrentUserMember("AllUsers") and (ADHelper.strComputerType <> "Server") then
		objReg.GetStringValue HKEY_CURRENT_USER,lstrKeyPath,lstrValueName,lstrValue
		if IsNull(lstrValue) then
			set oExec = WSHShell.exec(strScriptPath & "\Support\Script\ComInventory\GetShipdate.exe") ' Read a SQL DB
			Do While oExec.Status = 0			<-----
     			WScript.Sleep 100				<-----
			Loop						<-----
			WScript.Echo oExec.Statusmsgbox "Test1 " & oExec.ExitCode		
			if oExec.ExitCode <> 0 then
				outFile = "\\servername\install\Log\ComputerInfo\" & WSHNetwork.ComputerName & ".txt"
				Set objFile = objFSO.CreateTextFile(outFile,True)
				objFile.Write strUsername & "|" & strModel & "|" & ServiceTag
				objFile.Close
			else
				msgbox "Test " & oExec.ExitCode		
			End if	
		End If	
	End if
End Sub 'FindShipDate

But should I really do that?

Yours sincerely

Kenneth.

Link to comment
Share on other sites

True, but as you can see in my VBS I create a txtfile on a server, if the exitcode is not 0.

In the future that server might not exists anymore and then I have to modify my autoit to the new server.

I would like to avoid to modify my autoit code.

Yours sincerely

Kenneth.

Link to comment
Share on other sites

Store the variable information (server address etc.) in an INI-file. So you can easily move to another server and only need to modify the config INI file.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

×
×
  • Create New...