error-handling - 如何捕获空目录属性变量错误 8000500D 的错误
问题描述
在从活动目录检索用户属性的脚本中,有一个属性有时为空,但当它为空时,我似乎无法捕获错误。IsNull、IsObject、IsEmpty 和 IsBlank 似乎无法捕捉到它。每次我运行脚本时,我都会收到the directory property cannot be found in the cache
代码错误8000500D
有没有办法可以捕获该错误?
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshEnvironment = WshShell.Environment("process")
Set ADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & ADSysInfo.UserName)
strFax = ""
'check if fax number exists. Throw warning message if missing
If IsObject(objUser.FaxNumber) and Not isNull(objUser.FaxNumber)
If Not IsBlank(objUser.FaxNumber) Then
strFax = objUser.FaxNumber
Else
MsgBox "Please be aware that the fax number is missing", vbExclamation, "Attribute Missing"
End If
End If
[编辑]:这是否是捕获错误的最佳方法,并允许其他不相关的错误正常抛出?
On Error Resume Next
strFax = objUser.FaxNumber
If Err.Number <> 0 Then
MsgBox "Please be aware that the fax number is missing", vbExclamation, "Attribute Missing"
Err.clear()
strFax=""
End If
on error goto 0
解决方案
这是否是捕获错误的最佳方法,并且还允许正常抛出其他不相关的错误?
On Error Resume Next
strFax = objUser.FaxNumber
If Err.Number <> 0 Then
MsgBox "Please be aware that the fax number is missing", vbExclamation, "Attribute Missing"
Err.clear()
strFax=""
End If
on error goto 0