2010-08-23 14 views
10

En la línea "If (IsNull (value)) then" below is my code correct? Quiero verificar si la clave de registro existe y si no, mostrar una página web.vbscript y comprobar nulo

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 

Respuesta

5

Si RegRead lanza un error, entonces se value no inicializado; una variable no inicializada tiene el valor Empty, no Null. Por lo tanto, se debe añadir la línea

value = Null 

después de la declaración Dim. De lo contrario, IsNull siempre devolvería False.

+0

La clave aquí (sin doble sentido) es que RegRead [lanza un error] (http : //msdn.microsoft.com/en-us/library/x05fawxd%28v=vs.84%29.aspx) si la clave no existe y el OP tiene 'On Resume Error Next' activado. Alternativamente, uno podría usar 'IsEmpty (value)' en lugar de 'IsNull (value)'. –

2

¿Quieres decir 'Null' o 'Nothing'?

En VBScript, Nothing significa ausencia de valor (o un puntero nulo). Null se usa para representar valores NULL de una base de datos.

Consulte this link para obtener más información.

También, ver this example de cómo detectar si existe una clave del registro:

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 
0

Ésta es mi solución a un problema de negocio. Querían hacer que el USB fuera solo de lectura para que los datos no pudieran desviarse en las memorias USB. Después de hacer ping y conectarme a WMI, tuve que determinar si la clave ya existía y si el valor estaba establecido. En un par de miles de computadoras.

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 
4

En VBScript, donde todas las variables son variantes, las variables pueden ser uno de dos valores especiales: VACÍO o NULO. EMPTY se define como una variable con un valor no inicializado, mientras que NULL es una variable que no contiene datos válidos.

Si desea probar si la variable es decir, 'valor' es nulo o vacío a continuación, utilizar siguiente sentencia if:

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