title: VBScript Error "Permission Denied" But Works Anyway
body: <p>I have a VBScript setup as a logon script in my GPO.  I'm having an issue where every time it runs at logon, I get Permission Denied on the line:</p>
  
  <pre><code>set lf = fso.opentextfile(lfp, 2, true)
  </code></pre>
  
  <p>Points of interest:</p>
  
  <ul>
  <li>Despite the error, the script completes successfully.</li>
  <li>The script executes without error if I run it manually.</li>
  <li>The lfp variable points to c:\folder\folder\file.log.  The file is created (when necessary) and populated appropriately (overwritten, as expected when it does exist).</li>
  <li>If the file is created, the fso is closed before trying to opentextfile.</li>
  <li>The user logging in does have modify permission to the path and to the file being replaced when it exists via inherited Authenticated Users permission (from c:\folder1 - see below).</li>
  <li>If I throw in a wscript.sleep 30000 just before that line, it just waits 30 seconds to throw permissions denied.</li>
  <li>If user is a member of local administrators group on PC, I get no errors.  Again, when user is not local admin, it errors, but completes successfully.</li>
  <li>I see the same behavior under both Windows 7 and 10.</li>
  </ul>
  
  <p>I'm at a loss here.  Here's the pertinent section of code (please excuse any poor coding practice):</p>
  
  <pre><code>'notify robocopy log file location
  function seelog(log)
    lf.writeline "[" &amp; now &amp; "]  " &amp; "See log file for details: " &amp; log
  end function
  
  'process robocopy exit codes and write log
  function writeerrors(items)
    docs = items(0)
    retcode = items(1)
    logfile = items(2)
    if docs = "c" then
      name = "some stuff"
    else
      name = "some other stuff"
    end if
    If retcode = 0 Then
      lf.writeline "[" &amp; now &amp; "]  " &amp; name &amp; " folder was already up to date."
    elseif retcode = 1 then
      lf.writeline "[" &amp; now &amp; "]  " &amp; name &amp; " folder was updated."
      seelog(logfile)
    else
      lf.writeline "[" &amp; now &amp; "]  " &amp; name &amp; " folder update exited with robocopy error code: " &amp; retcode
      seelog(logfile)
    End If
  end function
  
  'get logged in user
  un = CreateObject("WScript.Network").UserName
  
  'check for logfile, and if not exist, create
  'folder1 always exists, no need to check or create
  lfp = "c:\folder1\folder2\folder3\logfile1.log"
  ld1 = "c:\folder1\folder2"
  ld2 = "c:\folder1\folder2\folder3"
  
  set fso = createobject("scripting.filesystemobject")
  
  if not fso.fileexists(lfp) then
    if not fso.folderexists(ld1) then
      fso.createfolder(ld1)
    end if
    if not fso.folderexists(ld2) then
      fso.createfolder(ld2)
    end if
    set cf = fso.createtextfile(lfp)
    cf.close
  end if
  
  'open logfile (lfp variable)
  'for writing (2)
  'overwrite if already exists (true)
  wscript.sleep 30000
  '************permission denied happens on next line on lfp var*************
  Set lf = fso.OpenTextFile(lfp, 2, True)
  
  lf.writeline "[" &amp; now &amp; "]  " &amp; "Script started."
  lf.writeline "[" &amp; now &amp; "]  " &amp; "Logged in user: " &amp; un
  lf.writeline "[" &amp; now &amp; "]  " &amp; "========================================================="
  
  more code writing to log file and executing robocopy....
  </code></pre>
  
  <p>I suppose suppressing all errors is an option, but 1) I'd rather not, and 2) I'm not clear on how to accomplish that in VBScript.</p>
  
  <p>ETA:</p>
  
  <pre><code>On Error Resume Next
    Set lf = fso.OpenTextFile(lfp, 2, True)
  On Error Goto 0
  </code></pre>
  
  <p>I tested this and it does break the script.  lf is not set due to the error so the following lines error out with 'object required "lf"' code 800a01a8 as expected.</p>