2

Been writing a script for showing and logging acceptance of a End User License Agreement for use over Active directory. It's written into a .hta file and uses VBScript for the most part.

I've run into a lot of issues that I have fixed so far but this last one has got me stumped.

When the file is run it gives me an

"End of Statement Expected"

error for line 35 of the script which is:

27    For Each ln in dict.Items
28      If ln = UserName Then
29        wasFound = true
30      End If
31    Next
32    If wasFound Then
33      WScript.Quit()
34      Else
35        Call DisableTaskMgr
36      Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
37      Set colItems = objWMIService.ExecQuery(wmiQuery)
38      For Each objItem in colItems
39        objItem.terminate(1)
40      Next
41    End If

I have no idea what it's asking me to change.

The full file can be found here

'On Error Resume Next
Set objShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
System = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System\"
dim currentDir : currentDir = objShell.CurrentDirectory
UserName = objShell.ExpandEnvironmentStrings("%USERNAME%")
pathToDirectory = currentDir & "/store/directory.txt"
wasFound = false
row = 0
Version = "V1.0"
Set dict = CreateObject("Scripting.Dictionary")
Set directoryfile = fso.OpentextFile(pathToDirectory, 1)
dim strComputer : strComputer = "."
dim wmiNS : wmiNS = "/root/cimv2"
dim wmiQuery : wmiQuery = "Select processID from win32_process where name = 'explorer.exe'"
dim objWMIService 
dim colItems 
dim objItem 
dim strOUT     

Do Until directoryfile.AtEndOfStream
  line = directoryfile.Readline
  dict.Add row, line
  row = row + 1
Loop

directoryfile.Close

For Each ln in dict.Items
  If ln = UserName Then
    wasFound = true
  End If
Next
If wasFound Then
  window.close
Else
  Call DisableTaskMgr
  Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
  Set colItems = objWMIService.ExecQuery(wmiQuery)
  For Each objItem in colItems
    objItem.terminate(1)
  Next
End If

sub DisableTaskMgr
  objShell.RegWrite System, "REG_SZ"
  objShell.RegWrite System & "/DisableTaskMgr", 1, "REG_DWORD"
end sub

sub EnableTaskMgr
  objShell.RegWrite System, "REG_SZ"
  objShell.RegWrite System & "/DisableTaskMgr", 0, "REG_DWORD"
end sub

sub RestartExplorerExe
  objShell.Run "explorer.exe"
end sub

sub Logon
  Call EnableTaskMgr
  If Not (fso.FileExists(currentDir & "/store/" & LCase(UserName) & ".csv")) Then
    Set objFile = fso.CreateTextFile(currentDir & "/store/" & LCase(UserName) & ".csv", 2, True)
    objFile.close
  Else
  End If
  Set ObjOpenFile = fso.OpenTextFile(currentDir & "/store/" & UserName & ".csv", 8, -2)
  objOpenFile.WriteLine(UserName & "," & Now & "," & Version)
  objOpenFile.Close
  Set objOpenFile = fso.OpenTextFile(currentDir & "/store/directory.txt", 8, -2)
  objOpenFile.WriteLine(UserName)
  objOpenFile.Close
  Call RestartExplorerExe
  window.close   
end sub

sub Logoff
  objShell.Run "shutdown /l"
end sub

If you guys find any other silly errors in the code I'd appreciate the help with those too, been working on this script for about 3 months.

EDIT: Most issues have now been fixed but I have run into a very odd issue that wasn't appearing before. Inside the CSV file that is created into the Logon method it is supposed to write {username},{date},{version number} but instead it writes random asian characters into the file.

user692942
  • 16,398
  • 7
  • 76
  • 175
  • 2
    You realise that `Dim variable = something` is incorrect syntax in VBScript, you cannot `Dim` and assign on the same line unless you use this little cheat `Dim variable : variable = something`. You have a lot of them in the code but because you have `On Error Resume Next` set they will be ignored and the next line will attempt to run. Also `OpenTextFile()` file needs a physical path not a virtual one, so that will also error. Personally I'd comment out `On Error Resume Next` until you have the script behaving as you would expect. – user692942 Jan 25 '16 at 13:55
  • 1
    You are also mixing variable and object variable assignments, `Set` is only used to assign instances of objects. A string in VBScript is not classed as an object instance but a simple type instead. Fact is `End of Statement Expected` could actually be coming from a dozen or more errors you currently have. – user692942 Jan 25 '16 at 14:06
  • @Lankymart By Physical path do you mean that I need to have the full path example: "C:\..\..\store\directory.txt"? – Bill Proxima Chandler Jan 25 '16 at 14:12
  • Yes, the path style you are using there *(in terms of Windows)* is a virtual path expected by a web server not a local / physical one. just writing `"/store/directory.txt"` the `FileSystemObject` has no concept of what it's working directory is, you always need to specify a full path. Commenting out `On Error Resume Next` should highlight most of these for you to dig into. – user692942 Jan 25 '16 at 14:15
  • Not sure why you would indent there but each to there own, usually indentation denotes code is inside a procedure, conditional statement or loop. Adding an extra indent after the declarations does nothing. – user692942 Jan 25 '16 at 14:19

1 Answers1

1

On Error Resume Next was causing my errors to be hidden. This was causing my inability to fix the script.

Taking that away gave me all the information I needed to fix all issues.