Solved

Managing java exception list with vbscript

Posted on 2015-02-19
6
508 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
  • 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 500 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
 

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

786 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