开发者

vbscript and checking for null

开发者 https://www.devze.com 2023-01-12 22:48 出处:网络
On the line \"If (IsNull(value)) then\" below is my code correct? I want to 开发者_开发知识库check if the registry key exists and if not then show a web page.

On the line "If (IsNull(value)) then" below is my code correct? I want to 开发者_开发知识库check if the registry key exists and if not then show a web page.

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


In VBScript—where all variables are variants—variables can be one of two special values: EMPTY or NULL. EMPTY is defined as a variable with an un-initialized value, whereas NULL is a variable that contains no valid data.

If you want to test whether the variable namely 'value' is NULL or EMPTY then use following if statement:

If IsNull(value)  Or  IsEmpty(value) Then
   '...do something
End If


If RegRead throws an error, then value is not initialized; an uninitialized variable has the value Empty, not Null. Therefore, you should add the line

value = Null

after the Dim statement. Otherwise, IsNull would always return False.


Do you mean 'Null' or 'Nothing'?

In VBScript, Nothing means absence of value (or a null pointer). Null is used to represent NULL values from a database.

See this link for more info.

Also, see this example for how to detect if a registry key exists:

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


This is my solution to a business problem. They wanted to make USB read-only so data couldn't wander off on thumb drives. After pinging and connecting to WMI I had to determine whether or not the key already existed and the value was set. On a couple thousand computers.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消