Running VBScript code in Run Script using OnExecute

Hi Guys,

                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             --
' ----------------------------------------------
Dim servicesArray(16), arrayInput
Dim i : i = 0

Do Until objINI.AtEndOfStream
    arrayInput = Trim(CStr(objINI.ReadLine))
    If Instr(arrayInput,"[") Then
    ElseIf arrayInput = "" Then
    ElseIf InStr(Left(arrayInput,3), "REM") Then
    Else
        arrayInput = Mid(arrayInput,2)
        servicesArray(i) = arrayInput
        i = i + 1
        Call writeToLog("Added " & arrayInput & " to array")
    End If
Loop


' ----------------------------------------------
' -- Select country & create array of servers --
' ----------------------------------------------
Dim theDummy

theDummy = "TEST2"

Call writeLogBlankLine()

Dim serverArray()
Select Case theDummy
    Case "TEST1"
        ReDim serverArray(3)
        serverArray(0) = "SERVERNAME"
        serverArray(1) = "SERVERNAME"
        serverArray(2) = "SERVERNAME"
        serverArray(3) = "SERVERNAME"
        Call writeToLog("Stopping services for TEST1")
    Case "TEST2"
        ReDim serverArray(1)
        serverArray(0) = "."
        serverArray(1) = "SERVERNAME"
        Call writeToLog("Stopping services for TEST2")
    Case "TEST3"
        ReDim serverArray(1)
        serverArray(0) = "SERVERNAME"
        serverArray(1) = "SERVERNAME"
        Call writeToLog("Stopping services for TEST3")
    Case "TEST4"
        ReDim serverArray(1)
        serverArray(0) = "SERVERNAME"
        serverArray(1) = "SERVERNAME"
        Call writeToLog("Stopping services for TEST4")
    Case "TEST5"
        ReDim serverArray(1)
        serverArray(0) = "SERVERNAME"
            serverArray(1) = "SERVERNAME"
        Call writeToLog("Stopping services for TEST5")
    Case "TEST6"
        ReDim serverArray(1)
        serverArray(0) = "SERVERNAME"
        serverArray(1) = "SERVERNAME"
        Call writeToLog("Stopping services for TEST6")
    Case Else
          Call writeToLog("Invalid Country - Input error - SCRIPT CLOSING")
        WScript.Quit
End Select



' ----------------------------------------------
' -- User details for domain           --
' ----------------------------------------------

Dim strUsername, strPassword
strUsername = "XXXXXXXX"
strUsername = "Domain\" & strUsername
strPassword = "XXXXXXXX"


Call writeToLog("Running as: " & strUsername)


Call writeLogBlankLine()


' ----------------------------------------------
' -- Now connect and do work                  --
' ----------------------------------------------
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
    For x = 0 To UBound(servicesArray)
        If objService.Name = servicesArray(x) Then
            objService.StopService()
            Call writeToLog("Stopping " & objService.Name & " on server " & serverArray(0))
        Else
        End If
    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
    For x = 0 To UBound(servicesArray)
        If objService.Name = servicesArray(x) Then
            objService.StopService()
                 Call writeToLog("Stopping " & objService.Name & " on server " & serverArray(1))
        Else
        End If
    Next
Next

Set colServices = Nothing
Set objServices = Nothing



' ----------------------------------------------
' -- If TEST1, there are 3 Server boxes        --
' ----------------------------------------------
If theDummy = "TEST1" Then
    Call writeToLog("Connecting to server...")
    Set objServices = SWbemLocator.ConnectServer(serverArray(2), "root\cimv2", strUsername, strPassword)
    If Err.Number = 0 Then Call writeToLog("Connected to " & serverArray(2)) Else Call writeToLog("Error connecting! Error: " & err.number)
    Set colServices = objServices.InstancesOf("Win32_Service")

    For Each objService in colServices
        For x = 0 To UBound(servicesArray)
            If objService.Name = servicesArray(x) Then
                objService.StopService()
                Call writeToLog("Stopping " & objService.Name & " on server " & serverArray(2))
            Else
            End If
        Next
    Next

    Set colServices = Nothing
    Set objServices = Nothing

    Call writeToLog("Connecting to server...")
    Set objServices = SWbemLocator.ConnectServer(serverArray(3), "root\cimv2", strUsername, strPassword)
    If Err.Number = 0 Then Call writeToLog("Connected to " & serverArray(3)) Else Call writeToLog("Error connecting! Error: " & err.number)
    Set colServices = objServices.InstancesOf("Win32_Service")

    For Each objService in colServices
        For x = 0 To UBound(servicesArray)
            If objService.Name = servicesArray(x) Then
                objService.StopService()
                Call writeToLog("Stopping " & objService.Name & " on server " & serverArray(3))
            Else
            End If
        Next
    Next

    Set colServices = Nothing
    Set objServices = Nothing
End If

Call writeToLog("All services should now be stopped")
Call writeToLog("SCRIPT COMPLETE")

' ----------------------------------------------
' -- Log file functions                       --
' ----------------------------------------------
Function writeToLog(strToWrite)    <<<<< ERRORS HERE
        Set objLOG = objFSO.OpenTextFile(LOG_FILE, 8)
    objLog.Write Now & "--" & strToWrite & vbCrLf
    objLog.Close
End Function

Function writeLogBlankLine()
        Set objLOG = objFSO.OpenTextFile(LOG_FILE, 8)
    objLog.Write vbCrLf
    objLog.Close
End Function

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.

Hi Angus,

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

Brian.