Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Microsoft VBScript compilation error: Unexpected 'Next'

Posted on 2009-04-01
15
Medium Priority
?
1,331 Views
Last Modified: 2012-05-06
Created a script that is to poll a text file and make registry changes to each server on the list. I have tried a few variations on it and all of them error out.
On error resume next
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
Set objFSO=CreateObject("Scripting.FileSystemObject")
objInput=Split(objFSO.OpenTextFile("server.txt").ReadAll, vbCrLf)
 
strBITSKey = "\HKLM\System\CurrentControlSet\Services\BITS /v Start "
strWUKey = "\HKLM\System\CurrentControlSet\Services\wuauserv /v Start "
strWUConfig = "\HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate /v WUServer "
strPolicies = "\HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU /v "
 
For each strComputer in objInput
  With Wscript
    .Echo "reg add \\" & strComputer & strBITSKey & "/d 2 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strWUKey & "/d 2 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strWUConfig & "/d http://par1fpwsus.ad.adp.com"
    .Echo "reg add \\" & strComputer & Replace(strWUConfig, "WUServer", "WUStatusServer") & " /d http://par1fpwsus"
    .Echo "reg add \\" & strComputer & strPolicies & "NoAutoUpdate /d 0 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strPolicies & "UseWUServer /d 1 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strPolicies & "RescheduleWaitTime /d 1 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strPolicies & "NoAutoRebootWithLoggedOnUsers /d /f 0 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strPolicies & "AUOptions /d 4 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strPolicies & "ScheduledInstallDay /d 0 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strPolicies & "ScheduledInstallTime /d 5 /t REG_DWORD"
  End With
  If Trim(strComputer) <> "" Then
  Set objWMIService = GetObject("winmgmts:\\{impersonationLevel=impersonate}!" & strComputer & "\root\cimv2")
  ErrorHandler
  Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='BITS' or Name='wuauserv'")
  ErrorHandler
  For Each objService In colServiceList
  Wscript.Echo "Server: " & strComputer & "          Service Name: " & objService.Name
  Next
Next
 
Set objInput=Nothing
Set objFSO=Nothing
Set WshShell=Nothing
 
'Error Handler
Sub ErrorHandler
If Err.Number <> 0 Then
  Select Case Hex(Err.Number)
     Case "1CE"
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". The remote server machine does not exist or is unavailable."
     Case "46"
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". Access denied."
     Case "1A8"
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". Unable to access registry value."
     Case "80041003"
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". Unable to find registry key or value."
     Case Else
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". An unkown error occurred."
  End Select
  Err.Clear
End If
End Sub

Open in new window

WSUS.txt
0
Comment
Question by:SeanHenry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
15 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 24040514
You appear to be missing an End If somewhere in this block:
  If Trim(strComputer) <> "" Then     '<-- there is not matching End If
  Set objWMIService = GetObject("winmgmts:\\{impersonationLevel=impersonate}!" & strComputer & "\root\cimv2")
  ErrorHandler
  Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='BITS' or Name='wuauserv'")
  ErrorHandler
  For Each objService In colServiceList
  Wscript.Echo "Server: " & strComputer & "          Service Name: " & objService.Name
  Next

Open in new window

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24040534
It's actually missing an "End If", presumably on line 35 - if that's where you want the if block to end...
0
 

Author Comment

by:SeanHenry
ID: 24040644
I made that change but now I get a syntax error for Sub ErrorHandler.
'Error Handler
Sub ErrorHandler
If Err.Number <> 0 Then
  Select Case Hex(Err.Number)
     Case "1CE"
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". The remote server machine does not exist or is unavailable."
     Case "46"
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". Access denied."
     Case "1A8"
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". Unable to access registry value."
     Case "80041003"
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". Unable to find registry key or value."
     Case Else
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". An unkown error occurred."
  End Select
  Err.Clear
End If
End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 67

Expert Comment

