Link to home
Start Free TrialLog in
Avatar of madhatter5501
madhatter5501Flag for United States of America

asked on

Need help developing vbscript

I am trying to write a script that will check the registry if a value exists and then remove it.

Does the following code make sense, or can it be improved?

'Check for Both Office 2007 and 2010 keys and if they exist to remove them'

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
Set objRegistry2 = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\AutoDiscover\RedirectServers\"
strKeyPath2 = "HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Autodiscover\RedirectServers\"

strValueName = ""

objRegistry.GetStringValue strKeyPath, strValueName, strValue2010
objRegistry.GetStringValue strKeyPath2, strValueName, strValue2007

	If IsNull(strValue2010) Then

		'Do Nothing
		
	Else

		Set WshShell = WScript.CreateObject("WScript.Shell")
		regEntry2010 = "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\AutoDiscover\RedirectServers\"
		WshShell.RegDelete regEntry2010

	ElseIf IsNull(strValue2007) Then

		'Do Nothing
		
	Else

		Set WshShell = WScript.CreateObject("WScript.Shell")
		regEntry2010 = "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\AutoDiscover\RedirectServers\"
		WshShell.RegDelete regEntry2010
	
	End If

'Function to removed the script after run the one time'
	Function removeScript()
		Set objFSO = CreateObject("Scripting.FileSystemObject")
		strScript = Wscript.RemoveOutlookAutodiscover.vbs
		objFSO.DeleteFile(strScript)
	End Function

Open in new window

Avatar of sirbounty
sirbounty
Flag of United States of America image

You can reduce it to ...

'Check for Both Office 2007 and 2010 keys and if they exist to remove them'

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strKeyRoot = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\VERSION\Outlook\AutoDiscover\RedirectServer\"

For Each strVer in Array("12.0","14.0")
  strValueName = ""
  strKeyPath = Replace(strKeyRoot, "VERSION", strVer)
  objRegistry.GetStringValue strKeyPath, strValueName, strValue

  If IsNull(strValue) Then
    'Do Nothing
  Else
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.RegDelete strKeyPath
  End If

Open in new window

Avatar of madhatter5501

ASKER

I get an error on Line 22 Expected Next.  So I added Next and then get an error on Line 14 Char 3 Error: Type Mismatch
Sorry, focused more on reducing the code than checking the logic.
Goal is to delete that key and everything below it?  Or a specific value?
If the former, it's as simple as

'Check for Both Office 2007 and 2010 keys and if they exist to remove them'

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyRoot = "SOFTWARE\Microsoft\Office\VERSION\Outlook\AutoDiscover\RedirectServer\"

For Each strVer in Array("12.0","14.0")
  strKeyPath = Replace(strKeyRoot, "VERSION", strVer)
  objRegistry.DeleteKey HKEY_CURRENT_USER, strKeyPath
Next

Open in new window

Unless you're wanting to double-check that the key exists.
There's no need to validate that it exists - the DeleteKey method won't throw an error if it's not there...
that works good for office 2010, I need to test still on 2007.  is there a way to make it delete the script from the hard drive once it has been run?
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for your help