'On Error Resume Next
' main chunk of code originally written by Ian Diston
' code rewritten by MWJ - work in progress


set objShell = WScript.CreateObject( "WScript.Shell" )
set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
const HKCU = &H80000001
strComputer = "."
 
 
'Calls the isMember function with the specified group to check if current user is a member
If isMember("APP_Smartview") Then
		' clean up any legacy registry key values, then create correct ones re: smartview
		FunRegKeysDelete
		FunRegKeysCreate
	Else
		'Call funRegKeys to delete any keys not required - as user is NOT a member of the Smartview app group
		FunRegKeysDelete
End If
 

 
Function IsMember(groupName)
' function checks to see if the passed group name contains the current user. Returns True or False
	Set groupListDict = CreateObject("Scripting.Dictionary")
	groupListDict.CompareMode = 1
	ADSPath = EnvString("userdomain") & "/" & EnvString("username")
	Set userPath = GetObject("WinNT://" & ADSPath & ",user")
	For Each listGroup in userPath.Groups
		groupListDict.Add listGroup.Name, "-"
	Next
    IsMember = CBool(groupListDict.Exists(groupName))
End Function


 
Function EnvString(variable)
' function returns a particular environment variable's value.
' for example, if you use EnvString("username"), it would return the value of %username%.
    variable = "%" & variable & "%"
    EnvString = objShell.ExpandEnvironmentStrings(variable)
End Function



Function FunRegKeysDelete
strKeyPath = "Software\Microsoft\Office\11.0\Excel\Options"
objReg.EnumValues HKCU, strKeyPath, arrValueNames, arrValueTypes
 
For i = 0 to UBound(arrValueNames)
	If Left (arrValueNames(i),4)  = "OPEN" then
			objReg.GetStringValue HKCU, strKeyPath, arrValueNames(i), strValue
			If strValue = """C:\Oracle\SmartView\Bin\HsTbar.xla""" then
				strBlank = ""
				objReg.SetStringValue HKCU, strKeyPath, arrValueNames(i), strBlank 
			Else
				'Do nothing
			End If
	Else
		'Do nothing
	End If
Next 


' portion of code below is written to delete the hyperion.commonadin registry key and all sub-keys for the 4x relevant office apps
' keys for deletion are listed below:
strKeyPathtoDELETE1 = "Software\Microsoft\Office\Excel\Addins\Hyperion.CommonAddin"
strKeyPathtoDELETE2 = "Software\Microsoft\Office\PowerPoint\Addins\Hyperion.CommonAddin"
strKeyPathtoDELETE3 = "Software\Microsoft\Office\Word\Addins\Hyperion.CommonAddin"
strKeyPathtoDELETE4 = "Software\Microsoft\Office\Outlook\Addins\Hyperion.CommonAddin"

' delete the keys above using a code loop to cycle through them
for z = 1 to 4 
	strKeyPathtoDELETE = strKeyPathtoDELETE(z)
	DeleteSubkeys HKCU, strKeyPathtoDELETE
next
	
Sub DeleteSubkeys(HKEY_LOCAL_MACHINE, strKeyPathtoDELETE)
    objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPathtoDELETE, arrSubkeys
    If IsArray(arrSubkeys) Then
        For Each strSubkey In arrSubkeys
            DeleteSubkeys HKEY_LOCAL_MACHINE, strKeyPathtoDELETE & "\" & strSubkey
        Next
    End If
    objReg.DeleteKey HKEY_LOCAL_MACHINE, strKeyPathtoDELETE
End Sub

' end of the "FunRegKeysDelete" function
End Function
 


 
Function FunRegKeysCreate
'This function creates the registry keys depending on Group Membership.
strKeyPath1 = "Software\Microsoft\Office\11.0\Excel\Options"
objReg.EnumValues HKCU, strKeyPath1, arrValueNames, arrValueTypes

intCount = 0
For i = 0 to UBound(arrValueNames)
	If Left (arrValueNames(i),4)  = "OPEN" then
		intCount = intCount + 1
		'msgbox arrValueNames(i) & "-" & intCount
	Else
		'Do nothing
	End If
Next 

If intCount = 0 then
	'No OPEN key found so create first OPEN key
	strHSTbar = """C:\Oracle\SmartView\Bin\HsTbar.xla"""
	objReg.SetStringValue HKCU, strKeyPath1, "OPEN", strHSTbar
Else
	'OPEN found so create next OPEN value 
	strOpenValue = intcount
	'MsgBox "OPEN" & strOpenValue
	strHSTbar = """C:\Oracle\SmartView\Bin\HsTbar.xla"""
	objReg.SetStringValue HKCU, strKeyPath1, "OPEN" & strOpenValue, strHSTbar
End If


' section of code inserted to handle Hyperion.commonAddin registry key which is added to the addins for each office application respectively.
' additional key paths to have registry entries for Hyperion Toolbar added 
strKeyPath2 = "Software\Microsoft\Office\Excel\Addins\Hyperion.CommonAddin"
strKeyPath3 = "Software\Microsoft\Office\PowerPoint\Addins\Hyperion.CommonAddin"
strKeyPath4 = "Software\Microsoft\Office\Word\Addins\Hyperion.CommonAddin"
strKeyPath5 = "Software\Microsoft\Office\Outlook\Addins\Hyperion.CommonAddin"
' additional registry values for hyperion toolbar registry key entries - for the above regkey locations
strFriendlyName = """Oracle® Hyperion Smart View for Office, Fusion Edition"""
strDescription = """Oracle® Hyperion Smart View for Office, Fusion Edition"""
strDWORDLoadBehaviour = "00000000"
strDWORDCommandLineSafe = "00000000"

' enumerate registry values re: checking for the appropriate entries for the hyperion toolbar for each office application
objReg.EnumValues HKCU, strKeyPath2, arrValueNames2, arrValueTypes2
objReg.EnumValues HKCU, strKeyPath3, arrValueNames3, arrValueTypes3
objReg.EnumValues HKCU, strKeyPath4, arrValueNames4, arrValueTypes4
objReg.EnumValues HKCU, strKeyPath5, arrValueNames5, arrValueTypes5


for x = 2 to 5
' main loop to cycle through the repeated code for each of the above registry keys

	intCount = 0
	' check the 2nd array re: excel toolbar registry key presence, else create them
	For i = 0 to UBound(arrValueNames(x))
		If Left (arrValueNames(x)(i),13)  = "LoadBehaviour" then
			intCount = intCount + 1
			'msgbox arrValueNames(x)(i) & "-" & intCount
		Else
			'Do nothing
		End If
	Next 

	If intCount = 0 then
		'No LoadBehaviour key found so create relevant keys for the toolbar
		objReg.SetStringValue HKCU, strKeyPath(x), "FriendlyName", strFriendlyName
		objReg.SetStringValue HKCU, strKeyPath(x), "Description", strDescription
		objReg.SetDWORDValue HKCU, strKeyPath(x), "LoadBehaviour", strDWORDLoadBehaviour
		objReg.SetDWORDValue HKCU, strKeyPath(x), "CommandLineSafe", strDWORDCommandLineSafe
	Else
		' move along - nothing to see here
	End If

' end of the code loop
next


' end of the "FunRegKeysCreate" function
End Function




' Script Clean up Section
Set objShell = Nothing
Set objReg = Nothing

