Compliance setting to copy DSNs into place

This compliance setting copies all the .REG files from a network folder into the local registry, effectively creating DSNs. It only makes the DSN if it can verify that the driver is installed.

This uses a regular expression (REGEX) to find the DRIVER entry in the reg file.  If it can’t find the driver executable in the correct windows folder, it skips the reg file and moves on to the next one.

This assumes you have a bunch of .REG files in a network folder, and you want to make sure each one is on the workstations.

The .REG file has a section for the “ODBC DATA SOURCES” and ODBC.INI sections as needed to properly register into the DSN system.  See below.

[VBScript code]
Option Explicit
' InstallODBCRegistryfiles.VBS v.
'   Purpose: Apply .REG files from network to instantiate the ODBC connections
'Deployment: Run as an SCCM Compliance setting
'    Author: Roger Cossaboom 8/7/2017
Dim F ' file
Dim Failed : Failed=vbFalse 

'dim  SourcePath: SourcePath= "\\\MAJOR05\ORAFILES"
dim  SourcePath: SourcePath= "\\orhome02\home1\Rcossaboom\Tasks\Scheduled Task\ODBC_registry_files"

' open to a shell for running the REGEDIT command
on error resume next 
dim objShell : Set objShell = WScript.CreateObject ("WScript.Shell")
on error goto 0
If  NOT IsObject(objShell) then wscript.echo "Failed" : wscript.quit 1

' Connect to the local drive
on error resume next 
Dim objFSO : Set objFSO = CreateObject("scripting.FileSystemObject")
on error goto 0
If not IsObject(objFSO) then wscript.echo "Failed" : Set objShell = Nothing : wscript.quit 1

' loop through each .REG file on the server, apply the file
For Each F In objFSO.GetFolder(SourcePath).Files
	If LCase(objFSO.GetExtensionName(f.Name)) = "reg" Then
		if DriverIsInstalled(SourcePath & "\" & then ' Install the DSN iff the driver is already there "REGEDIT /s """ & SourcePath & "\" & & """",,VBTRUE
		end if
	end	if

Set objShell = Nothing
Set objFSO = Nothing

if Failed then
	wscript.echo "Failed"
	wscript.quit 1
end if

wscript.echo "Success"

' ===[ check if driver is installed for this .REG file
Function DriverIsInstalled(ThisRegFileName)
	dim F, ThisLine, ThisDriverFileName
	Dim objRegEx : Set objRegEx = CreateObject("VBScript.RegExp")
	objRegEx.Global = True : objRegEx.IgnoreCase = True 
	objRegEx.Pattern = """Driver"" *= *" ' regex pattern

	' Connect to the local file system
	on error resume next
	Dim objFSO : Set objFSO = CreateObject("scripting.FileSystemObject")
	on error goto 0
	If not IsObject(objFSO) then 
		DriverIsInstalled = VBFALSE
		' read through the .REG file, looking for the DRIVER line
		on error resume next 	
		Set F = objFSO.OpenTextFile(ThisRegFileName,1,-2)
		on error goto 0
		If not IsObject(F) then 
			DriverIsInstalled = VBFALSE
			Do Until F.AtEndOfStream
				ThisLine = lcase(replace(F.ReadLine,"\\","\"))
				if left(ThisLine,8) = """driver""" then	
					ThisDriverFileName = objRegEx.Replace(ThisLine, "") ' remove leading info
					ThisDriverFileName = Replace(ThisDriverFileName,"""","") ' remove quotes
					if objFSO.Fileexists(ThisDriverFileName) then
						DriverIsInstalled = VBTRUE
						DriverIsInstalled = VBFALSE
					end if			
				end if			
		end if
		Set objRegEx = Nothing
		set objFSO = nothing
		set F = Nothing
	end if
End function

[Example Registry File]
Windows Registry Editor Version 5.00

"Sample 1"="SQL Server Native Client 11.0"

"Description"="mytestDSN - RC v1"

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s