Solved

Managing java exception list with vbscript

Posted on 2015-02-19
6
532 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Selenium docs api java index 3 48
Modify file details 1 14
Java Eclipse Loop 3 20
Save and close already open Excel file using VBscript 9 48
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

839 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