2010-08-23 14 views
10

Sulla riga "If (IsNull (valore)) then" di seguito è corretto il mio codice? Voglio verificare se la chiave del Registro di sistema esiste e, in caso contrario, mostrare una pagina web.vbscript e controllo per null

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 

risposta

5

Se RegRead genera un errore, quindi value non viene inizializzata; una variabile non inizializzata ha il valore Empty, non Null. Pertanto, è necessario aggiungere la riga

value = Null 

dopo l'istruzione Dim. Altrimenti, IsNull restituirebbe sempre False.

+0

La chiave qui (no pun intended) è che RegRead [genera un errore] (http : //msdn.microsoft.com/en-us/library/x05fawxd%28v=vs.84%29.aspx) se la chiave non esiste e l'OP ha acceso l'opzione "Ripristino errore successivo". In alternativa, si potrebbe usare 'IsEmpty (valore)' invece di 'IsNull (valore)'. –

2

Intendi "Null" o "Nothing"?

In VBScript, Nothing indica assenza di valore (o un puntatore nullo). Null viene utilizzato per rappresentare valori NULL da un database.

Vedere this link per ulteriori informazioni.

Inoltre, vedere this example per come rilevare se una chiave di registro esiste:

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

Questa è la mia soluzione ad un problema di business. Volevano rendere l'USB di sola lettura in modo che i dati non potessero andare in giro su chiavette USB. Dopo il ping e la connessione a WMI ho dovuto determinare se la chiave esistesse già e il valore fosse impostato. Su un paio di migliaia di computer.

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

In VBScript cui tutte le variabili sono varianti-variabili possono essere di due valori speciali: vuoto o nullo. EMPTY è definito come una variabile con un valore non inizializzato, mentre NULL è una variabile che non contiene dati validi.

Se si desidera verificare se la variabile vale a dire 'valore' è nullo o vuoto quindi utilizzare seguente if:

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