locked
VBScript, check to see if registry keys exist before running script RRS feed

  • Question

  • Hi there, 

    I am trying to run the following script in my environment and want to ensure that once it's run once and the registry keys are in place, it does not execute again/ run again as this causes issues. I am going to set it up to run when users log into their machine via group policy so really need to make sure once run it won't execute again. Can anyone advise what I need to add to it to make sure it checks first for the keys in the registry before running again? e8cb48869c395445ade13e3c1c80d154 and 5cb93b24ff710741b7d83b9cb6317992 

    '-=-=-=-=-=-=-=-=-=-=-=-=-=-
    'Written By James McWhinney
    'Vancouver BC, Canada
    'www.importfanatik.com
    'April 26th, 2006
    '-=-=-=-=-=-=-=-=-=-=-=-=-=-

    const HKEY_CURRENT_USER = &H80000001
    strComputer = "."
    Set oReg=GetObject( "winmgmts:{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\default:StdRegProv")
    RegistryFolder = "Software\Microsoft\Windows NT\" & _ 
      "CurrentVersion\Windows Messaging Subsystem\Profiles\OutlookProfile\"

    LDAPdisplayname = "Domino Directory"
    LDAPserver = "dominodirectory.company.co.uk"
    LDAPport = "389"
    LDAPsearchbase = "o=company.co.uk"


    'Add Ldap Type Key
    sKeyPath = RegistryFolder & "e8cb48869c395445ade13e3c1c80d154\"
    oReg.CreateKey HKEY_CURRENT_USER, sKeyPath 
    oReg.SetBinaryValue HKEY_CURRENT_USER, _
         sKeyPath, "00033009", Array(0,0,0,0)
    oReg.SetBinaryValue HKEY_CURRENT_USER, _
         sKeyPath, "00033e03", Array(&H23,0,0,0)
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , _
         "001e3001", "Microsoft LDAP Directory"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , _
         "001e3006", "Microsoft LDAP Directory"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , _
         "001e300a", "EMABLT.DLL"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , _
         "001e3d09", "EMABLT"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , _
         "001e3d13", "{6485D268-C2AC-11D1-AD3E-10A0C911C9C0}"
    oReg.SetBinaryValue HKEY_CURRENT_USER, sKeyPath, _
        "01023d0c", Array(&H5c,&Hb9,&H3b,&H24, _
        &Hff,&H71,&H07,&H41,&Hb7,&Hd8,_
        &H3b,&H9c,&Hb6,&H31,&H79,&H92)

    'Add Ldap connection settings key
    sKeyPath = RegistryFolder & "5cb93b24ff710741b7d83b9cb6317992\"
    oReg.CreateKey HKEY_CURRENT_USER, sKeyPath
    oReg.SetBinaryValue HKEY_CURRENT_USER, _
         sKeyPath, "00033009", Array(&H20,0,0,0)
    oReg.SetBinaryValue HKEY_CURRENT_USER, sKeyPath, "000b6613", Array(0,0)
    oReg.SetBinaryValue HKEY_CURRENT_USER, sKeyPath, "000b6615", Array(0,0)
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e3001", LDAPdisplayname
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e3d09", "EMABLT"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e3d0a", "BJABLR.DLL"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e3d0b", "ServiceEntry"
    oReg.SetStringValue HKEY_CURRENT_USER, _
         sKeyPath , "001e3d13", "{6485D268-C2AC-11D1-AD3E-10A0C911C9C0}"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e6600", LDAPserver
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e6601", LDAPport
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e6602", ""
    oReg.SetStringValue HKEY_CURRENT_USER, _
         sKeyPath , "001e6603", LDAPsearchbase
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , _
         "001e6604", "(&(mail=*)(|(mail=%s*)" & _ 
         "(|(cn=%s*)(|(sn=%s*)(givenName=%s*)))))"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e6605", "SMTP"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e6606", "mail"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e6607", "60"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e6608", "100"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e6609", "120"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e660a", "15"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e660b", ""
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e660c", "OFF"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e660d", "OFF"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e660e", "NONE"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e660f", "OFF"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e6610", "postalAddress"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e6611", "cn"
    oReg.SetStringValue HKEY_CURRENT_USER, sKeyPath , "001e6612", "1"
    oReg.SetBinaryValue HKEY_CURRENT_USER, sKeyPath, "001e67f1", Array(&H0a)
    oReg.SetBinaryValue HKEY_CURRENT_USER, sKeyPath, "01023615", _
         Array(&H50,&Ha7,&H0a,&H61,&H55,&Hde,_
         &Hd3,&H11,&H9d,&H60,&H00,_
         &Hc0,&H4f,&H4c,&H8e,&Hfa)
    oReg.SetBinaryValue HKEY_CURRENT_USER, sKeyPath, "01023d01", _
         Array(&He8,&Hcb,&H48,&H86,&H9c,&H39,_
         &H54,&H45,&Had,&He1,&H3e,&H3c,_
         &H1c,&H80,&Hd1,&H54)
    oReg.SetBinaryValue HKEY_CURRENT_USER, sKeyPath, "01026631", _
         Array(&H98,&H17,&H82,&H92,&H5b,&H43,_
         &H03,&H4b,&H99,&H5d,&H5c,_
         &Hc6,&H74,&H88,&H7b,&H34)
    oReg.SetBinaryValue HKEY_CURRENT_USER, sKeyPath, "101e3d0f", _
         Array(&H02,&H00,&H00,&H00,&H0c,&H00,_
         &H00,&H00,&H17,&H00,&H00,&H00,_
         &H45,&H4d,&H41,&H42,&H4c,&H54,_
         &H2e,&H44,&H4c,&H4c,&H00,&H42,_
         &H4a,&H41,&H42,&H4c,&H52,&H2e,_
         &H44,&Hc,&H4c,&H00)

    'Append to Backup Key for ldap types
    sKeyPath = RegistryFolder & "9207f3e0a3b11019908b08002b2a56c2\"
    oReg.getBinaryValue HKEY_CURRENT_USER,sKeyPath, "01023d01",Backup
    Dim oldLength
    oldLength = UBound(Backup)
    ReDim Preserve Backup(oldLength+16)
    Backup(oldLength+1) = &He8
    Backup(oldLength+2) = &Hcb
    Backup(oldLength+3) = &H48
    Backup(oldLength+4) = &H86
    Backup(oldLength+5) = &H9c
    Backup(oldLength+6) = &H39
    Backup(oldLength+7) = &H54
    Backup(oldLength+8) = &H45
    Backup(oldLength+9) = &Had
    Backup(oldLength+10) = &He1
    Backup(oldLength+11) = &H3e
    Backup(oldLength+12) = &H3c
    Backup(oldLength+13) = &H1c
    Backup(oldLength+14) = &H80
    Backup(oldLength+15) = &Hd1
    Backup(oldLength+16) = &H54
    oReg.SetBinaryValue HKEY_CURRENT_USER, sKeyPath, "01023d01", Backup


    'Append to Backup Key for ldap connection settings
    sKeyPath = RegistryFolder & "9207f3e0a3b11019908b08002b2a56c2\"
    oReg.getBinaryValue HKEY_CURRENT_USER,sKeyPath, "01023d0e",Backup
    oldLength = UBound(Backup)
    ReDim Preserve Backup(oldLength+16)
    Backup(oldLength+1) = &H5c
    Backup(oldLength+2) = &Hb9
    Backup(oldLength+3) = &H3b
    Backup(oldLength+4) = &H24
    Backup(oldLength+5) = &Hff
    Backup(oldLength+6) = &H71
    Backup(oldLength+7) = &H07
    Backup(oldLength+8) = &H41
    Backup(oldLength+9) = &Hb7
    Backup(oldLength+10) = &Hd8
    Backup(oldLength+11) = &H3b
    Backup(oldLength+12) = &H9c
    Backup(oldLength+13) = &Hb6
    Backup(oldLength+14) = &H31
    Backup(oldLength+15) = &H79
    Backup(oldLength+16) = &H92
    oReg.SetBinaryValue HKEY_CURRENT_USER, sKeyPath, "01023d0e", Backup


    'Delete Active Books List Key
    sKeyPath = RegistryFolder & "9375CFF0413111d3B88A001" & _ 
               "04B2A6676\{ED475419-B0D6-11D2-8C3B-00104B2A6676}"
    oReg.DeleteKey HKEY_CURRENT_USER, sKeyPath


    • Edited by Blazingblu22 Friday, January 31, 2014 3:07 PM incorrect
    Friday, January 31, 2014 1:47 PM

