vbscript-所需对象错误
出于某种原因,当我运行vbscript时,我在第4行Char 1处获得了InstallLog所需的对象。知道为什么会发生这种情况吗vbscript-所需对象错误,vbscript,Vbscript,出于某种原因,当我运行vbscript时,我在第4行Char 1处获得了InstallLog所需的对象。知道为什么会发生这种情况吗 Dim wshShell, FSO, strDexcomFolder, strDexcom, SysRoot, intRunError, strGroup, strDomain, InstallLog Const ForWriting = 2 Set InstallLog = FSO.OpenTextFile("Install_Log.txt", ForWriti
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
Dim wshShell、FSO、strDexcom文件夹、strDexcom、SysRoot、intRunError、strGroup、strDomain、InstallLog
写入常数=2
设置InstallLog=FSO.OpenTextFile(“Install_Log.txt”,用于写入)
设置wshShell=CreateObject(“WScript.Shell”)
SysRoot=WshShell.expandEnvironmentString(“%SystemDrive%”)
设置FSO=CreateObject(“Scripting.FileSystemObject”)
strDexcomFolder=“c:\ProgramFiles(x86)\Bioex”
strDomain=“xxxxxxxx”
strGroup=“域用户”
msgbox strDexcomFolder
如果存在FSO.folder(strDexcomFolder),则
msgbox“开始”
intRunError=WshShell.Run(“icacls”“”&strDexcomFolder&“/grant”&strDomain&“\”&strGroup&“:(OI)(CI)(M)”,2,True)
msgbox intRunError
如果错误号为0,则
InstallLog.WriteLine(“”)
InstallLog.WriteLine(“分配权限时出错!”)
InstallLog.WriteLine(“错误号:”&Err.Number&“,“&Err.Description&”)
InstallLog.WriteLine(“”)
MsgBox“分配权限时出错!”
InstallLog.close
如果结束
其他的
Wscript.Echo“错误:文件夹”&strDexcomFolder&“不存在”
如果结束
WScript.Quit
您肯定需要将这行代码放在使用FSO对象或调用类似于FSO.OpenTextFile的函数之前
Set FSO = CreateObject("Scripting.FileSystemObject")
在这里。这会让你走的。icacls命令现在被回送到日志中,以便您可以确认语法是否正确传递。编辑-某些命令行程序如果不在参数前面加上“cmd.exe/C”,则无法正确传递参数。我还添加了icacls.exe的完整路径,以防您从系统路径以外的位置运行
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
选项显式
Dim wshShell、objFSO、strDexcom文件夹、strDexcom、SysRoot、intRunError、strGroup、strDomain、InstallLog、strWinDir
设置wshShell=CreateObject(“WScript.Shell”)
SysRoot=WshShell.expandEnvironmentString(“%SystemDrive%”)
strWinDir=WshShell.expandEnvironmentString(“%windir%”)
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
常数ForReading=1
写入常数=2
出现的常数=8
常数只读=1
strDexcomFolder=“c:\ProgramFiles(x86)\Bioex”
strDomain=“xxxxxxxx”
strGroup=“域用户”
设置InstallLog=objFSO.CreateTextFile(“Install_Log.txt”,True)
MsgBox strDexcomFolder
如果存在objFSO.folder(strDexcomFolder),则
MsgBox“开始”
InstallLog.WriteLine(“运行命令-”&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
如果intRunError为0,则
InstallLog.WriteLine(“”)
InstallLog.WriteLine(“分配权限时出错!”)
InstallLog.WriteLine(“错误号:”&Err.Number&“,”&Err.Description)
InstallLog.WriteLine(“”)
MsgBox“分配权限时出错!”
如果结束
其他的
InstallLog.WriteLine(“错误:文件夹”&strDexcomFolder&“不存在”)
WScript.Echo“错误:文件夹”&strDexcomFolder&“不存在”
如果结束
InstallLog.close
WScript.Quit
如果在使用前设置了FSO
会发生什么?嘿,果然成功了,脚本运行时没有需要的对象。我知道这一定很简单。出于某种原因,它没有将错误输出到Install_Log.txt文件。我发现的另一件事是,如果组名中有空格,它显然不接受strgroup…有什么办法吗?示例:strGroup=“isdept”-works strGroup=“Domain Users”-不起作用。谢谢,你和DoctoreLove在这一行是正确的,需要在使用它的人之前添加toi。脚本在运行时没有出现所需对象错误。我知道这一定很简单。由于某些原因,当它在分配权限时出错时,它没有写入Install_Log.txt文件。我发现的另一件事是,如果组名中有空格,它显然不接受strgroup…有什么办法吗?示例:strGroup=“isdept”-works strGroup=“Domain Users”-不起作用Err.number 0
仅当脚本在执行on error resume next
语句后的代码块中遇到一些运行时错误/异常时才会为真。它不会捕获由icacls.exe
报告的任何错误,因为vbscript已成功执行命令WshShell.Run(“icacls.exe”…)
。如果需要捕获这些错误,可以将由icacls.exe
生成的stdout/stderr重定向到日志文件。看:谢谢你,效果很好!我以前从未使用过Chr(34),这似乎非常有用!:)你也可以添加额外的引号来做同样的事情。当我第一次学习VBscript时,我在知道额外的引号之前就发现了chr(34),我只是坚持使用它。这是个人喜好的问题。它们是相同的-WScript.Echo”““This has spaces with quotes”“”WScript.Echo chr(34)和“This has spaces with quotes”&chr(34)@RandySchuman个人认为chr(34)
在可读性方面比更烦人。您可以像InstallLog.WriteLine(“运行命令-”&strWinDir&“\System32\cmd.exe/C”&strWinDir&“\System32\icacls.exe”“”&strDexcomFolder&“/grant”“”&strDomain&“\”&strGroup&“:(OI)(CI)(M)”)那样编写该行代码。
。只要记住,无论何时在VBScript字符串中使用双引号“
”
“双引号”
来逃避它。抱歉@RandySchuman,这不是批评,就像你说的,这是个人选择。