vbs script to copy PAC file to local machine

Hello,

I am trying to implement the use of a PAC file to set the browser proxy settings.

I have the PAC file saved to a network share.

I need a logon script that will copy the file to the users local machine. the problem that i am having is that the users don't have local admin rights, so the script fails with access denied.

Const OverwriteExisting = True
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objName= CreateObject("wscript.network")
 
objFSO.CopyFile "\\server\PAC\PAC.pac" , "C:\Windows\System32\Drivers\etc\PAC.pac" , OverwriteExisting


I have thought of trying to save it to the users profile, but not sure if GPO to to direct the proxy setting with allow %username% to be used?

Any suggestions?

Thanks
shipownersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SEWS-ECommented:
Hi,

Here's a script I wrote to do just that. It creates a folder called IT on the C:\ drive, then modifies the folder permissions so the script can copy the new PAC file to it, and then changes the permissions back so they can't tamper with it when the script ends.

Just modify it to suit your needs :)

''''''''''''''''''''''''''''''''''''''''''
'CREATOR: Paul Stone
'CREATED: 09/02/2011
'TITLE: distributePAC.vbs
'DESCRIPTION: The script is used to distribute
' a PAC file to roaming web access users.
'It also creates an IT folder on the users
'local pc.
'''''''''''''''''''''''''''''''''''''''''''
'On Error resume next

Option Explicit

'This part of the script creates the dir C:\IT

Dim objFSO, objFolder, objShell, strDirectory, strPAC, strLocalPAC
Dim intRunError

'Constants for FileSystemObject
Const DeleteReadOnly = TRUE
Const OverwriteExisting = TRUE

strDirectory = "c:\IT"

' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objShell = CreateObject("Wscript.Shell")

' Note If..Exists. Then, Else ... End If construction
If NOT objFSO.FolderExists(strDirectory) Then
	Set objFolder = objFSO.CreateFolder(strDirectory)
	'wscript.Echo "Just created " & strDirectory
	Else
		' Assign user permission to home folder.
		'wscript.echo "IT folder exists"
		'wscript.echo "Changing permissions on IT folder"
		
		intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " _
		& strDirectory & " /t /c /p Administrators:F Users:C", 2, True)
		
		If intRunError <> 0 Then
			'Wscript.Echo "Error assigning permissions for user " _
			'& " to home folder " & strDirectory
		End If
		
		'wscript.quit
	End If

'Copy the pac file to the new IT folder

'Set variable for location of NEW pac file
strPAC = "\\domaincontroller01\netlogon\PAC Files\roamingpac.pac"

strlocalPAC = strDirectory & "\roamingpac.pac"

'wscript.echo strlocalPAC

'Set objFSO = CreateObject("Scripting.FileSystemObject")

'wscript.Echo "Performing some checks"

'Check if PAC file exists, and copy or overwrite with new PAC
IF NOT objFSO.FileExists(strlocalPAC) THEN
	objFSO.CopyFile strPAC , strDirectory & "\"
	'wscript.Echo "Copying new pac file to " & strLocalPAC
ELSE
	objFSO.CopyFile strPAC , strDirectory & "\", TRUE
	'wscript.Echo "Copying new pac file to " & strLocalPAC
END IF

'This part of the script modifies the permissions on the C:\IT folder

'wscript.echo "Changing Permissions Again!"

Set objShell = CreateObject("Wscript.Shell")
'Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists(strDirectory) Then
	' Assign user permission to home folder.
	intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " _
	& strDirectory & " /t /c /p Administrators:F Users:R", 2, True)
	If intRunError <> 0 Then
		'Wscript.Echo "Error assigning permissions for user " _
		'& " to home folder " & strDirectory
	End If
End If

'wscript.echo "Boom!! Script completed!"
WScript.Quit

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shipownersAuthor Commented:
Excellent thanks, working well so far.
0
shipownersAuthor Commented:
Hi SEWS-E,

I'm not sure how to add additional points to this, but would you be able to point me in the direction of adding code to copy/overwrite the proxy.pac file at every logon?

I needed to add an additional site in the PAC to bypass the proxy, but it appears that once the file is in the 'IT' directory it isn't overwritten..

Thanks in advance.
0
SEWS-ECommented:
Hi,

Looking at the script it looks like this should already be happening on lines 64 - 70:

IF NOT objFSO.FileExists(strlocalPAC) THEN
      objFSO.CopyFile strPAC , strDirectory & "\"
      'wscript.Echo "Copying new pac file to " & strLocalPAC
ELSE
      objFSO.CopyFile strPAC , strDirectory & "\", TRUE
      'wscript.Echo "Copying new pac file to " & strLocalPAC
END IF

Basically this checks whether the file (as declared in the variable strlocalPAC) is NOT on the PC. If it is NOT, it copies the pac file, and funnily enough, if it is on the pc, it still copies it!

However, if the newer version of the PAC file is not being copied as expected here's how I have handled this in the past.

1. Change the name of the new pac file on the server to mypac.pac.new.
2. Check whether mypac.pac.new is present on the local system (it won't be)
3. Copy mypac.pac.new to the local system
4. Delete, the old pac file (you can rename it if you need to keep it for historical purposes)
5. Rename mypac.pac.new to mypac.pac

Hope that helps :)
0
shipownersAuthor Commented:
Thanks,

I'll give it another go.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.