vbscript和检查null
在下面的“If(IsNull(value))then”行中,我的代码正确吗?我想检查注册表项是否存在,如果不存在,则显示一个网页vbscript和检查null,vbscript,Vbscript,在下面的“If(IsNull(value))then”行中,我的代码正确吗?我想检查注册表项是否存在,如果不存在,则显示一个网页 Option Explicit On error resume next Dim SysVarReg, Value Set SysVarReg = WScript.CreateObject("WScript.Shell") value = SysVarReg.RegRead ("HKCU\Software\test\FirstLogonComplete") If (
Option Explicit
On error resume next
Dim SysVarReg, Value
Set SysVarReg = WScript.CreateObject("WScript.Shell")
value = SysVarReg.RegRead ("HKCU\Software\test\FirstLogonComplete")
If (IsNull(value)) then
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "c:\Program Files\Internet Explorer\iexplore.exe https://intranet/start.htm"
Dim SysVarReg2, Value2
Value2 = "TRUE"
Set SysVarReg2 = WScript.CreateObject("WScript.Shell")
SysVarReg2.RegWrite "HKCU\Software\test\FirstLogonComplete", Value2
else
wscript.echo "Already logged on"
end if
如果重新研磨抛出错误,则
值
未初始化;未初始化变量的值为空
,而不是空
。
因此,您应该添加行
value = Null
在
Dim
语句之后。否则,IsNull
将始终返回False
您的意思是“Null”还是“Nothing”
在VBScript中,无表示缺少值(或空指针)。
Null用于表示数据库中的Null值
有关更多信息,请参阅
另外,有关如何检测注册表项是否存在的信息,请参见:
Const HKLM = &H80000002
Set oReg =GetObject("Winmgmts:root\default:StdRegProv")
sKeyPath = "Software\Microsoft\Windows\CurrentVersion"
If RegValueExists(HKLM, sKeyPath, sValue) Then
WScript.Echo "Value exists"
Else
WScript.Echo "Value does not exist"
End If
Function RegValueExists(sHive, sRegKey, sRegValue)
Dim aValueNames, aValueTypes
RegValueExists = False
If oReg.EnumValues(sHive, sKeyPath, aValueNames, aValueTypes) = 0 Then
If IsArray(aValueNames) Then
For i = 0 To UBound(aValueNames)
If LCase(aValueNames(i)) = LCase(sRegValue) Then
RegValueExists = True
End If
Next
End If
End If
End Function
这是我对一个商业问题的解决方案。他们想使USB成为只读的,这样数据就不会在拇指驱动器上漂移。在ping并连接到WMI之后,我必须确定密钥是否已经存在,并且设置了值。在几千台电脑上
keyExists = fnReadKeyValue()
'======================================
'======================================
Function fnReadKeyValue()
' ' EXAMPLE VALUES
' const HKEY_LOCAL_MACHINE = &H80000002
' strComputer = "."
' strKeyPath = "SYSTEM\CurrentControlSet\Control\StorageDevicePolicies"
' strEntryName = "WriteProtect"
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
objReg.GetDWordValue HKEY_LOCAL_MACHINE, strKeyPath, strEntryName, strValue
if IsNull(strValue) then
objLogFile.WriteLine "That registry value doesn't exist."
fnReadKeyValue = "FAIL"
else
fnReadKeyValue = strValue
end if
End Function
在所有变量都是变量的VBScript中,变量可以是两个特殊值之一:空或空。EMPTY被定义为具有未初始化值的变量,而NULL是不包含有效数据的变量 如果要测试变量“value”是NULL还是空,请使用以下If语句:
If IsNull(value) Or IsEmpty(value) Then
'...do something
End If
此处的关键点(并非双关语)是,如果该关键点不存在,并且OP已启用“错误恢复下一步”,则重新读取。或者,可以使用IsEmpty(value)
而不是IsNull(value)
。