?
Solved

Managing java exception list with vbscript

Posted on 2015-02-19
6
Medium Priority
?
687 Views
Last Modified: 2015-02-23
Hello,
The following script add java exception list.
I would like to include and exit sub if the java version of the user i bellow 7.45.

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const Open_as_Unicode = -1

Set objNetwork = CreateObject("WScript.Network")

strFolder = "C:\Users\" & objNetwork.UserName & "\AppData\LocalLow\Sun\Java\Deployment\security"
strFile = "exception.sites"

intAnswer = _
    Msgbox("Are your sure that you want to add exception sites? (this script is just required for java versions above 7.45)", _
        vbYesNo, "Exception site")
            
If intAnswer = vbNo Then
wscript.Quit

Else

strPath = strFolder & "\" & strFile
Set FSO = CreateObject("Scripting.FileSystemObject")

Set dctURLs = CreateObject("Scripting.Dictionary")
dctURLs.CompareMode = vbTextCompare

' Add corporate URLs that are required here
dctURLs.Add "http://test.com", 0



If (FSO.FileExists(strPath) = True) Then
      Set objFile = FSO.OpenTextFile(strPath, ForReading, 0)
      ' Read through the current list to maintain any user added URLs
      While Not objFile.AtEndOfStream
            strURL = Trim(objFile.ReadLine)
            If strURL <> "" Then
                  If dctURLs.Exists(strURL) = False Then dctURLs.Add strURL, 0
            End If
      Wend
      objFile.Close
      Set objFile = Nothing
End If
Set objFile = FSO.CreateTextFile(strPath, True, 0)
For Each strURL In dctURLs
      objFile.WriteLine strURL
Next
objFile.Close
Set objFile = Nothing
End If
MsgBox("Exception sites have been added")
0
Comment
Question by:LD16
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:Mohammed Khawaja
ID: 40619368
I am not much of a VB scripter, however, you could do the following:

1.  Create a batch file with the following lines:

echo off
wmic product where "Name like '%Java% and Version < 7.45 " get name, version | find "Java" > nul
if not errorlevel 1 cscript addjavaexception.vbs

2.  Rename your vbs file to addjavaexception.vbs

WMIC can take a minute or so to get the required data but this should work.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 40622606
Hi there, rather than use WMI to query the Win32_Product class (since that can be problematic), you can try this code which enumerates the Uninstall registry keys to determine whether the Java version is over a specific version (by Java version and update number).

Regards,

Rob.

Const HKEY_LOCAL_MACHINE = &H80000002
intMajorVersion = 7
intMinorVersion = 45
blnOverRequiredVersion = False
arrKeys = Array("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall")
Set objRegistry = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\default:StdRegProv")
For Each strKey In arrKeys
	objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKey, arrSubKeys
	If Not IsNull(arrSubKeys) Then
		For Each strSubKey In arrSubKeys
			objRegistry.GetStringValue HKEY_LOCAL_MACHINE, strKey & "\" & strSubKey, "DisplayName", strDisplayName
			objRegistry.GetStringValue HKEY_LOCAL_MACHINE, strKey & "\" & strSubKey, "DisplayVersion", strDisplayVersion
			objRegistry.GetStringValue HKEY_LOCAL_MACHINE, strKey & "\" & strSubKey, "UninstallString", strUninstallString
			If InStr(1, strDisplayName, "Java ", vbTextCompare) > 0 And InStr(1, strDisplayName, " Update ", vbTextCompare) > 0 Then
				arrVersion = Split(strDisplayName, " ")
				'WScript.Echo strDisplayName 
				'WScript.Echo "Major: " & arrVersion(1) & " Minor: " & arrVersion(3)
				If Int(arrVersion(1)) >= Int(intMajorVersion) And Int(arrVersion(3)) >= Int(intMinorVersion) Then blnOverRequiredVersion = True
			End If
			strDisplayName = vbEmpty
			strDisplayVersion = vbEmpty
			strUninstallString = vbEmpty
		Next
	End If
Next
If blnOverRequiredVersion = True Then
	'WScript.Echo "Running exception list"
	Const ForReading = 1
	Const ForWriting = 2
	Const ForAppending = 8
	Const Open_as_Unicode = -1
	
	Set objNetwork = CreateObject("WScript.Network")
	
	strFolder = "C:\Users\" & objNetwork.UserName & "\AppData\LocalLow\Sun\Java\Deployment\security"
	strFile = "exception.sites"
	
	intAnswer = _
	Msgbox("Are your sure that you want to add exception sites? (this script is just required for java versions above 7.45)", _
	vbYesNo, "Exception site")
	
	If intAnswer = vbYes Then
		strPath = strFolder & "\" & strFile
		Set FSO = CreateObject("Scripting.FileSystemObject")
	
		Set dctURLs = CreateObject("Scripting.Dictionary")
		dctURLs.CompareMode = vbTextCompare
	
		' Add corporate URLs that are required here
		dctURLs.Add "http://test.com", 0
	
		If (FSO.FileExists(strPath) = True) Then
			Set objFile = FSO.OpenTextFile(strPath, ForReading, 0)
			' Read through the current list to maintain any user added URLs
			While Not objFile.AtEndOfStream
				strURL = Trim(objFile.ReadLine)
				If strURL <> "" Then
					If dctURLs.Exists(strURL) = False Then dctURLs.Add strURL, 0
				End If
			Wend
			objFile.Close
			Set objFile = Nothing
		End If
		Set objFile = FSO.CreateTextFile(strPath, True, 0)
		For Each strURL In dctURLs
			objFile.WriteLine strURL
		Next
		objFile.Close
		Set objFile = Nothing
	End If
	MsgBox("Exception sites have been added")
Else
	MsgBox "Your Java version is less than Java " & strMajorVersion & " Update " & strMinorVersion & ". Exceptions not required"
End If

Open in new window

0
 
LVL 1

Author Comment

by:LD16
ID: 40622997
@Rob,

Is there a way to make the following correction:

intAnswer = _
	Msgbox("Are your sure that you want to add exception sites? (this script is just required for java versions above 7.45)", _
	vbYesNo, "Exception site")
	
	If intAnswer = vbYes Then

If intAnswer = vbNo Then
wscript.Quit

Open in new window


Otherwise with your code if I click No and I have a version above 7.45 I add the exception lists
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 65

Expert Comment

by:RobSampson
ID: 40623489
Move line 71 up one. When you click no, it doesn't add the exceptions but does show the message. Moving that line up will not show the message when you click No. Sorry about that.

Rob.
0
 
LVL 1

Author Comment

by:LD16
ID: 40625511
Hello, I have followed your recommendations and it works!
Thank you!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40626599
Good to hear.  Glad to help.

Rob.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question