tags:

views:

28

answers:

3

Hi all,

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
A: 

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.

fmunkert
A: 

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
Kilanash
A: 

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
gWaldo