Solved

Managing java exception list with vbscript

Posted on 2015-02-19
6
562 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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: 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.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

730 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