Answers

  • I found this script here:

    http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=2065565

    And need to push it out to about 500 computers :)

    Pushing someone else's script out to 500 machines is a risky undertaking unless you understand every detail of the script, why it is there and what it does. You might end up having to visit 500 machines to fix some problem created by the script. Looks like a good way to fill an idle weekend! If you are responsible for 500+ machines then your best course of action is to attend a training course in PowerShell.
    • Marked as answer by Bill_Stewart Sunday, June 29, 2014 7:06 PM
    Friday, January 31, 2014 6:52 PM

All replies

  • whats the error you are receiving?
    Friday, January 31, 2014 2:29 PM
  • I'm not receiving an error, it runs silently and doesn't error. The script just runs again and populates an address book entry into the Outlook client for the second time, so if it keeps running it will continue adding this address book over and over, which isn't ideal. thanks
    Friday, January 31, 2014 2:32 PM
  • where in the script are you checking if the registry key exists or not?
    Friday, January 31, 2014 2:47 PM
  • That's what I need help with, just to add a section before the script starts to check if those keys exist, if they do then to exit, if they dont to continue.
    Friday, January 31, 2014 2:48 PM
  • I don't remember where I picked this up, but here's something from my old library:

    KeyExists "HKLM\Software\"
    
    Function KeyExists( regkey)
    	 Set shell = CreateObject("WScript.Shell")
    	 On Error GoTo 0
    	 keyVal = shell.RegRead(regkey)
    	 If Err.Number <> 0 Then
    		WScript.Echo Err.Message
    		KeyExists = False
    	 Else
                    Wscript.Echo "Key was found"
    		KeyExists = True
    	 End If
    End Function


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Friday, January 31, 2014 2:54 PM
  • what registry key do you need to check for? could you please explain instead of just posting the script. thank you!
    Friday, January 31, 2014 2:56 PM
  • I did, in my first post it said:

    I need to add to it to make sure it checks first for the keys in the registry before running againe8cb48869c395445ade13e3c1c80d154 and 5cb93b24ff710741b7d83b9cb6317992 

    thanks

    Friday, January 31, 2014 3:03 PM
  • I did, in my first post it said:

    I need to add to it to make sure it checks first for the keys in the registry before running againe8cb48869c395445ade13e3c1c80d154 and 5cb93b24ff710741b7d83b9cb6317992 

    thanks

    Use the function I posted above.

    KeyExists "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\EvOutlookDeploymentProfile\<your key>\"


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Friday, January 31, 2014 3:08 PM
  • Please try this.. hope it helps!

    const HKEY_CURRENT_USER = &H80000001
    strComputer = "."
    Set oReg=GetObject( "winmgmts:{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\default:StdRegProv")
    RegistryFolder = "Software\Microsoft\Windows NT\" & _ 
      "CurrentVersion\Windows Messaging Subsystem\Profiles\EvOutlookDeploymentProfile\"
      
       
    StrRegExistFlage = "No"
    
    
    strKeyPath = RegistryFolder
    oReg.EnumValues hDefKey, strKeyPath, arrValueNames, arrTypes
    If isarray(arrValueNames) then
    	For i = LBound(arrValueNames) To UBound(arrValueNames)
    		strRegFolderName = arrValueNames(i)
    			If strRegFolderName = "e8cb48869c395445ade13e3c1c80d154" Then
    				StrRegExistFlage = "Yes"
    			ElseIf strRegFolderName = "5cb93b24ff710741b7d83b9cb6317992" Then
    				StrRegExistFlage = "Yes"
    			End If
    	Next
    End If
    
    If StrRegExistFlage = "No" Then
    	<Execute the script>	
    Else
    	<Exit the script>
    End If
    

    Friday, January 31, 2014 3:08 PM
  • Thank you, let me try that and I will come back to you shortly.
    Friday, January 31, 2014 3:39 PM
  • Hi, so I added the above text to the script, just at the top of the script, but when running it now it gives me an error on line 33, saying 'Expected Statement' I really don't have any VB Experience, so any help you could provide would be great. Thanks
    Friday, January 31, 2014 5:29 PM
  • can you please run this script and tell me what you see. please run it on one computer with the regkey and one with out .

    const HKEY_CURRENT_USER = &H80000001
    strComputer = "."
    Set oReg=GetObject( "winmgmts:{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\default:StdRegProv")
    RegistryFolder = "Software\Microsoft\Windows NT\" & _ 
      "CurrentVersion\Windows Messaging Subsystem\Profiles\EvOutlookDeploymentProfile\"
      
       
    StrRegExistFlage = "No"
    
    
    strKeyPath = RegistryFolder
    oReg.EnumValues hDefKey, strKeyPath, arrValueNames, arrTypes
    If isarray(arrValueNames) then
    	For i = LBound(arrValueNames) To UBound(arrValueNames)
    		strRegFolderName = arrValueNames(i)
    			If strRegFolderName = "e8cb48869c395445ade13e3c1c80d154" Then
    				StrRegExistFlage = "Yes"
    			ElseIf strRegFolderName = "5cb93b24ff710741b7d83b9cb6317992" Then
    				StrRegExistFlage = "Yes"
    			End If
    	Next
    End If
    
    If StrRegExistFlage = "No" Then
    	Wscript.echo "Executing the script"
    Else
    	Wscript.echo "Exit the script"
    End If

    Friday, January 31, 2014 5:36 PM
  • Sure, I ran it on a machine without the registry key and it said, Executing Script. I ran it on a machine with the registry key and it also said Executing script.
    Friday, January 31, 2014 5:50 PM
  • can you please post the full registry path and key..
    Friday, January 31, 2014 5:53 PM
  • The one is:

    HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\EvOutlookDeploymentProfile\e8cb48869c395445ade13e3c1c80d154

    The other is:

    HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\EvOutlookDeploymentProfile\5cb93b24ff710741b7d83b9cb6317992

    Friday, January 31, 2014 5:58 PM
  • I found this script here:

    http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=2065565

    And need to push it out to about 500 computers :)

    Friday, January 31, 2014 5:59 PM
  • I found this script here:

    http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=2065565

    And need to push it out to about 500 computers :)

    Pushing someone else's script out to 500 machines is a risky undertaking unless you understand every detail of the script, why it is there and what it does. You might end up having to visit 500 machines to fix some problem created by the script. Looks like a good way to fill an idle weekend! If you are responsible for 500+ machines then your best course of action is to attend a training course in PowerShell.
    • Marked as answer by Bill_Stewart Sunday, June 29, 2014 7:06 PM
    Friday, January 31, 2014 6:52 PM
  • Have you tried the function I posted?

    It works.


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Friday, January 31, 2014 7:12 PM
  • Const HKEY_CURRENT_USER  = &H80000001
    StrRegExistFlage = "No"
    strComputer = "."
    Set oReg=GetObject( "winmgmts:{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\default:StdRegProv")
    strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\EvOutlookDeploymentProfile"
    oReg.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubKeys
    If isarray(arrSubKeys) then
    	For Each subkey In arrSubKeys
      	If subkey = "e8cb48869c395445ade13e3c1c80d154" Then
      		StrRegExistFlage = "Yes"
      	ElseIf subkey = "5cb93b24ff710741b7d83b9cb6317992" Then
      		StrRegExistFlage = "Yes"
      	End If
    	Next
    Else
    	Wscript.echo "Reg folder not found"
    End If
    
    If StrRegExistFlage = "Yes" Then
    	Wscript.echo "exit script"
    Else
    	Wscript.echo "execute script"
    End If

    Friday, January 31, 2014 7:21 PM
  • this will help to read computer names from c:\computerlist.txt and connect to each computer and check if the registry exists or not. you need to make sure you run with user that has access to all the computer. this script only checks if the regkey exists or not. you will have to include logic from your script as to what actually should happen when registry key not found.  as always please use this as example and use it at your own risk. 

    Const HKEY_CURRENT_USER = &H80000001
    Const FORREADING = 1
    
    strComputer = "."
    inFilename = "c:\Computerlist.txt"
    Set fsoIn = CreateObject("scripting.filesystemobject")
    Set inFile = fsoIn.OpenTextFile(inFilename, ForReading, True)
    
    While Not inFile.AtEndOfStream
    	arrComputerNames = Split(inFile.Readline,",")
    	' arrComputerNames(0) contains the computer name
    	strComputer = arrComputerNames(0)
    	StrRegExistFlage = "No"
    	Set oReg=GetObject( "winmgmts:{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\default:StdRegProv")
    	strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\EvOutlookDeploymentProfile"
    	oReg.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubKeys
    	If isarray(arrSubKeys) then
    		For Each subkey In arrSubKeys
      		If subkey = "e8cb48869c395445ade13e3c1c80d154" Then
      			StrRegExistFlage = "Yes"
      		ElseIf subkey = "5cb93b24ff710741b7d83b9cb6317992" Then
      			StrRegExistFlage = "Yes"
      		End If
    		Next
    	Else
    		Wscript.echo "Reg folder not found on computer " & strComputer
    	End If
    	If StrRegExistFlage = "Yes" Then
    		Wscript.echo "exit script on computer " & strComputer
    	Else
    		Wscript.echo "execute script on comptuer " & strComputer
    	End If
    Wend
    inFile.close

    Friday, January 31, 2014 7:44 PM
  • Hi, before pushing it out I will test it on a number of machines, in  a controlled fashion. It has been used before by others and I have tested it already on a few machines with no issues. The only issue so far is that if run more than once it adds another address book entry. I appreciate your comments though. I have come to a forum where I would hope to find other experienced users who have the knowledge and insight to be able to help, I have not just deployed it via Group Policy to 500 machines without thinking. I realize the damage this could cause.
    Friday, January 31, 2014 10:25 PM
  • Hi Mike, 

    How would I integrate your code into my script, as mentioned before I have really no knowledge of VBScript and would need some more assistance with where to fit this in. Can you possibly show me how I would include this into my script?

    Thanks for your help, really appreciate it.

    Friday, January 31, 2014 10:29 PM
  • I found this script here:

    http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=2065565

    And need to push it out to about 500 computers :)


    I found much better solution to this problem: https://blogs.msdn.microsoft.com/emeamsgdev/2016/06/09/mapi-utility-to-add-ldap-address-books-to-existing-outlook-profiles/

    Although this simple command line tool is not "official", it comes from Microsoft and is based on MAPI API, so it's more reliable than modifying registry based on empirical investigation.

    Tuesday, July 11, 2017 6:23 AM