Solved

Managing java exception list with vbscript

Posted on 2015-02-19
6
450 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 24

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now