Link to home
Start Free TrialLog in
Avatar of Simon336697
Simon336697Flag for Australia

asked on

VBScript compilation error.

Hi everyone,
Im having a problem with the attached script.
The error is:


D:\multierror.vbs(40, 1) Microsoft VBScript compilation error: Syntax error

Any help greatly appreciated.

I think its my lack of understanding of where to place the Next statements.
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
 ("d:\comps.txt", ForReading)
i = 0
Do Until objTextFile.AtEndOfStream
 strNextLine = objTextFile.Readline
 objDictionary.Add i, strNextLine
 i = i + 1
Loop
 
For Each objItem in objDictionary
	StrComputer = objDictionary.Item(objItem)
 
 	If Ping(strComputer) = True Then
  	On Error Resume Next
 
	set objWMIDateTime = CreateObject("WbemScripting.SWbemDateTime")
	set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	set colOS = objWMI.InstancesOf("Win32_OperatingSystem")
 
  		If Err.Number = 0 Then
  		On Error GoTo 0
 
			for each objOS in colOS
				objWMIDateTime.Value = objOS.LastBootUpTime
				Wscript.Echo "System Up Time: " &  TimeSpan(objWMIDateTime.GetVarDate,Now) & " (hh:mm:ss)"
			next
 
 
 
  		Else
   		Err.Clear
   		On Error GoTo 0
   		Wscript.Echo strComputer & "||" & "UNREACHABLE"
  		End If		
 
 
Function TimeSpan(dt1, dt2) 
	' Function to display the difference between
	' 2 dates in hh:mm:ss format
	If (isDate(dt1) And IsDate(dt2)) = false Then 
		TimeSpan = "00:00:00" 
		Exit Function 
        End If 
 
        seconds = Abs(DateDiff("S", dt1, dt2)) 
        minutes = seconds \ 60 
        hours = minutes \ 60 
        minutes = minutes mod 60 
        seconds = seconds mod 60 
 
        if len(hours) = 1 then hours = "0" & hours 
 
        TimeSpan = hours & ":" & _ 
            RIGHT("00" & minutes, 2) & ":" & _ 
            RIGHT("00" & seconds, 2) 
End Function 
 
 
 
Function Ping(strComputer)
 Dim objShell, boolCode
 Set objShell = CreateObject("WScript.Shell")
 boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
 If boolCode = 0 Then
  Ping = True
 Else
  Ping = False
 End If
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello Simon336697,

At first sight you are missing and end if and a next:

Regards,
Chris
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
 ("d:\comps.txt", ForReading)
i = 0
Do Until objTextFile.AtEndOfStream
 strNextLine = objTextFile.Readline
 objDictionary.Add i, strNextLine
 i = i + 1
Loop
 
For Each objItem in objDictionary
	StrComputer = objDictionary.Item(objItem)
 
 	If Ping(strComputer) = True Then
  	On Error Resume Next
 
	set objWMIDateTime = CreateObject("WbemScripting.SWbemDateTime")
	set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	set colOS = objWMI.InstancesOf("Win32_OperatingSystem")
 
  		If Err.Number = 0 Then
  		On Error GoTo 0
 
			for each objOS in colOS
				objWMIDateTime.Value = objOS.LastBootUpTime
				Wscript.Echo "System Up Time: " &  TimeSpan(objWMIDateTime.GetVarDate,Now) & " (hh:mm:ss)"
			next
 
 
 
  		Else
   		Err.Clear
   		On Error GoTo 0
   		Wscript.Echo strComputer & "||" & "UNREACHABLE"
  		End If
  	End If	
  Next
 
 
Function TimeSpan(dt1, dt2) 
	' Function to display the difference between
	' 2 dates in hh:mm:ss format
	If (isDate(dt1) And IsDate(dt2)) = false Then 
		TimeSpan = "00:00:00" 
		Exit Function 
        End If 
 
        seconds = Abs(DateDiff("S", dt1, dt2)) 
        minutes = seconds \ 60 
        hours = minutes \ 60 
        minutes = minutes mod 60 
        seconds = seconds mod 60 
 
        if len(hours) = 1 then hours = "0" & hours 
 
        TimeSpan = hours & ":" & _ 
            RIGHT("00" & minutes, 2) & ":" & _ 
            RIGHT("00" & seconds, 2) 
End Function 
 
 
 
Function Ping(strComputer)
 Dim objShell, boolCode
 Set objShell = CreateObject("WScript.Shell")
 boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
 If boolCode = 0 Then
  Ping = True
 Else
  Ping = False
 End If
End Function

Open in new window

Avatar of Simon336697

ASKER

Guys thank you so much it now works.
The only thing though is for systems that I cannot reach, Im not getting any output eg.

the
Else
Err.Clear
On Error GoTo 0
Wscript.Echo strComputer & "||" & "UNREACHABLE"

does not seem to be working.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi chris and pratima....guys you were both spot on thank you so much :>)