Solved

Need help with VBS code to modify the content of a file

Posted on 2014-01-21
13
1,097 Views
Last Modified: 2014-01-30
Hi,

We need modify the following file "exception.sites" (no extension) from the following path

C:\Users\username\AppData\LocalLow\Sun\Java\Deployment\security\

We need to add a few sites to the current "exception.sites" the sites have to be entered one per line, for example:

http://url1 
http://url2

The file can be modified by the users since they might be entering a site that has to be excluded from Java.

The script will run as part as the logon script and will add the sites to the local file on a per user basis.

We are trying to find a way to manage the new Java 7 U 51 exception list and warranty that the business sites are included in the exception.list file and also that the users will be able to manage their own exceptions.  

https://blogs.oracle.com/java-platform-group/entry/upcoming_exception_site_list_in

Anyone can give us a hand with the script?

Thank you!
0
Comment
Question by:llarava
  • 6
  • 5
  • 2
13 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 39797937
I recommend to change that to use a corporate (system) file and a user file, and merging those together on login. Otherwise you'll need to check whether the corporate sites are not already in the exception list - nothing really difficult, but sounds unnecessary.
0
 

Author Comment

by:llarava
ID: 39797972
Yes, that makes sense. How do you get that implemented?
0
 

Author Comment

by:llarava
ID: 39798005
So far I have the following:

Const ForAppending = 8
Const Open_as_Unicode = True

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

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

If (FSO.FileExists(strPath) = True) Then
  Set objFile = FSO.OpenTextFile(strPath, ForAppending, Open_as_Unicode)
  objFile.Writeline("test")
    objFile.Close : Set objFile = Nothing
End If

But I need to change - strFolder = "C:\Users\testuser\AppData\LocalLow\Sun\Java\Deployment\security" for a variable that captures the different users.

Also a way to control that sites are only added one time.

Could someone help to adjust to script?

Thank you!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39798442
Hi, I haven't tested this yet, but it should work.

Regards,

Rob.

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

Set objNetwork = CreateObject("WScript.Network")

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

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://URL1", 0
dctURLs.Add "http://URL2", 0

If (FSO.FileExists(strPath) = True) Then
	Set objFile = FSO.OpenTextFile(strPath, ForReading, Open_as_Unicode)
	' 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
	Set objFile = FSO.CreateTextFile(strPath, True, Open_as_Unicode)
	For Each strURL In dctURLs
		objFile.WriteLine strURL
	Next
	objFile.Close
	Set objFile = Nothing
End If 

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39798550
That script assumes that the root folder for user profiles is C:\Users, but I reckon that's sufficient in your case. Since it is only adding URLs, you can't tell to remove, but should you need that too, it is nothing difficult to implement.
0
 

Author Comment

by:llarava
ID: 39798984
Hi Rob,

Thanks for your reply.

I have executed the script 3 times and the URLs are added to the file every time the script is executed as shown below,

Notice how the first time the URL's are being added correctly then they are added again in a strange format, etc.

Since the script is going to run as part as the login script can the URL's be added just once.  

http://URL1
http://URL2
ÿþh t t p : / / U R L 1
 
 h t t p : / / U R L 2
 
 ÿ þ h   t   t   p   :   /   /   U   R   L   1  
   
 
   h   t   t   p   :   /   /   U   R   L   2  

Any thoughts?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 65

Expert Comment

by:RobSampson
ID: 39799038
Hmmm, try changing
Const Open_as_Unicode = True

Open in new window


to
Const Open_as_Unicode = -1

Open in new window


Rob.
0
 

Author Comment

by:llarava
ID: 39799067
I I have made the change as indicated but still


I have made the changed as indicated. I have executed the script 3 times again and this is the content of the file.

http://URL1
http://URL2
ÿþh t t p : / / U R L 1
 
 h t t p : / / U R L 2
 
 ÿ þ h   t   t   p   :   /   /   U   R   L   1  
   
 
   h   t   t   p   :   /   /   U   R   L   2  
   
 
   ÿ   þ
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39799104
Perhaps it's not supposed to be Unicode....try this.

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"

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://URL1", 0
dctURLs.Add "http://URL2", 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
	Set objFile = FSO.CreateTextFile(strPath, True, 0)
	For Each strURL In dctURLs
		objFile.WriteLine strURL
	Next
	objFile.Close
	Set objFile = Nothing
End If 

Open in new window


If that still fails, try to copy an unmodified file back to your user account and try the script again.

Rob.
0
 

Author Comment

by:llarava
ID: 39799872
Hi,

I made the change and the script works like a champ. One more quick question. The "exception.sites" doesn't get created until the user executes the Java Exception List and adds a file to it.

Could the script create the the file "exception.sites" in the following path:

User\AppData\LocalLow\Sun\Java\Deployment\security\

Then append the sites in the script to it.

Finally if the file "exception.sites" exists then do not create the file but execute the part of the script that adds/checks if the internal sites are there.

Thank you!
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 39801363
Sure, I think just moving the CreateTextFile part out of the If statement should do that.

Regards,

Rob.

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"

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://URL1", 0
dctURLs.Add "http://URL2", 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

Open in new window

0
 

Author Comment

by:llarava
ID: 39807782
PERFECT! Thank you!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39808262
No problem. Thanks for the grade.

Rob.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 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

25 Experts available now in Live!

Get 1:1 Help Now