-1

For some reason when I run my vbscript, I am getting an object required at Line 4 Char 1 for the InstallLog. Any idea why this might be occurring?

Dim wshShell, FSO, strDexcomFolder, strDexcom, SysRoot, intRunError, strGroup, strDomain, InstallLog

Const ForWriting = 2
Set InstallLog = FSO.OpenTextFile("Install_Log.txt", ForWriting)

Set wshShell = CreateObject("WScript.Shell")
SysRoot = WshShell.ExpandEnvironmentStrings("%SystemDrive%")
Set FSO = CreateObject("Scripting.FileSystemObject")
strDexcomFolder = "c:\Program Files (x86)\Bioex"
strDomain = "xxxxxxxx"
strGroup = "domain users"

msgbox strDexcomFolder
If FSO.FolderExists(strDexcomFolder) Then
msgbox"start"
intRunError = WshShell.Run("icacls """ & strDexcomFolder & """ /grant " & strDomain & "\" & strGroup & ":(OI)(CI)(M) ", 2, True)
msgbox intRunError
   If Err.number <> 0 Then
    InstallLog.WriteLine("")
    InstallLog.WriteLine("Error Assigning Permissions!")
    InstallLog.WriteLine("Error #: "&Err.Number&", "&Err.Description&"") 
    InstallLog.WriteLine("")
    MsgBox"Error assigning permissions!"
    InstallLog.close
    End If
Else
    Wscript.Echo "Error: folder " & strDexcomFolder & " does not exist"
End If
WScript.Quit
Ryan
  • 3
  • 1
  • 3
  • 4
    What happens if you set `FSO` *before* you use it? – doctorlove Aug 30 '16 at 13:39
  • Hey, sure enough that worked, the script ran without the object required . I knew it had to be something simple. For some reason it's not outputting the error to the Install_Log.txt file though. – Ryan Aug 30 '16 at 14:01
  • Another thing I found is that apparently it doesn't accept the strgroup if the group name has a space in it...any way around that? Example: strGroup = "isdept" - works strGroup = "Domain Users" - Doesn't work – Ryan Aug 30 '16 at 14:09

2 Answers2

1

You will definitely need to have this line of code PRECEDES those which are using the FSO object or calling a function like FSO.OpenTextFile

Set FSO = CreateObject("Scripting.FileSystemObject")
some1
  • 857
  • 5
  • 11
  • Hey Thanks, You and DoctoreLove were correct on that line needing toi precede those that use it. The script ran without the object required error. I knew it had to be something simple. For some reason it's not writing to the Install_Log.txt file though when it errors on assigning the permissions. – Ryan Aug 30 '16 at 14:05
  • Another thing I found is that apparently it doesn't accept the strgroup if the group name has a space in it...any way around that? Example: strGroup = "isdept" - works strGroup = "Domain Users" - Doesn't work – Ryan Aug 30 '16 at 14:08
  • `Err.number <> 0` will only be true if your script encounter some runtime error/exception in a code block follow after the `on error resume next` statement. It will not trap any error reported by `icacls.exe` since vbscript have successfully executed the command `WshShell.Run("icacls.exe"...)`. If you need to capture those errors, you may redirect the stdout/stderr generated by `icacls.exe` to a log file. See:http://stackoverflow.com/questions/2075886/capturing-output-from-wshshell-exec-using-windows-script-host – some1 Aug 31 '16 at 04:10
1

Here. This should get you going. The icacls command is now being echoed into the log so you can confirm your syntax is being passed correctly. Edit - Some command line programs do not pass arguments correctly without preceding them with "cmd.exe /C". I added that also along with full path to icacls.exe in case you are running from a location that is not in the system path.

Option Explicit
Dim wshShell, objFSO, strDexcomFolder, strDexcom, SysRoot, intRunError, strGroup, strDomain, InstallLog, strWinDir

Set wshShell = CreateObject("WScript.Shell")
SysRoot = WshShell.ExpandEnvironmentStrings("%SystemDrive%")
strWinDir = WshShell.ExpandEnvironmentStrings("%windir%")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading =   1
Const ForWriting =   2
Const ForAppending = 8
Const ReadOnly =     1

strDexcomFolder = "c:\Program Files (x86)\Bioex"
strDomain = "xxxxxxxx"
strGroup = "domain users"

Set InstallLog = objFSO.CreateTextFile("Install_Log.txt", True)

MsgBox strDexcomFolder
If objFSO.FolderExists(strDexcomFolder) Then
  MsgBox "Start"
  InstallLog.WriteLine("Running Command - " & strWinDir & "\System32\cmd.exe /C " & strWinDir & "\System32\icacls.exe " & Chr(34) & strDexcomFolder & Chr(34) & " /grant " & Chr(34) & strDomain & "\" & strGroup & chr(34) & ":(OI)(CI)(M)")
  intRunError = WshShell.Run(strWinDir & "\System32\cmd.exe /C " & strWinDir & "\System32\icacls.exe " & Chr(34) & strDexcomFolder & Chr(34) & " /grant " & Chr(34) & strDomain & "\" & strGroup & chr(34) & ":(OI)(CI)(M)", 2, True)
  MsgBox intRunError
    If intRunError <> 0 Then
      InstallLog.WriteLine("")
      InstallLog.WriteLine("Error Assigning Permissions!")
      InstallLog.WriteLine("Error #: " & Err.Number & ", " & Err.Description) 
      InstallLog.WriteLine("")
      MsgBox "Error assigning permissions!"
    End If
Else
  InstallLog.WriteLine("Error: folder " & strDexcomFolder & " does not exist")
  WScript.Echo "Error: folder " & strDexcomFolder & " does not exist"
End If
InstallLog.close
WScript.Quit
Randy Schuman
  • 357
  • 1
  • 9
  • Thank you, that works great! I've never used the Chr(34) before, that seems to be quite useful! :) – Ryan Aug 31 '16 at 14:17
  • You can also add extra quotes to do the same thing. When I first learned VBscript, I found out about the chr(34) before I knew about the extra quotes and I have just stuck with it. It's a matter of personal preference. These are the same - WScript.Echo """This has spaces with quotes""" WScript.Echo chr(34) & "This has spaces with quotes" & chr(34) – Randy Schuman Aug 31 '16 at 22:23
  • @RandySchuman Personally find `Chr(34)` more annoying then `""` for readability. You could have wrote that line like `InstallLog.WriteLine("Running Command - " & strWinDir & "\System32\cmd.exe /C " & strWinDir & "\System32\icacls.exe """ & strDexcomFolder & """ /grant """ & strDomain & "\" & strGroup & """:(OI)(CI)(M)")`. Just remember whenever you use a literal double quote `"` inside a VBScript string double it `""` to escape it. – user692942 Sep 01 '16 at 11:29
  • Sorry @RandySchuman it wasn't a criticism like you say it's personal choice. – user692942 Sep 01 '16 at 20:12