Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 847
  • Last Modified:

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

0
Simon336697
Asked:
Simon336697
  • 2
  • 2
2 Solutions
 
Pratima PharandeCommented:
you are missing

 End If
 Next

Try below
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

0
 
Chris BottomleyCommented:
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

0
 
Simon336697Author Commented:
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.
0
 
Chris BottomleyCommented:
Your script didn't actually have a block for this

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
  	else
   		Wscript.Echo strComputer & "||" & "No Response"
  	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

0
 
Simon336697Author Commented:
Hi chris and pratima....guys you were both spot on thank you so much :>)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now