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.1.1.0.0
'=========================================================================================
'   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= "\\OCTANT03.octaint.net\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 & "\" & f.name) then ' Install the DSN iff the driver is already there
			objShell.run "REGEDIT /s """ & SourcePath & "\" & f.name & """",,VBTRUE
		else
			Failed=vbTrue
		end if
	end	if
next

Set objShell = Nothing
Set objFSO = Nothing

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

wscript.echo "Success"
wscript.quit

' ===[ 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
	else	
		' 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
		else		
			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
					else
						DriverIsInstalled = VBFALSE
					end if			
				end if			
			Loop
			F.Close	
		end if
		Set objRegEx = Nothing
		set objFSO = nothing
		set F = Nothing
	end if
End function

[Example Registry File]
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources]
"Sample 1"="SQL Server Native Client 11.0"

[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\Sample 1]
"Driver"="C:\\Windows\\system32\\sqlncli11.dll"
"Description"="mytestDSN - RC v1"
"Server"="DT6663"
"Trusted_Connection"="Yes"
"ServerSPN"=""
"FailoverPartnerSPN"=""
Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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