by:sirbounty
ID: 24040692
Helps  if you can state which line the error is on...nothing is standing out, but perhaps change Err.Clear to
On Error Goto 0
0
 

Author Comment

by:SeanHenry
ID: 24041123
Sorry line 2 is what PrimalScript is highlighting as a syntax error.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24041165
code works for me...not sure what it is...
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 24041206
Works for me to in isolation. Perhaps it is a syntax error somewhere else that is causing the runtime to think the error is in the sub.

Can you post the code as you have it now?
0
 

Author Comment

by:SeanHenry
ID: 24041241
Here it is. It runs fin up until the Sub errorhandler....
Testv2.txt
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 252 total points
ID: 24041298
Ok, you now appear to be missing a Next. You had two in the original version you posted, so i guess you removed one when you added the End If.

You need to put the Next back in for the outer loop.
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 248 total points
ID: 24041316
Ah - you're still missing a Next...try this:
On error resume next
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
Set objFSO=CreateObject("Scripting.FileSystemObject")
objInput=Split(objFSO.OpenTextFile("server.txt").ReadAll, vbCrLf)
 
strBITSKey = "\HKLM\System\CurrentControlSet\Services\BITS /v Start "
strWUKey = "\HKLM\System\CurrentControlSet\Services\wuauserv /v Start "
strWUConfig = "\HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate /v WUServer "
strPolicies = "\HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU /v "
 
For each strComputer in objInput
  With Wscript
    .Echo "reg add \\" & strComputer & strBITSKey & "/d 2 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strWUKey & "/d 2 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strWUConfig & "/d http://par1fpwsus.ad.adp.com"
    .Echo "reg add \\" & strComputer & Replace(strWUConfig, "WUServer", "WUStatusServer") & " /d http://par1fpwsus"
    .Echo "reg add \\" & strComputer & strPolicies & "NoAutoUpdate /d 0 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strPolicies & "UseWUServer /d 1 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strPolicies & "RescheduleWaitTime /d 1 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strPolicies & "NoAutoRebootWithLoggedOnUsers /d /f 0 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strPolicies & "AUOptions /d 4 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strPolicies & "ScheduledInstallDay /d 0 /t REG_DWORD"
    .Echo "reg add \\" & strComputer & strPolicies & "ScheduledInstallTime /d 5 /t REG_DWORD"
  End With
  If Trim(strComputer) <> "" Then 
  Set objWMIService = GetObject("winmgmts:\\{impersonationLevel=impersonate}!" & strComputer & "\root\cimv2")
  ErrorHandler
  Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name='BITS' or Name='wuauserv'")
  ErrorHandler
  For Each objService In colServiceList
  Wscript.Echo "Server: " & strComputer & "          Service Name: " & objService.Name
  Next
End If
Next
 
Set objInput=Nothing
Set objFSO=Nothing
Set WshShell=Nothing
 
'Error Handler
Sub ErrorHandler
If Err.Number <> 0 Then
  Select Case Hex(Err.Number)
     Case "1CE"
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". The remote server machine does not exist or is unavailable."
     Case "46"
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". Access denied."
     Case "1A8"
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". Unable to access registry value."
     Case "80041003"
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". Unable to find registry key or value."
     Case Else
        Wscript.Echo "ERROR: Server " & strComputer & " encountered error " & Hex(Err.Number) & ". An unkown error occurred."
  End Select
  Err.Clear
End If
End Sub

Open in new window

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24043550
SeanHenry, with all due respect, carl_tawn was a few seconds ahead of me on nearly every post here.
I think he deserves most, if not all the points.  
I'd like to have this reopened for a fair allotment of points if you don't mind....
0
 

Author Comment

by:SeanHenry
ID: 24043665
How do I do that?
0
 

Author Closing Comment

by:SeanHenry
ID: 31565362
That was what I was missing. thanks.
0
 

Author Comment

by:SeanHenry
ID: 24043712
Got it thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question