我在我的GPO中将VBScript设置为登录脚本。我遇到一个问题,每次登录时都会运行,我会在线上获得权限拒绝:
set lf = fso.opentextfile(lfp, 2, true)
兴趣点:
我在这里不知所措。这是相关的代码部分(请原谅任何糟糕的编码习惯):
'notify robocopy log file location
function seelog(log)
lf.writeline "[" & now & "] " & "See log file for details: " & 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 "[" & now & "] " & name & " folder was already up to date."
elseif retcode = 1 then
lf.writeline "[" & now & "] " & name & " folder was updated."
seelog(logfile)
else
lf.writeline "[" & now & "] " & name & " folder update exited with robocopy error code: " & 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 "[" & now & "] " & "Script started."
lf.writeline "[" & now & "] " & "Logged in user: " & un
lf.writeline "[" & now & "] " & "========================================================="
more code writing to log file and executing robocopy....
我认为压制所有错误是一种选择,但1)我宁愿不这样做,2)我不清楚如何在VBScript中实现这一点。
ETA:
On Error Resume Next
Set lf = fso.OpenTextFile(lfp, 2, True)
On Error Goto 0
我对此进行了测试,它确实打破了脚本。由于错误,未设置lf,因此以下行错误输出'对象需要" lf"'代码800a01a8如预期的那样。
答案 0 :(得分:0)
On Error Resume Next
将忽略该错误,然后On Error Goto 0
将恢复正常的错误处理
答案 1 :(得分:0)
我不喜欢这样做,但我的工作是从.bat启动.vbs(个人偏好 - 我喜欢在一个脚本中保留一个作业的所有内容,所以将来我不会#39 ; t必须去追逐文件。)
我在我的GPO中放置了一个logon.bat文件作为登录脚本。
@echo off
echo Launching update script...
cscript c:\folder1\script.vbs
这似乎可以解决我遇到的(显然是假的)权限问题。如果有人能告诉我为什么我看到我所看到的行为,我仍然很好奇。当调用OpenTextFile(直接从GPO启动时),只有管理员用户可以访问时,脚本引擎是否可以写入临时位置?