# Running VBScript code in Run Script using OnExecute

**URL:** https://www.finalbuilder.com/forums/t/running-vbscript-code-in-run-script-using-onexecute/4302
**Category:** Discussion
**Created:** [March 20, 2008, 6:14am UTC](https://www.finalbuilder.com/forums/t/running-vbscript-code-in-run-script-using-onexecute/4302 "2008-03-20T06:14:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ipfservicedelivery](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/i/f07891/32.png) [@ipfservicedelivery](https://www.finalbuilder.com/forums/u/ipfservicedelivery)
#### Post date: [March 20, 2008, 6:14am UTC](https://www.finalbuilder.com/forums/t/running-vbscript-code-in-run-script-using-onexecute/4302/1 "2008-03-20T06:14:27Z")

</div>

Hi Guys,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I am trying to run the following code (See Below) in Automise2 using the 'Run Script' action from the OnExecute but it fails compilation with the following VBScript error, however if I run it from a DOS command line it works fine. Basically the script reads in an .INI file which contains the services I want to close down, it writes to a log file on its progress. Are there any limitations within Automise2 with VBScript? Any help would be apprecited.

Brian

Error Executing script : OnExecute  
Microsoft VBScript compilation error  
Syntax error  
Line : 195  
Char : 1

**Line 195 is the first line after the Log Functions Header**

CONST INI\_FILE = "c:\services.ini"  
CONST LOG\_FILE = "c:\stopLog.txt"  
  
Dim objFSO, objShell, objINI, objLOG, currentDir, objTracking, startTime  
Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set objINI = objFSO.OpenTextFile(INI\_FILE, 1)  
Set objShell = CreateObject("WScript.Shell")  
currentDir = objShell.CurrentDirectory  
Set objLOG = objFSO.CreateTextFile(LOG\_FILE,True)  
Set objLOG = Nothing  
  
Call writeToLog("SCRIPT STARTED")  
startTime = Now()  
  
  
' ----------------------------------------------  
' -- Read service names from file&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --  
' ----------------------------------------------  
Dim servicesArray(16), arrayInput  
Dim i : i = 0  
  
Do Until objINI.AtEndOfStream  
&nbsp;&nbsp;&nbsp; arrayInput = Trim(CStr(objINI.ReadLine))  
&nbsp;&nbsp;&nbsp; If Instr(arrayInput,"[") Then  
&nbsp;&nbsp;&nbsp; ElseIf arrayInput = "" Then  
&nbsp;&nbsp;&nbsp; ElseIf InStr(Left(arrayInput,3), "REM") Then  
&nbsp;&nbsp;&nbsp; Else  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; arrayInput = Mid(arrayInput,2)  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; servicesArray(i) = arrayInput  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i = i + 1  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Call writeToLog("Added " & arrayInput & " to array")  
&nbsp;&nbsp;&nbsp; End If  
Loop  
  
  
' ----------------------------------------------  
' -- Select country & create array of servers --  
' ----------------------------------------------  
Dim theDummy  
  
theDummy = "TEST2"  
  
Call writeLogBlankLine()  
  
Dim serverArray()  
Select Case theDummy  
&nbsp;&nbsp;&nbsp; Case "TEST1"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ReDim serverArray(3)  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(0) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(1) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(2) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(3) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Call writeToLog("Stopping services for TEST1")  
&nbsp;&nbsp;&nbsp; Case "TEST2"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ReDim serverArray(1)  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(0) = "."  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(1) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Call writeToLog("Stopping services for TEST2")  
&nbsp;&nbsp;&nbsp; Case "TEST3"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ReDim serverArray(1)  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(0) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(1) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Call writeToLog("Stopping services for TEST3")  
&nbsp;&nbsp;&nbsp; Case "TEST4"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ReDim serverArray(1)  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(0) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(1) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Call writeToLog("Stopping services for TEST4")  
&nbsp;&nbsp;&nbsp; Case "TEST5"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ReDim serverArray(1)  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(0) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(1) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Call writeToLog("Stopping services for TEST5")  
&nbsp;&nbsp;&nbsp; Case "TEST6"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ReDim serverArray(1)  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(0) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; serverArray(1) = "SERVERNAME"  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Call writeToLog("Stopping services for TEST6")  
&nbsp;&nbsp;&nbsp; Case Else  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Call writeToLog("Invalid Country - Input error - SCRIPT CLOSING")  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WScript.Quit  
End Select  
  
  
  
' ----------------------------------------------  
' -- User details for domain&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --  
' ----------------------------------------------  
  
Dim strUsername, strPassword  
strUsername = "XXXXXXXX"  
strUsername = "Domain\" & strUsername  
strPassword = "XXXXXXXX"  
  
  
Call writeToLog("Running as: " & strUsername)  
  
  
Call writeLogBlankLine()  
  
  
' ----------------------------------------------  
' -- Now connect and do work&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --  
' ----------------------------------------------  
Dim SWbemLocator, objServices, colServices, objService, x  
Set SWbemLocator = CreateObject("WbemScripting.SWbemLocator")  
If Err.Number = 0 Then Call writeToLog("Locator created") Else Call writeToLog("Error creating locator! Error: " & err.number)  
  
Call writeToLog("Connecting to server...")  
Set objServices = SWbemLocator.ConnectServer(serverArray(0), "root\cimv2")  
If Err.Number = 0 Then Call writeToLog("Connected to " & serverArray(0)) Else Call writeToLog("Error connecting! Error: " & err.number)  
Set colServices = objServices.InstancesOf("Win32\_Service")  
  
For Each objService in colServices  
&nbsp;&nbsp;&nbsp; For x = 0 To UBound(servicesArray)  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; If objService.Name = servicesArray(x) Then  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; objService.StopService()  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Call writeToLog("Stopping " & objService.Name & " on server " & serverArray(0))  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Else  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; End If  
&nbsp;&nbsp;&nbsp; Next  
Next  
  
Set colServices = Nothing  
Set objServices = Nothing  
  
Call writeToLog("Connecting to server...")  
Set objServices = SWbemLocator.ConnectServer(serverArray(1), "root\cimv2", strUsername, strPassword)  
If Err.Number = 0 Then Call writeToLog("Connected to " & serverArray(1)) Else Call writeToLog("Error connecting! Error: " & err.number)  
Set colServices = objServices.InstancesOf("Win32\_Service")  
  
For Each objService in colServices  
&nbsp;&nbsp;&nbsp; For x = 0 To UBound(servicesArray)  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; If objService.Name = servicesArray(x) Then  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; objService.StopService()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Call writeToLog("Stopping " & objService.Name & " on server " & serverArray(1))  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Else  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; End If  
&nbsp;&nbsp;&nbsp; Next  
Next  
  
Set colServices = Nothing  
Set objServices = Nothing  
  
  
  
' ----------------------------------------------  
' -- If TEST1, there are 3 Server boxes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --  
' ----------------------------------------------  
If theDummy = "TEST1" Then  
&nbsp;&nbsp;&nbsp; Call writeToLog("Connecting to server...")  
&nbsp;&nbsp;&nbsp; Set objServices = SWbemLocator.ConnectServer(serverArray(2), "root\cimv2", strUsername, strPassword)  
&nbsp;&nbsp;&nbsp; If Err.Number = 0 Then Call writeToLog("Connected to " & serverArray(2)) Else Call writeToLog("Error connecting! Error: " & err.number)  
&nbsp;&nbsp;&nbsp; Set colServices = objServices.InstancesOf("Win32\_Service")  
  
&nbsp;&nbsp;&nbsp; For Each objService in colServices  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; For x = 0 To UBound(servicesArray)  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; If objService.Name = servicesArray(x) Then  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; objService.StopService()  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Call writeToLog("Stopping " & objService.Name & " on server " & serverArray(2))  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Else  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; End If  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Next  
&nbsp;&nbsp;&nbsp; Next  
  
&nbsp;&nbsp;&nbsp; Set colServices = Nothing  
&nbsp;&nbsp;&nbsp; Set objServices = Nothing  
  
&nbsp;&nbsp;&nbsp; Call writeToLog("Connecting to server...")  
&nbsp;&nbsp;&nbsp; Set objServices = SWbemLocator.ConnectServer(serverArray(3), "root\cimv2", strUsername, strPassword)  
&nbsp;&nbsp;&nbsp; If Err.Number = 0 Then Call writeToLog("Connected to " & serverArray(3)) Else Call writeToLog("Error connecting! Error: " & err.number)  
&nbsp;&nbsp;&nbsp; Set colServices = objServices.InstancesOf("Win32\_Service")  
  
&nbsp;&nbsp;&nbsp; For Each objService in colServices  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; For x = 0 To UBound(servicesArray)  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; If objService.Name = servicesArray(x) Then  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; objService.StopService()  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Call writeToLog("Stopping " & objService.Name & " on server " & serverArray(3))  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Else  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; End If  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Next  
&nbsp;&nbsp;&nbsp; Next  
  
&nbsp;&nbsp;&nbsp; Set colServices = Nothing  
&nbsp;&nbsp;&nbsp; Set objServices = Nothing  
End If  
  
Call writeToLog("All services should now be stopped")  
Call writeToLog("SCRIPT COMPLETE")  
  
' ----------------------------------------------  
' -- Log file functions&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --  
' ----------------------------------------------  
Function writeToLog(strToWrite)&nbsp;&nbsp;&nbsp; **\<\<\<\<\< ERRORS HERE**  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set objLOG = objFSO.OpenTextFile(LOG\_FILE, 8)  
&nbsp;&nbsp;&nbsp; objLog.Write Now & "--" & strToWrite & vbCrLf  
&nbsp;&nbsp;&nbsp; objLog.Close  
End Function  
  
Function writeLogBlankLine()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set objLOG = objFSO.OpenTextFile(LOG\_FILE, 8)  
&nbsp;&nbsp;&nbsp; objLog.Write vbCrLf  
&nbsp;&nbsp;&nbsp; objLog.Close  
End Function

---

<div class="post-metadata">

### Author: ![angus](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/a/b3f665/32.png) [@angus](https://www.finalbuilder.com/forums/u/angus)
#### Post date: [March 20, 2008, 6:33am UTC](https://www.finalbuilder.com/forums/t/running-vbscript-code-in-run-script-using-onexecute/4302/2 "2008-03-20T06:33:12Z")

</div>

Hi Brian,   
  
Thanks for your post. The problem is that when you're working inside a script event, you're actually working inside a Sub. And in VBScript (unlike JavaScript) you can't nest functions or subs.   
  
The easiest solution is to cut out those two functions (WriteToLog and WriteLogBlankLine) then go to View -\> Project Global Script and paste them in there. then they'll be available to all the scripts in your project.   
  
Incidentally, if you're going to be reusing this code a lot then I'd suggest maybe creating a custom action with ActionStudio.

---

<div class="post-metadata">

### Author: ![ipfservicedelivery](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/i/f07891/32.png) [@ipfservicedelivery](https://www.finalbuilder.com/forums/u/ipfservicedelivery)
#### Post date: [March 20, 2008, 6:40am UTC](https://www.finalbuilder.com/forums/t/running-vbscript-code-in-run-script-using-onexecute/4302/3 "2008-03-20T06:40:23Z")

</div>

Hi Angus,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thanks for the reply. So the Global Project script is the place, cool. Again thanks for the quick answer

Brian.
